/// <summary> /// 获取BNY文件的结束里程 /// </summary> /// <param name="bnyFilePath">bny文件路径</param> /// <returns>结束里程</returns> public float GetBNYEndMile(string bnyFilePath) { try { FileStream fs = new FileStream(bnyFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); BinaryReader br = new BinaryReader(fs, Encoding.Default); br.BaseStream.Position = 0; int iChannelNumberSize = BNYFile.GetChannelSize(); br.BaseStream.Position = br.BaseStream.Length - iChannelNumberSize; byte[] b = new byte[iChannelNumberSize]; b = br.ReadBytes(iChannelNumberSize); float fGL = (BitConverter.ToSingle(b, (3 - 1) * BNYFile.oneByteSize)); br.Close(); fs.Close(); return(fGL); } catch (Exception ex) { throw new Exception(ex.Message, ex); } }
/// <summary> /// 跳转按钮事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnGo_Click(object sender, EventArgs e) { this.btnPrev.Enabled = true; if (Convert.ToInt32(this.txtGoPage.Text.Trim()) > pageCount) { MessageBox.Show("您输入的页数大于总页数"); return; } else { startPosition = (pageIndex - 1) * rowCount * BNYFile.GetChannelSize(); var listAll = common.GetBNYData(bnyPath, rowCount, startPosition, ref endPosition); pageIndex = Convert.ToInt32(this.txtGoPage.Text.Trim()); DisplayData(listAll); this.txtCurrentPage.Text = pageIndex.ToString(); if (pageIndex == pageCount) { this.btnNext.Enabled = false; } if (pageIndex == 1) { this.btnPrev.Enabled = false; } } }
/// <summary> /// 查询指定采样点的BNY数据集合 /// </summary> /// <param name="bnyFilePath">bny文件路径</param> /// <param name="channelNums">通道号数组</param> /// <param name="sampleNum">采样点个数</param> /// <param name="startFilePos">开始位置</param> /// <param name="endFilePos">结束位置</param> /// <returns>数据集合</returns> public List <float[]> GetBNYData(string bnyFilePath, int[] channelNums, int sampleNum, long startFilePos, ref long endFilePos) { try { FileStream fs = new FileStream(bnyFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); BinaryReader br = new BinaryReader(fs, Encoding.Default); br.BaseStream.Position = startFilePos; int iChannelNumberSize = BNYFile.GetChannelSize(); endFilePos = startFilePos + iChannelNumberSize; if (endFilePos > br.BaseStream.Length) { endFilePos = br.BaseStream.Length; sampleNum = Convert.ToInt32((endFilePos - startFilePos) / iChannelNumberSize); } byte[] b = new byte[iChannelNumberSize]; List <float[]> allList = new List <float[]>(); int channelCount = channelNums.Length; for (int i = 0; i < channelCount; i++) { float[] array = new float[sampleNum]; allList.Add(array); } for (int i = 0; i < sampleNum; i++) { b = br.ReadBytes(iChannelNumberSize); for (int j = 0; j < channelCount; j++) { float fGL = (BitConverter.ToSingle(b, (channelNums[j] - 1) * BNYFile.oneByteSize)); allList[j][i] = fGL; } } br.Close(); fs.Close(); return(allList); } catch (Exception ex) { throw new Exception(ex.Message, ex); } }
/// <summary> /// 获取BNY文件里程的位置 /// </summary> /// <param name="bnyFilePath">bny文件路径</param> /// <param name="isInc">是否为增里程</param> /// <param name="mile">里程</param> /// <returns>位置</returns> public long GetMilePosition(string bnyFilePath, bool isInc, float mile) { try { long position = -1; FileStream fs = new FileStream(bnyFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); BinaryReader br = new BinaryReader(fs, Encoding.Default); br.BaseStream.Position = 0; int iChannelNumberSize = BNYFile.GetChannelSize(); long endFilePos = br.BaseStream.Length; int sampleNum = Convert.ToInt32(endFilePos / iChannelNumberSize); byte[] b = new byte[iChannelNumberSize]; for (int i = 0; i < sampleNum; i++) { b = br.ReadBytes(iChannelNumberSize); float fGL = (BitConverter.ToSingle(b, (3 - 1) * BNYFile.oneByteSize)); if (isInc) { if (fGL >= mile) { position = br.BaseStream.Position - iChannelNumberSize; break; } } else { if (fGL <= mile) { position = br.BaseStream.Position - iChannelNumberSize; break; } } } br.Close(); fs.Close(); return(position); } catch (Exception ex) { throw new Exception(ex.Message, ex); } }
/// <summary> /// 上一页按钮事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnPrev_Click(object sender, EventArgs e) { //凡是点击上一页下一页总会可用 20170927 this.btnNext.Enabled = true; startPosition = (pageIndex - 1) * rowCount * BNYFile.GetChannelSize(); var listAll = common.GetBNYData(bnyPath, rowCount, startPosition, ref endPosition); pageIndex--; DisplayData(listAll); this.txtCurrentPage.Text = pageIndex.ToString(); if (pageIndex == 1) { this.btnPrev.Enabled = false; } }
/// <summary> /// 查询按钮事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSearch_Click(object sender, EventArgs e) { bnyPath = txtBNYFilePath.Text.Trim(); if (bnyPath == "") { MessageBox.Show("请先选择BNY文件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } //bnyPath = @"H:\工作文件汇总\铁科院\程序\工具\BNY\data\20170409京广高速上行(广州南~武汉)_001.bny"; //bnyPath = @"F:\个人文件\铁路\工程代码\文件工具\BNY工具\data\20170409京广高速上行(广州南~武汉)_001.bny"; long bnyFileLength = common.GetBNYLastPosition(bnyPath); pageCount = 0; pageCount = Convert.ToInt32(bnyFileLength / (rowCount * BNYFile.GetChannelSize())); if (bnyFileLength % (rowCount * BNYFile.GetChannelSize()) > 0) { pageCount += 1; } var listAll = common.GetBNYData(bnyPath, rowCount, startPosition, ref endPosition); startPosition = endPosition; this.txtCurrentPage.Text = pageIndex.ToString(); this.lblTotalPage.Text = pageCount.ToString(); float startmiled = common.GetBNYStartMile(bnyPath); float endmiled = common.GetBNYEndMile(bnyPath); if (startmiled < endmiled) { isInc = true; } this.lbl_StartMile.Text = startmiled.ToString(); this.lbl_EndMile.Text = endmiled.ToString(); DisplayData(listAll); }
/// <summary> /// 下一页按钮事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnNext_Click(object sender, EventArgs e) { this.btnPrev.Enabled = true; if (pageIndex >= pageCount) { return; } else { startPosition = (pageIndex - 1) * rowCount * BNYFile.GetChannelSize(); var listAll = common.GetBNYData(bnyPath, rowCount, startPosition, ref endPosition); pageIndex++; DisplayData(listAll); this.txtCurrentPage.Text = pageIndex.ToString(); if (pageIndex == pageCount) { this.btnNext.Enabled = false; } } }
/// <summary> /// 获取BNY数据并向cit文件中写入数据 /// </summary> /// <param name="channelList"></param> private void GetBnyDataAndWriteCit(string citPath, string bnyPath, List <ChannelNew> channelList) { CitFileHelper citHelper = new CitFileHelper(); int[] bnyChannelArr = channelList.Where(s => s.BNYChannelId >= 0).Select(s => s.BNYChannelId).ToArray(); int sampleNum = 50000; long startPosition = 0; long endPosition = 0; long bnyFileLength = common.GetBNYLastPosition(bnyPath); int pageCount = 0; double realPageCount = bnyFileLength * 1.0 / (sampleNum * BNYFile.GetChannelSize()); if (realPageCount % ((int)realPageCount) > 0) { pageCount = ((int)realPageCount) + 1; } else { pageCount = (int)realPageCount; } List <float[]> bnyList = new List <float[]>(); List <float[]> resultList = new List <float[]>(); float[] fvalue = new float[sampleNum]; for (int i = 0; i < pageCount; i++) { bnyList = common.GetBNYData(bnyPath, sampleNum, startPosition, ref endPosition); startPosition = endPosition; resultList.Clear(); for (int j = 0; j < channelList.Count; j++) { if (channelList[j].BNYChannelId >= 0) { if (bnyList[channelList[j].BNYChannelId].Length < sampleNum) { fvalue = new float[bnyList[channelList[j].BNYChannelId].Length]; if (channelList[j].BNYChannelId == 2) { if (channelList[j].dataChannelInfo.sNameEn == "KM") { fvalue = bnyList[channelList[j].BNYChannelId].Select(p => ((float)(int)p)).ToArray(); } else if (channelList[j].dataChannelInfo.sNameEn == "M") { fvalue = bnyList[channelList[j].BNYChannelId].Select(p => (p - (int)p) * 1000).ToArray(); } else { fvalue = bnyList[channelList[j].BNYChannelId]; } } else { fvalue = bnyList[channelList[j].BNYChannelId]; } } else { if (channelList[j].BNYChannelId == 2) { if (channelList[j].dataChannelInfo.sNameEn == "KM") { fvalue = bnyList[channelList[j].BNYChannelId].Select(p => ((float)(int)p)).ToArray(); } else if (channelList[j].dataChannelInfo.sNameEn == "M") { fvalue = bnyList[channelList[j].BNYChannelId].Select(p => (p - (int)p) * 1000).ToArray(); } else { fvalue = bnyList[channelList[j].BNYChannelId]; } } else { fvalue = bnyList[channelList[j].BNYChannelId]; } } } else { fvalue = new float[bnyList[0].Length]; } resultList.Add(fvalue); } citHelper.WriteChannelDataFloat(citPath, resultList); if (tokenSource.IsCancellationRequested) { throw new OperationCanceledException(); } } }
private void GetBnyData(string bnyPath, List <ChannelNew> channelList) { int sampleNum = 5000; long startPosition = 0; long endPosition = 0; long bnyFileLength = common.GetBNYLastPosition(bnyPath); int pageCount = 0; double realPageCount = bnyFileLength * 1.0 / (sampleNum * BNYFile.GetChannelSize()); if (realPageCount % ((int)realPageCount) > 0) { pageCount = ((int)realPageCount) + 1; } else { pageCount = (int)realPageCount; } List <float[]> bnyList = new List <float[]>(); List <float[]> resultList = new List <float[]>(); float[] fvalue = new float[sampleNum]; for (int i = 0; i < pageCount; i++) { bnyList = common.GetBNYData(bnyPath, sampleNum, startPosition, ref endPosition); startPosition = endPosition; resultList.Clear(); for (int j = 0; j < channelList.Count; j++) { if (channelList[j].BNYChannelId >= 0) { if (bnyList[channelList[j].BNYChannelId].Length < sampleNum) { fvalue = new float[bnyList[channelList[j].BNYChannelId].Length]; if (channelList[j].BNYChannelId == 2) { if (channelList[j].dataChannelInfo.sNameEn == "KM") { fvalue = bnyList[channelList[j].BNYChannelId].Select(p => ((float)(int)p)).ToArray(); } else if (channelList[j].dataChannelInfo.sNameEn == "M") { fvalue = bnyList[channelList[j].BNYChannelId].Select(p => (p - (int)p) * 1000).ToArray(); } else { fvalue = bnyList[channelList[j].BNYChannelId]; } } else { fvalue = bnyList[channelList[j].BNYChannelId]; } } else { if (channelList[j].BNYChannelId == 2) { if (channelList[j].dataChannelInfo.sNameEn == "KM") { fvalue = bnyList[channelList[j].BNYChannelId].Select(p => ((float)(int)p)).ToArray(); } else if (channelList[j].dataChannelInfo.sNameEn == "M") { fvalue = bnyList[channelList[j].BNYChannelId].Select(p => (p - (int)p) * 1000).ToArray(); } else { fvalue = bnyList[channelList[j].BNYChannelId]; } } else { fvalue = bnyList[channelList[j].BNYChannelId]; } } } else { fvalue = new float[bnyList[0].Length]; } resultList.Add(fvalue); } if (channelDataList.ContainsKey(bnyPath)) { channelDataList[bnyPath].Enqueue(new List <float[]>(resultList)); } if (tokenSource.IsCancellationRequested) { token.ThrowIfCancellationRequested(); break; } int count = channelDataList[bnyPath].Count / 10; if (count > 1) { Thread.Sleep(300 * count); } } channelDataList[bnyPath].Enqueue(null); }
/// <summary> /// 获取BNY数据并向cit文件中写入数据 /// </summary> /// <param name="channelList"></param> private void GetBnyDataAndWriteCit(List <ChannelNew> channelList) { int[] bnyChannelArr = channelList.Where(s => s.BNYChannelId >= 0).Select(s => s.BNYChannelId).ToArray(); int sampleNum = 1000; long startPosition = 0; long endPosition = 0; long bnyFileLength = common.GetBNYLastPosition(bnyFilePath); int pageCount = Convert.ToInt32(bnyFileLength / (sampleNum * BNYFile.GetChannelSize())); List <float[]> bnyList = new List <float[]>(); try { using (FileStream fs = new FileStream(createCitFilePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite)) { using (BinaryWriter bw = new BinaryWriter(fs, Encoding.Default)) { DataHeadInfo dhi = citHelper.GetDataInfoHead(createCitFilePath); List <DataChannelInfo> channelInfoList = citHelper.GetDataChannelInfoHead(createCitFilePath); int iChannelNumberSize = dhi.iChannelNumber * 2; byte[] dataArray = new byte[iChannelNumberSize]; List <Byte> dataList = new List <Byte>(); short tmpRmsData = 0;; Byte[] tmpBytes = new Byte[2]; for (int i = 0; i < pageCount; i++) { bnyList = common.GetBNYData(bnyFilePath, sampleNum, startPosition, ref endPosition); startPosition = endPosition; List <float[]> resultList = new List <float[]>(); //Action<float> action = new Action<float>({ }); for (int j = 0; j < channelList.Count; j++) { float[] fvalue = new float[sampleNum]; if (channelList[j].BNYChannelId >= 0) { fvalue = bnyList[channelList[j].BNYChannelId]; } resultList.Add(fvalue); } long iArrayLen = resultList[0].Length; for (int k = 0; k < iArrayLen; k++) { if (citHelper.IsEncrypt(dhi)) { for (int iTmp = 0; iTmp < channelList.Count; iTmp++) { if (channelList[iTmp].BNYChannelId > 6) { tmpRmsData = (short)((resultList[iTmp][k] / 10 - channelInfoList[iTmp].fOffset) * channelInfoList[iTmp].fScale); } else { tmpRmsData = (short)((resultList[iTmp][k] - channelInfoList[iTmp].fOffset) * channelInfoList[iTmp].fScale); } tmpBytes = CitFileHelper.ByteXORByte(BitConverter.GetBytes(tmpRmsData)); dataList.AddRange(tmpBytes); } } else { for (int iTmp = 0; iTmp < channelList.Count; iTmp++) { try { if (channelList[iTmp].BNYChannelId > 6) { tmpRmsData = (short)((resultList[iTmp][k] / 10 - channelInfoList[iTmp].fOffset) * channelInfoList[iTmp].fScale); } else { tmpRmsData = (short)((resultList[iTmp][k] - channelInfoList[iTmp].fOffset) * channelInfoList[iTmp].fScale); } dataList.AddRange(BitConverter.GetBytes(tmpRmsData)); } catch (Exception ex) { throw ex; } } } ///bw.BaseStream.Position = 100; bw.Write(dataList.ToArray()); bw.Flush(); dataList.Clear(); } if (token.IsCancellationRequested) { token.ThrowIfCancellationRequested(); } } } } } catch (Exception ex) { throw ex; } }