/// <summary> /// 获取通道信息 /// </summary> /// <param name="bDataInfo"></param> /// <param name="start"></param> /// <returns></returns> private DataChannelInfo GetChannelInfo(byte[] bDataInfo, ref int start) { DataChannelInfo dci = new DataChannelInfo(); StringBuilder sNameEn = new StringBuilder(); StringBuilder sUnit = new StringBuilder(); int iNameEnLen = (int)bDataInfo[start]; for (int i = 1; i <= iNameEnLen; i++) { sNameEn.Append(UnicodeEncoding.Default.GetString(bDataInfo, i + start, 1)); } start += (1 + iNameEnLen); int iUnitLen = (int)bDataInfo[start]; for (int i = 1; i <= iUnitLen; i++) { sUnit.Append(UnicodeEncoding.Default.GetString(bDataInfo, i + start, 1)); } start += (1 + iUnitLen); dci.sNameEn = sNameEn.ToString(); dci.sUnit = sUnit.ToString(); dci.fScale = BitConverter.ToInt32(bDataInfo, start); start += 4; while (start < bDataInfo.Length && (bDataInfo[start] == (byte)0)) { start++; } return(dci); }
public string QueryDataChannelInfoHead(string sFile) { try { using (FileStream fs = new FileStream(sFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (BinaryReader br = new BinaryReader(fs, Encoding.Default)) { br.BaseStream.Position = 0; gfh = GetDataInfoHead(br.ReadBytes(GEO_FILE_HEADER_SIZE)); iChannelLength = BitConverter.ToInt16(br.ReadBytes(2), 0); iChannelCount = BitConverter.ToInt16(br.ReadBytes(2), 0); StringBuilder sbName = new StringBuilder(); dciL = new List <DataChannelInfo>(); byte[] bChannelData = br.ReadBytes(iChannelLength - 2); int iGeometryRecordId = BitConverter.ToInt16(br.ReadBytes(2), 0); //if (iGeometryRecordId != GEOMETRY_RECORD_ID) //{ // return "1,文件格式错误!"; //} for (int i = 0; i < iChannelLength - 2;) { DataChannelInfo dci = GetChannelInfo(bChannelData, ref i); dciL.Add(dci); sbName.Append(dci.sNameEn + ","); } sbName.Remove(sbName.Length - 1, 1); br.Close(); fs.Close(); return("0," + sbName.ToString()); } } } catch (Exception ex) { return("1,qc" + ex.Message); } }
/// <summary> /// /// </summary> /// <param name="sFile"></param> /// <returns></returns> public List <DataChannelInfo> GetDataChannel(string sFile) { using (FileStream fs = new FileStream(sFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (BinaryReader br = new BinaryReader(fs, Encoding.Default)) { br.BaseStream.Position = 0; GEO_FILE_HEADER gfh = GetDataInfoHead(br.ReadBytes(GEO_FILE_HEADER_SIZE)); //添加通道信息 iChannelLength = BitConverter.ToInt16(br.ReadBytes(2), 0); iChannelCount = BitConverter.ToInt16(br.ReadBytes(2), 0); StringBuilder sbName = new StringBuilder(); List <DataChannelInfo> dciL = new List <DataChannelInfo>(); byte[] bChannelData = br.ReadBytes(iChannelLength - 2); for (int j = 0; j < iChannelLength - 2;) { DataChannelInfo dci = GetChannelInfo(bChannelData, ref j); dciL.Add(dci); if (dciL.Count > iChannelCount) { break; } //if (iType == 10) //{ // if (dci.sNameEn.ToLower().Contains("null")) // { // break; // } //} } return(dciL); } } }
/// <summary> /// 将GEO文件转换为CIT文件 /// </summary> /// <param name="geoFile">GEO文件路径</param> /// <param name="citFile">CIT文件路径</param> /// <param name="citDataHeadInfo">cit头部信息</param> /// <returns>成功:true,失败:false</returns> public bool ConvertData(string geoFile, string citFile, FileInformation citDataHeadInfo) { try { int type = 1; //10号车通道对应表 if (citDataHeadInfo.sTrain.Contains("999307")) { type = 307;//iType=307时,geo转换有特殊情况 } int iPostion = 18; QueryDataChannelInfoHead(geoFile); string mileageRange = GetExportDataMileageRange(geoFile); mileageRange = mileageRange.Substring(2); float startMileage = float.Parse(mileageRange.Substring(0, mileageRange.IndexOf("-"))); float endMileage = float.Parse(mileageRange.Substring(mileageRange.IndexOf("-") + 1)); if (citDataHeadInfo.iKmInc == 0) { if (endMileage < startMileage) { float temp = startMileage; startMileage = endMileage; endMileage = temp; } } else { if (endMileage > startMileage) { float temp = startMileage; startMileage = endMileage; endMileage = temp; } } citDataHeadInfo.fkmFrom = startMileage; citDataHeadInfo.fkmTo = endMileage; using (FileStream fs = new FileStream(geoFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (BinaryReader br = new BinaryReader(fs, Encoding.Default)) { br.BaseStream.Position = 0; gfh = GetDataInfoHead(br.ReadBytes(GEO_FILE_HEADER_SIZE)); //添加通道信息 iChannelLength = BitConverter.ToInt16(br.ReadBytes(2), 0); iChannelCount = BitConverter.ToInt16(br.ReadBytes(2), 0); citDataHeadInfo.iChannelNumber = 2 + channelMappingList.Count; #region 通道定义 dciL = new List <DataChannelInfo>(); byte[] bChannelData = br.ReadBytes(iChannelLength - 2); //读取通道 for (int j = 0; j < iChannelLength - 2;) { DataChannelInfo dci = GetChannelInfo(bChannelData, ref j); dciL.Add(dci); if (dciL.Count > iChannelCount) { break; } if (type == 10) { if (dci.sNameEn.ToLower().Contains("null")) { break; } } } //通道匹配 int channelID = 2; List <int> listID = new List <int>(); List <ChannelDefinition> channelList = new List <ChannelDefinition>(); channelList.Add(new ChannelDefinition { sID = 1, sNameEn = "Km", sNameCh = "Km", fScale = 1.0f, fOffset = 0.0f, sUnit = "" }); channelList.Add(new ChannelDefinition { sID = 2, sNameEn = "Meter", sNameCh = "Meter", fScale = 4.0f, fOffset = 0.0f, sUnit = "" }); for (int i = 0; i < channelMappingList.Count; i++) { for (int j = 0; j < dciL.Count; j++) { if (dciL[j].sNameEn.Equals(channelMappingList[i].sGEO)) { listID.Add(j); ChannelDefinition channelInfoItem = new ChannelDefinition(); channelInfoItem.sID = channelID; channelInfoItem.sNameEn = channelMappingList[i].sCIT; channelInfoItem.sNameCh = channelMappingList[i].sChinese; channelInfoItem.fScale = dciL[j].fScale; channelInfoItem.fOffset = 0.0f; channelInfoItem.sUnit = dciL[j].sUnit; channelList.Add(channelInfoItem); ++channelID; break; } } } #endregion #region 通道数据 List <byte[]> bytes = GetChannelData(br, gfh, type, listID, iPostion); #endregion citHelper.WriteCitFile(citFile, citDataHeadInfo, channelList, "", bytes); br.Close(); } fs.Close(); } return(true); } catch (Exception ex) { throw new Exception(ex.Message); } }