/// <summary> /// 遥测站回应 /// $00011K01yymmddhhmm000Dhhmm000D#13 /// </summary> public bool Parse(String msg, out CBatchStruct batch) { batch = new CBatchStruct(); try { string data = string.Empty; if (!ProtocolHelpers.DeleteSpecialChar(msg, out data)) { return(false); } // 解析站点ID , 4位 0001 batch.StationID = data.Substring(0, 4); // 解析命令指令 ,2位 1K batch.Cmd = data.Substring(4, 2); // 解析站点类型, 2位 01 batch.StationType = ProtocolHelpers.ProtoStr2StationType(data.Substring(6, 2)); int year = Int32.Parse("20" + data.Substring(8, 2)); // 年 yy int month = Int32.Parse(data.Substring(10, 2)); // 月 mm int day = Int32.Parse(data.Substring(12, 2)); // 日 dd // 截取小时,分钟和数据段 // hhmm000Dhhmm000D var datas = new List <CTimeAndData>(); int segLength = 10; string dataSegs = data.Substring(14); while (dataSegs.Length >= segLength) { int hour = Int32.Parse(dataSegs.Substring(0, 2)); // 小时 hh int minute = Int32.Parse(dataSegs.Substring(2, 2)); // 分钟 mm String hexStr = dataSegs.Substring(4, 4); // 数据 000D,水位值要16转10,后2位没用 // int value = Int32.Parse(hexStr, System.Globalization.NumberStyles.HexNumber); string value = "22"; datas.Add(new CTimeAndData() { Time = new DateTime(year, month, day, hour, minute, 0), Data = value }); // 取剩下的小时,分钟和数据段 dataSegs = dataSegs.Substring(segLength); } batch.Datas = datas; return(true); } catch (Exception exp) { System.Diagnostics.Debug.WriteLine("数据:" + msg); System.Diagnostics.Debug.WriteLine("U盘批量传输解析数据不完整!" + exp.Message); } return(false); }
public bool Parse(String msg, out CReportStruct report) { //6013 $60131G2201161111040003046112271367 report = null; try { string data = string.Empty; if (!ProtocolHelpers.DeleteSpecialChar(msg, out data)) { return(false); } // 解析站点ID String stationID = data.Substring(0, 4); // 解析通信类别 String type = data.Substring(4, 2); // 解析报文类别 EMessageType reportType = ProtocolMaps.MessageTypeMap.FindKey(data.Substring(6, 2)); // 解析站点类别 //EStationType stationType = EStationType.EHydrology; //String stationTypeStr = data.Substring(8, 2); //switch (stationTypeStr) //{ // case "01": // 雨量 // stationType = EStationType.ERainFall; // break; // case "02": // 水位 // case "12": // stationType = EStationType.ERiverWater; // break; // case "03": // 水文 // case "13": // stationType = EStationType.EHydrology; // break; //} EStationType stationType = ProtocolHelpers.ProtoStr2StationType(data.Substring(8, 2)); // 解析接收时间 DateTime recvTime = DateTime.Now; // 获取数据段,不包含站号、类别、报类、站类信息 var lists = data.Substring(10).Split(CSpecialChars.BALNK_CHAR); var datas = GetData(lists, stationType); report = new CReportStruct() { Stationid = stationID, Type = type, ReportType = reportType, StationType = stationType, RecvTime = recvTime, Datas = datas }; return(true); } catch (Exception exp) { System.Diagnostics.Debug.WriteLine("数据:" + msg); System.Diagnostics.Debug.WriteLine("上行指令解析数据不完整!" + exp.Message); } return(false); }
public bool Parse_Old(string msg, out List <CUpReport> upReport) { upReport = new List <CUpReport>(); var msgSegs = msg.Split(CSpecialChars.BALNK_CHAR); foreach (var msgItem in msgSegs) { try { CUpReport report = null; string data = string.Empty; if (!ProtocolHelpers.DeleteSpecialChar(msgItem, out data)) { return(false); } // 解析站点ID String StationID = data.Substring(0, 4); // 解析通信类别 String type = data.Substring(4, 2); // 解析报文类别 EMessageType reportType = ProtocolMaps.MessageTypeMap.FindKey(data.Substring(6, 2)); // 解析站点类别 EStationType stationType = ProtocolHelpers.ProtoStr2StationType(data.Substring(8, 2)); // 解析接收时间 DateTime recvTime = new DateTime( year: Int32.Parse("20" + data.Substring(10, 2)), //年 month: Int32.Parse(data.Substring(12, 2)), //月 day: Int32.Parse(data.Substring(14, 2)), //日 hour: Int32.Parse(data.Substring(16, 2)), //时 minute: Int32.Parse(data.Substring(18, 2)), //分 second: 0 ); // 解析包序号 string packageNum = data.Substring(20, 2); // 解析数据 string item = data.Substring(22); CUpData upData = new CUpData(); while (item.Length > 2) { try { // 数据分为两部分 // 2 Byte 指令 + 剩下的为数据,数据的长度>= 1 // 解析指令类型param EOldParam param = OldTypeMaps.OldParamMap.FindKey(item.Substring(0, 2)); // 如果接收到的数据段长度大于2,表示对应的字段有值 // 默认为String.Empty string info = string.Empty; int length = Int32.Parse(OldTypeMaps.OldParamLengthMap.FindValue(param)); info = item.Substring(2); if (String.IsNullOrEmpty(info) && ProtocolHelpers.isLegalCharacters(info)) { continue; } switch (param) { case EOldParam.Rain: upData.Rain = Decimal.Parse(info.Substring(0, length)); break; case EOldParam.Water: upData.Water1 = Decimal.Parse(info.Substring(0, length)) * (Decimal)0.01; break; case EOldParam.WaterTimed: // 将水位定时报中的数据储存到list中 var waterList = new Dictionary <DateTime, Decimal>(); int roundNum = Int32.Parse(info.Substring(0, 2)); var watersegs = info; if (roundNum == 01 || roundNum == 02) { roundNum = roundNum == 01 ? 12 : 24; length = roundNum == 24 ? 96 : 48; watersegs = info.Substring(2); } else { roundNum = 12; } recvTime = recvTime.AddMinutes(-5 * roundNum); for (int i = 1; i <= roundNum; i++) { DateTime WaterTime = recvTime.AddMinutes(5); Decimal TimedWater = Decimal.Parse(watersegs.Substring(0, 4)) * (Decimal)0.01; waterList.Add(WaterTime, TimedWater); watersegs = watersegs.Substring(4); } upData.WaterList = waterList; break; case EOldParam.HalfVoltage: // 将电压值存入list中 var halfVoltageList = new Dictionary <DateTime, Decimal>(); DateTime HalfVoltageTime = recvTime; for (int i = 1; i <= 12; i++) { Decimal Voltage = Decimal.Parse(info.Substring(0, 4)) * (Decimal)0.01; HalfVoltageTime = recvTime.AddHours(-1 * (i - 1)); halfVoltageList.Add(HalfVoltageTime, Voltage); info = info.Substring(4); } upData.VoltageList = halfVoltageList; break; case EOldParam.Voltage: upData.Voltage = (Decimal.Parse(info.Substring(0, length)) * (Decimal)0.01); break; case EOldParam.AllVoltage: // 将电压值存入list中 var allVoltageList = new Dictionary <DateTime, Decimal>(); DateTime AllVoltageTime = recvTime; for (int i = 1; i <= 24; i++) { Decimal Voltage = Decimal.Parse(info.Substring(0, 4)) * (Decimal)0.01; AllVoltageTime = recvTime.AddHours(-1 * (i - 1)); allVoltageList.Add(AllVoltageTime, Voltage); info = info.Substring(4); } upData.VoltageList = allVoltageList; break; case EOldParam.Interval: int Interval = Int32.Parse(info.Substring(0, length)); break; case EOldParam.ESaveNum: int ESaveNum = Int32.Parse(info.Substring(0, length)); break; case EOldParam.ETimedNum: int ETimedNum = Int32.Parse(info.Substring(0, length)); break; case EOldParam.WaterBase: decimal WaterBase = Decimal.Parse(info.Substring(0, length)) * (Decimal)0.01; break; case EOldParam.MannualWater: DateTime MannualWaterTime = new DateTime( year: Int32.Parse(info.Substring(0, 2)), month: Int32.Parse(info.Substring(2, 2)), day: Int32.Parse(info.Substring(4, 2)), hour: Int32.Parse(data.Substring(6, 2)), minute: Int32.Parse(data.Substring(8, 2)), second: 0 ); recvTime = MannualWaterTime; Decimal MannualWater = Decimal.Parse(info.Substring(10, 6)) * (Decimal)0.01; upData.WaterList.Add(MannualWaterTime, MannualWater); break; case EOldParam.WaterAddRange: Decimal WaterAddRange = Decimal.Parse(info.Substring(0, length)); break; case EOldParam.RainAddRange: Decimal RainAddRange = Decimal.Parse(info.Substring(0, length)); break; case EOldParam.DestPhoneNum: string DestPhoneNum = info.Substring(0, length); break; case EOldParam.TerminalNum: string TerminalNum = info.Substring(0, length); break; case EOldParam.RespWave: string RespWave = info.Substring(0, length); break; case EOldParam.WavePost: int WavePost = Int32.Parse(info.Substring(0, length)); break; case EOldParam.WorkStatus: EWorkStatus WorkStatus = OldTypeMaps.WorkStatus4ProtoMap.FindKey(info.Substring(0, length)); break; case EOldParam.StandbyChannel: ChannelType MainChannel = OldTypeMaps.ChannelType4ProtoMap.FindKey(info.Substring(0, 2)); System.Diagnostics.Debug.Assert(MainChannel != ChannelType.None, "主用信道不能为NONE"); ChannelType ViceChannel = OldTypeMaps.ChannelType4ProtoMap.FindKey(info.Substring(2, 2)); break; case EOldParam.GSMSignal: Decimal GSMSignal = Decimal.Parse(info.Substring(0, length)); break; case EOldParam.MannualFlow: DateTime MannualFlowTime = new DateTime( year: Int32.Parse(info.Substring(0, 2)), month: Int32.Parse(info.Substring(2, 2)), day: Int32.Parse(info.Substring(4, 2)), hour: Int32.Parse(data.Substring(6, 2)), minute: Int32.Parse(data.Substring(8, 2)), second: 0 ); recvTime = MannualFlowTime; upData.WaterFlow = Decimal.Parse(info.Substring(10, 6)) * (Decimal)0.01; break; case EOldParam.GSMElec: EGSMElec GSMElec = OldTypeMaps.GSMElec4ProtoMap.FindKey(info.Substring(0, length)); break; case EOldParam.WaterTemp: upData.WaterTemp = Decimal.Parse(info.Substring(0, length)) * (Decimal)0.1; break; case EOldParam.DataPackNum: int dataPackNum = Int32.Parse(info.Substring(0, length)); break; default: break; } item = item.Substring(2 + length); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("数据:" + msg); System.Diagnostics.Debug.WriteLine("下行指令解析数据不完整!" + e.Message); break; } //填充基本数据 report = new CUpReport() { Stationid = StationID, Type = type, ReportType = reportType, StationType = stationType, RecvTime = recvTime, PackageNum = packageNum, Data = upData }; upReport.Add(report); } } catch (Exception exp) { System.Diagnostics.Debug.WriteLine(exp.Message); continue; } } return(true); }
public bool Parse_New(string msg, out List <CUpReport> upReport) { upReport = new List <CUpReport>(); CUpReport report = null; var msgSegs = msg.Split(CSpecialChars.BALNK_CHAR); foreach (var msgItem in msgSegs) { try { string data = string.Empty; if (!ProtocolHelpers.DeleteSpecialChar(msgItem, out data)) { return(false); } // 解析站点ID String stationID = data.Substring(0, 4); // 解析通信类别 String type = data.Substring(4, 2); // 解析报文类别 EMessageType reportType = ProtocolMaps.MessageTypeMap.FindKey(data.Substring(6, 2)); // 解析站点类别 EStationType stationType = ProtocolHelpers.ProtoStr2StationType(data.Substring(8, 2)); // 解析包序号 string packageNum = data.Substring(10, 4); // 解析接收时间 DateTime recvTime = new DateTime( year: Int32.Parse("20" + data.Substring(14, 2)), //年 month: Int32.Parse(data.Substring(16, 2)), //月 day: Int32.Parse(data.Substring(18, 2)), //日 hour: Int32.Parse(data.Substring(20, 2)), //时 minute: Int32.Parse(data.Substring(22, 2)), //分 second: 0 ); // 解析数据 CUpData upData = new CUpData(); string item = data.Substring(24); while (item.Length > 2) { try { // 数据分为两部分 // 2 Byte 指令 + 剩下的为数据,数据的长度>= 1 // 解析指令类型param ENewParam param = NewTypeMaps.NewParamMap.FindKey(item.Substring(0, 2)); // 如果接收到的数据段长度大于2,表示对应的字段有值 // 默认为String.Empty string info = string.Empty; int length = Int32.Parse(NewTypeMaps.NewParamLengthMap.FindValue(param)); info = item.Substring(2, length); item = item.Substring(2 + length); if (String.IsNullOrEmpty(info) && ProtocolHelpers.isLegalCharacters(info)) { continue; } switch (param) { case ENewParam.Rain: upData.Rain = Decimal.Parse(info); break; case ENewParam.Water1: upData.Water1 = Decimal.Parse(info) * (Decimal)0.01; break; case ENewParam.Water2: upData.Water2 = Decimal.Parse(info) * (Decimal)0.01; break; case ENewParam.Clock: int year = Int32.Parse("20" + info.Substring(0, 2)); int month = Int32.Parse(info.Substring(2, 2)); int day = Int32.Parse(info.Substring(4, 2)); int hour = Int32.Parse(info.Substring(6, 2)); int minute = Int32.Parse(info.Substring(8, 2)); int second = Int32.Parse(info.Substring(10, 2)); upData.Time = new DateTime(year, month, day, hour, minute, second); break; case ENewParam.Voltage: upData.Voltage = (Decimal.Parse(info) * (Decimal)0.01); break; case ENewParam.WaterInterval: int WaterInterval = Int32.Parse(info); break; case ENewParam.RainInterval: int RainInterval = Int32.Parse(info); break; case ENewParam.ETimedNum: int ETimedNum = Int32.Parse(info); break; case ENewParam.ESaveNum: int ESaveNum = Int32.Parse(info); break; case ENewParam.WaterBase1: Decimal WaterBase1 = Decimal.Parse(info) * (Decimal)0.01; break; case ENewParam.WaterBase2: Decimal WaterBase2 = Decimal.Parse(info) * (Decimal)0.01; break; case ENewParam.MannualWater: DateTime MannualWaterTime = new DateTime( year: Int32.Parse(info.Substring(0, 2)), month: Int32.Parse(info.Substring(2, 2)), day: Int32.Parse(info.Substring(4, 2)), hour: Int32.Parse(data.Substring(6, 2)), minute: Int32.Parse(data.Substring(8, 2)), second: 0 ); Decimal MannualWater = Decimal.Parse(info.Substring(10, 6)) * (Decimal)0.01; upData.WaterList.Add(MannualWaterTime, MannualWater); break; case ENewParam.WaterAddRange: Decimal WaterAddRange = Decimal.Parse(info); break; case ENewParam.RainAddRange: Decimal RainAddRange = Decimal.Parse(info); break; case ENewParam.DestPhoneNum1: string DestPhoneNum1 = info; break; case ENewParam.DestPhoneNum2: string DestPhoneNum2 = info; break; case ENewParam.TerminalNum: string TerminalNum = info; break; case ENewParam.RespWave: string RespWave = info; break; case ENewParam.WavePost: int WavePost = Int32.Parse(info); break; case ENewParam.WorkStatus: EWorkStatus WorkStatus = OldTypeMaps.WorkStatus4ProtoMap.FindKey(info); break; case ENewParam.StandbyChannel: ChannelType MainChannel = OldTypeMaps.ChannelType4ProtoMap.FindKey(info); System.Diagnostics.Debug.Assert(MainChannel != ChannelType.None, "主用信道不能为NONE"); ChannelType ViceChannel = OldTypeMaps.ChannelType4ProtoMap.FindKey(info.Substring(2, 2)); break; case ENewParam.GSMSignal: Decimal GSMSignal = Decimal.Parse(info); break; case ENewParam.MannualFlow: DateTime MannualFlowTime = new DateTime( year: Int32.Parse(info.Substring(0, 2)), month: Int32.Parse(info.Substring(2, 2)), day: Int32.Parse(info.Substring(4, 2)), hour: Int32.Parse(data.Substring(6, 2)), minute: Int32.Parse(data.Substring(8, 2)), second: 0 ); upData.Time = MannualFlowTime; upData.WaterFlow = Decimal.Parse(info.Substring(10, 6)) * (Decimal)0.01; break; case ENewParam.GSMElec: EGSMElec GSMElec = OldTypeMaps.GSMElec4ProtoMap.FindKey(info); break; case ENewParam.WaterTemp: upData.WaterTemp = Decimal.Parse(info) * (Decimal)0.1; break; case ENewParam.FourWaterTemp: var waterTempList = new Dictionary <DateTime, Decimal>(); DateTime WaterTempTime = recvTime; for (int i = 1; i <= 4; i++) { Decimal WaterTemp = Decimal.Parse(info.Substring(0, 4)) * (Decimal)0.1; WaterTempTime = recvTime.AddHours(-6 * (i - 1)); info = info.Substring(4); waterTempList.Add(WaterTempTime, WaterTemp); } upData.TempList = waterTempList; break; case ENewParam.DataPackNum: int DataPackNum = Int32.Parse(info); break; default: break; } } catch (Exception e) { System.Diagnostics.Debug.WriteLine("数据:" + msg); System.Diagnostics.Debug.WriteLine("下行指令解析数据不完整!" + e.Message); break; } //填充基本数据 report = new CUpReport() { Stationid = stationID, Type = type, ReportType = reportType, StationType = stationType, RecvTime = recvTime, PackageNum = packageNum, Data = upData }; upReport.Add(report); } } catch (Exception exp) { System.Diagnostics.Debug.WriteLine(exp.Message); continue; } } return(true); }
public bool Parse(String msg, out CReportStruct report) { //$30011G2512010030yymmddhhmm1297001100001100001100001100001100001100001100001100001100001100001100001100001100001100001100001100001100001100001100001100001100001100001100001100001100001100001100001100001100001100; report = null; try { string data = string.Empty; if (!ProtocolHelpers.DeleteSpecialChar(msg, out data)) { return(false); } // 解析站号 String StationId = data.Substring(0, 4); // 解析通信类别 String type = data.Substring(4, 2); //定义报文类别 String reportTypeString = data.Substring(6, 2); //定义站点类别 String stationTypeString = data.Substring(8, 2); // 根据报文类别进行区别处理 switch (reportTypeString) { case "25": { // 解析报文类别 reportTypeString = "22"; EMessageType reportType = ProtocolMaps.MessageTypeMap.FindKey(reportTypeString); // 解析站点类别 EStationType stationType = ProtocolHelpers.ProtoStr2StationType(stationTypeString); //解析间隔分钟 int intervalMin = Int32.Parse(data.Substring(10, 2)); //解析数据个数 int dataNum = Int32.Parse(data.Substring(12, 4)); //解析接收时间 DateTime recvTime = new DateTime( year: Int32.Parse("20" + data.Substring(16, 2)), //年 month: Int32.Parse(data.Substring(18, 2)), //月 day: Int32.Parse(data.Substring(20, 2)), //日 hour: Int32.Parse(data.Substring(22, 2)), //时 minute: Int32.Parse(data.Substring(24, 2)), //分 second: 0 ); //解析电压 2(整数位)+ 2(小数位) 单位 V Decimal Voltage = Decimal.Parse(data.Substring(26, 4)) * (Decimal)0.01; //获取数据段,不包括间隔分钟、数据个数、电压等 var lists = data.Substring(30).Split(CSpecialChars.BALNK_CHAR); var datas = GetProData(intervalMin, dataNum, lists, recvTime, Voltage); report = new CReportStruct() { Stationid = StationId, Type = type, ReportType = reportType, StationType = stationType, RecvTime = recvTime, Datas = datas }; } break; case "22": { // 解析报文类别 EMessageType reportType = ProtocolMaps.MessageTypeMap.FindKey(reportTypeString); // 解析站点类别 EStationType stationType = ProtocolHelpers.ProtoStr2StationType(stationTypeString); //解析包序号 String PackageNum = data.Substring(10, 4); //解析接收时间 DateTime recvTime = new DateTime( year: Int32.Parse("20" + data.Substring(14, 2)), //年 month: Int32.Parse(data.Substring(16, 2)), //月 day: Int32.Parse(data.Substring(18, 2)), //日 hour: Int32.Parse(data.Substring(20, 2)), //时 minute: 0, //分 second: 0 //秒 ); //把包序号写入缓存 CEntityPackage package = new CEntityPackage() { StrStationID = StationId, PackageNum = PackageNum, time = recvTime }; if (cEntityPackage.ContainsKey(StationId)) { cEntityPackage[StationId] = package; } else { cEntityPackage.Add(StationId, package); } //解析电压 2(整数位)+ 2(小数位) 单位 V Decimal Voltage = Decimal.Parse(data.Substring(22, 4)) * (Decimal)0.01; //获取数据段,不包括间隔分钟、数据个数、电压等 var lists = data.Substring(26).Split(CSpecialChars.BALNK_CHAR); var datas = GetData(lists, recvTime, Voltage, stationType); report = new CReportStruct() { Stationid = StationId, Type = type, ReportType = reportType, StationType = stationType, RecvTime = recvTime, Datas = datas }; } break; case "21": { if (data.Substring(8, 2) != "11") { // 解析报文类别 EMessageType reportType = ProtocolMaps.MessageTypeMap.FindKey(reportTypeString); // 解析站点类别 EStationType stationType = ProtocolHelpers.ProtoStr2StationType(stationTypeString); // 解析接收时间 DateTime recvTime = new DateTime( year: Int32.Parse("20" + data.Substring(10, 2)), //年 month: Int32.Parse(data.Substring(12, 2)), //月 day: Int32.Parse(data.Substring(14, 2)), //日 hour: Int32.Parse(data.Substring(16, 2)), //时 minute: Int32.Parse(data.Substring(18, 2)), //分 second: 0 //秒 ); var lists = data.Substring(20).Split(CSpecialChars.BALNK_CHAR); var datas = GetAddData(lists, recvTime, stationType); report = new CReportStruct() { Stationid = StationId, Type = type, ReportType = reportType, StationType = stationType, RecvTime = recvTime, Datas = datas }; } else { //1G2111为人工水位 // 解析报文类别 EMessageType reportType = EMessageType.EMannual; // 解析站点类别 EStationType stationType = EStationType.ERiverWater; // 解析接收时间 DateTime recvTime = new DateTime( year: DateTime.Now.Year, //年 month: DateTime.Now.Month, //月 day: DateTime.Now.Day, //日 hour: Int32.Parse(data.Substring(10, 2)), //时 minute: Int32.Parse(data.Substring(12, 2)), //分 second: 0 //秒 ); var lists = data.Substring(14).Split(CSpecialChars.BALNK_CHAR); var datas = GetMannualData(lists, recvTime); report = new CReportStruct() { Stationid = StationId, Type = type, ReportType = reportType, StationType = stationType, RecvTime = recvTime, Datas = datas }; } } break; case "23": { //1G23为人工流量 // 解析报文类别 EMessageType reportType = EMessageType.EMannual; // 解析站点类别 EStationType stationType = EStationType.ERiverWater; // 解析接收时间 DateTime recvTime = new DateTime( year: DateTime.Now.Year, //年 month: DateTime.Now.Month, //月 day: Int32.Parse(data.Substring(8, 2)), //日 hour: Int32.Parse(data.Substring(10, 2)), //时 minute: Int32.Parse(data.Substring(12, 2)), //分 second: 0 //秒 ); var lists = data.Substring(14).Split(CSpecialChars.BALNK_CHAR); var datas = GetWaterData(lists, recvTime); report = new CReportStruct() { Stationid = StationId, Type = type, ReportType = reportType, StationType = stationType, RecvTime = recvTime, Datas = datas }; } break; case "29": { string doubleMsg = data.Substring(8); Parse(doubleMsg, out report); } break; } return(true); } catch (Exception exp) { System.Diagnostics.Debug.WriteLine("数据:" + msg); System.Diagnostics.Debug.WriteLine("数据协议解析不完整" + exp.Message); } return(false); }
public bool Parse(string msg, out CDownConf downConf) { downConf = new CDownConf(); try { string data = string.Empty; if (!ProtocolHelpers.DeleteSpecialChar(msg, out data)) { return(false); } // 解析站点ID String stationID = data.Substring(0, 4); // 解析通信类别 String type = data.Substring(4, 2); data = data.Substring(6); var segs = data.Split(CSpecialChars.BALNK_CHAR); foreach (var item in segs) { try { // 数据分为两部分 // 2 Byte 指令 + 剩下的为数据,数据的长度>= 2 // 解析指令类型param EDownParam param = ProtocolMaps.DownParamMap.FindKey(item.Substring(0, 2)); // 如果接收到的数据段长度大于2,表示对应的字段有值 // 默认为String.Empty string info = string.Empty; if (item.Length > 2) { // "辽宁系统协议.doc"文档中规定的字段长度 int length = Int32.Parse(ProtocolMaps.DownParamLengthMap.FindValue(param)); // 如果接收到的数据段的长度不等于规定长度,或者规定长度为-1(即长度不定) // 截取剩下的所有字符串 // 否则,截取规定长度的字符串 info = (item.Length - 2 != length || length == -1) ? item.Substring(2) : item.Substring(2, length); } if (String.IsNullOrEmpty(info)) { continue; } switch (param) { case EDownParam.Clock: int year = Int32.Parse("20" + info.Substring(0, 2)); int month = Int32.Parse(info.Substring(2, 2)); int day = Int32.Parse(info.Substring(4, 2)); int hour = Int32.Parse(info.Substring(6, 2)); int minute = Int32.Parse(info.Substring(8, 2)); int second = Int32.Parse(info.Substring(10, 2)); downConf.Clock = new DateTime(year, month, day, hour, minute, second); break; case EDownParam.NormalState: downConf.NormalState = ProtocolMaps.NormalState4ProtoMap.FindKey(info); break; case EDownParam.Voltage: downConf.Voltage = (Decimal.Parse(info) * (Decimal)0.01); break; case EDownParam.StationCmdID: downConf.StationCmdID = info; break; case EDownParam.TimeChoice: downConf.TimeChoice = ProtocolMaps.TimeChoice4ProtoMap.FindKey(info); break; case EDownParam.TimePeriod: downConf.TimePeriod = ProtocolMaps.TimePeriodMap.FindKey(info); break; case EDownParam.WorkStatus: downConf.WorkStatus = ProtocolMaps.WorkStatus4ProtoMap.FindKey(info); break; case EDownParam.VersionNum: downConf.VersionNum = info; break; case EDownParam.StandbyChannel: downConf.MainChannel = ProtocolMaps.ChannelType4ProtoMap.FindKey(info.Substring(0, 2)); System.Diagnostics.Debug.Assert(downConf.MainChannel != EChannelType.None, "主用信道不能为NONE"); downConf.ViceChannel = ProtocolMaps.ChannelType4ProtoMap.FindKey(info.Substring(2, 2)); break; case EDownParam.TeleNum: downConf.TeleNum = info; break; case EDownParam.RingsNum: downConf.RingsNum = Decimal.Parse(info); break; case EDownParam.DestPhoneNum: downConf.DestPhoneNum = info; break; case EDownParam.TerminalNum: downConf.TerminalNum = info; break; case EDownParam.RespBeam: downConf.RespBeam = info; break; case EDownParam.AvegTime: downConf.AvegTime = Decimal.Parse(info); break; case EDownParam.RainPlusReportedValue: downConf.RainPlusReportedValue = Decimal.Parse(info); break; case EDownParam.KC: downConf.KC = info; break; case EDownParam.Rain: downConf.Rain = Decimal.Parse(info); break; // 单位为米 case EDownParam.Water: downConf.Water = (Decimal.Parse(info) * (Decimal)0.01); break; // 默认单位为厘米 case EDownParam.Water: downConf.StorageWater = Decimal.Parse(info); break; case EDownParam.WaterPlusReportedValue: downConf.WaterPlusReportedValue = Decimal.Parse(info); break; case EDownParam.SelectCollectionParagraphs: downConf.SelectCollectionParagraphs = ProtocolMaps.SelectCollectionParagraphs4ProtoMap.FindKey(info); break; case EDownParam.StationType: var stype = ProtocolHelpers.ProtoStr2StationType(info); downConf.StationType = stype; break; case EDownParam.UserName: downConf.UserName = info; break; case EDownParam.StationName: downConf.StationName = info; break; default: break; } } catch (Exception e) { System.Diagnostics.Debug.WriteLine("数据:" + msg); System.Diagnostics.Debug.WriteLine("下行指令解析数据不完整!" + e.Message); } } return(true); } catch (Exception exp) { System.Diagnostics.Debug.WriteLine(exp.Message); } return(false); }
/// <summary> /// 非卫星报文数据解析过程 /// </summary> /// <param name="msg">原始报文数据</param> /// <param name="report">报文最终解析出的结果数据结构</param> /// <returns>是否解析成功</returns> public bool Parse(String msg, out CReportStruct report) { //$30151G22010201120326001297065535323906553532390655353239065535323906553532390655353239065535323906553532390655353239065535323906553532390655353239 report = null; try { if (msg == "") { return(false); } string data = string.Empty; //去除起始符'$' if (!ProtocolHelpers.DeleteSpecialChar(msg, out data)) { return(false); } //站号(4位) string StationId = data.Substring(0, 4); //类别(2位):1G string type = data.Substring(4, 2); //报类(2位):22-定时报 string reportTypeString = data.Substring(6, 2); //站类(2位) string stationTypeString = data.Substring(8, 2); EMessageType reportType; EStationType stationType; string packageNum; DateTime recvTime; Decimal Voltage; string lists; List <CReportData> datas; ///0201120326001297065535323906553532390655353239065535323906553532390655353239065535323906553532390655353239065535323906553532390655353239 //站类区别处理 switch (reportTypeString) { //定时报新增48段次定时报类 case "25": //获取报类 reportTypeString = "22"; reportType = ProtocolMaps.MessageTypeMap.FindKey(reportTypeString); //获取站类 stationType = ProtocolHelpers.ProtoStr2StationType(stationTypeString); //包序号暂不处理 packageNum = data.Substring(10, 4); //接收时间 recvTime = new DateTime( year: Int32.Parse("20" + data.Substring(14, 2)), month: Int32.Parse(data.Substring(16, 2)), day: Int32.Parse(data.Substring(18, 2)), hour: Int32.Parse(data.Substring(20, 2)), minute: Int32.Parse(data.Substring(22, 2)), second: 0 ); //电压值:1297=12.97V Voltage = Decimal.Parse(data.Substring(24, 4)) * (Decimal)0.01; //数据段 lists = data.Substring(28).Replace(" ", ""); datas = GetData_1(lists, recvTime, Voltage, stationType); report = new CReportStruct() { Stationid = StationId, Type = type, ReportType = reportType, StationType = stationType, RecvTime = DateTime.Now, Datas = datas }; break; //定时报 case "22": //获取报类 reportType = ProtocolMaps.MessageTypeMap.FindKey(reportTypeString); //获取站类 stationType = ProtocolHelpers.ProtoStr2StationType(stationTypeString); //包序号 packageNum = data.Substring(10, 4); //接收时间 recvTime = new DateTime( year: Int32.Parse("20" + data.Substring(14, 2)), month: Int32.Parse(data.Substring(16, 2)), day: Int32.Parse(data.Substring(18, 2)), hour: Int32.Parse(data.Substring(20, 2)), minute: 0, second: 0 ); //把包序号写入缓存 CEntityPackage package = new CEntityPackage() { StrStationID = StationId, PackageNum = packageNum, time = recvTime }; if (cEntityPackage.ContainsKey(StationId)) { cEntityPackage[StationId] = package; } else { cEntityPackage.Add(StationId, package); } //电压值:1297=12.97V Voltage = Decimal.Parse(data.Substring(22, 4)) * (Decimal)0.01; //数据段 lists = data.Substring(26).Replace(" ", ""); datas = GetData(lists, recvTime, Voltage, stationType); report = new CReportStruct() { Stationid = StationId, Type = type, ReportType = reportType, StationType = stationType, RecvTime = DateTime.Now, Datas = datas }; break; //报讯系统加报 case "21": { if (data.Substring(8, 2) != "11") { // 解析报文类别 reportType = ProtocolMaps.MessageTypeMap.FindKey(reportTypeString); // 解析站点类别 stationType = ProtocolHelpers.ProtoStr2StationType(stationTypeString); // 解析接收时间 recvTime = new DateTime( year: Int32.Parse("20" + data.Substring(10, 2)), //年 month: Int32.Parse(data.Substring(12, 2)), //月 day: Int32.Parse(data.Substring(14, 2)), //日 hour: Int32.Parse(data.Substring(16, 2)), //时 minute: Int32.Parse(data.Substring(18, 2)), //分 second: 0 //秒 ); var dataLists = data.Substring(20).Split(CSpecialChars.BALNK_CHAR); datas = GetAddData(dataLists, recvTime, stationType); report = new CReportStruct() { Stationid = StationId, Type = type, ReportType = reportType, StationType = stationType, RecvTime = DateTime.Now, Datas = datas }; } break; } } return(true); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("数据:" + msg); System.Diagnostics.Debug.WriteLine("中游局协议解析不完整" + e.Message); //return false; } return(false); }
//批量传输Flash解析 public bool Parse_Flash(String msg, out CBatchStruct batch) { batch = new CBatchStruct(); try { string data = string.Empty; if (!ProtocolHelpers.DeleteSpecialChar(msg, out data)) { return(false); } Debug.WriteLine(msg); // 解析站点ID , 4位 0001 batch.StationID = data.Substring(0, 4); // 解析命令指令 ,2位 1K batch.Cmd = data.Substring(4, 2); // 解析站点类型, 2位 01 batch.StationType = ProtocolHelpers.ProtoStr2StationType(data.Substring(6, 2)); // 解析传输类型, 2位 03 batch.TransType = ProtocolMaps.TransMap.FindKey(data.Substring(8, 2)); var datas = new List <CTimeAndData>(); // 按小时传 int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0; int segLength = 0; string dataSegs = string.Empty; switch (batch.TransType) { case ETrans.ByHour: year = Int32.Parse("20" + data.Substring(10, 2)); // 年 yy month = Int32.Parse(data.Substring(12, 2)); // 月 mm // 截取日、小时、分钟和数据段 ,以\r结尾 // ddhhmm123456ddhhmm123456ddhhmm123456ddhhmm123456ddhhmm123456ddhhmm123456ddhhmm123456ddhhmm123456ddhhmm123456ddhhmm123456ddhhmm123456ddhhmm123456#13 dataSegs = data.Substring(14); segLength = 12; while (dataSegs.Length >= segLength) { // 读取前8位数据 day = Int32.Parse(dataSegs.Substring(0, 2)); // 日 dd hour = Int32.Parse(dataSegs.Substring(2, 2)); // 小时 hh minute = Int32.Parse(dataSegs.Substring(4, 2)); // 分钟 mm string value = dataSegs.Substring(6, 6); datas.Add(new CTimeAndData() { Time = new DateTime(year, month, day, hour, minute, 0), Data = value }); // 取剩下的分钟和数据段 dataSegs = dataSegs.Substring(segLength); } break; case ETrans.ByDay: // 按天传 year = Int32.Parse("20" + data.Substring(10, 2)); // 年 yy month = Int32.Parse(data.Substring(12, 2)); // 月 mm DateTime dt = new DateTime(); //day = Int32.Parse(data.Substring(14, 2)); // 日 dd //hour = Int32.Parse(data.Substring(16, 2)); // 小时 hh //DateTime dt = new DateTime(year, month, day, hour, 0, second); //// 截取分钟和数据段 //// mm123456mm123456mm123456mm123456mm123456mm123456mm123456mm123456mm123456mm123456mm123456mm123456#13 dataSegs = data.Substring(14); segLength = 12; // int previousMinute = 0; //bool isFirst = true; while (dataSegs.Length >= segLength) { day = Int32.Parse(dataSegs.Substring(0, 2)); // 日 dd hour = Int32.Parse(dataSegs.Substring(2, 2)); // 小时 hh minute = Int32.Parse(dataSegs.Substring(4, 2)); // 分钟 mm second = 0; // 第一段数据 //if (isFirst) //{ // dt = new DateTime(year, month, day, hour, minute, second); //} // 不是第一段数据 dt = new DateTime(year, month, day, hour, minute, second); //if (!isFirst) //{ // if (previousMinute >= minute) // { // dt = dt.AddHours(1); // //double addhour = 1; // //dt.AddHours(addhour); // } // dt = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, minute, dt.Second); //} //Int32 value = Int32.Parse(dataSegs.Substring(2, 6));// 数据 123456 string value = dataSegs.Substring(6, 6); datas.Add(new CTimeAndData() { Time = dt, Data = value }); Debug.WriteLine(string.Format("{0:D2} {1:D6} ------ {2} {3:D6}", minute, value, dt.ToString("yyyyMMdd HH:mm:ss"), value)); // 取剩下的日,小时,分钟和数据段 dataSegs = dataSegs.Substring(segLength); //previousMinute = minute; //isFirst = false; } break; } batch.Datas = datas; return(true); } catch (Exception exp) { System.Diagnostics.Debug.WriteLine("数据:" + msg); System.Diagnostics.Debug.WriteLine("Flash批量传输解析数据不完整!" + exp.Message); } return(false); }
/// <summary> /// 对下游局报讯报文进行处理 /// </summary> /// <param name="msg"></param> /// <param name="report"></param> /// <returns></returns> public bool Parse(String msg, out CReportStruct report) { //$30151G22010201120326001297065535323906553532390655353239065535323906553532390655353239065535323906553532390655353239065535323906553532390655353239 report = null; try { string data = string.Empty; //卫星信道报 if (msg.StartsWith("$")) { data = ProtocolHelpers.dealBCD(data); } //去除起始符'$' if (!ProtocolHelpers.DeleteSpecialChar(msg, out data)) { return(false); } //站号(4位) string StationId = data.Substring(0, 4); //类别(2位):1G string type = data.Substring(4, 2); //报类(2位):22-定时报 string reportTypeString = data.Substring(6, 2); //站类(2位) string stationTypeString = data.Substring(8, 2); ///0201120326001297065535323906553532390655353239065535323906553532390655353239065535323906553532390655353239065535323906553532390655353239 //站类区别处理 switch (reportTypeString) { //定时报 case "22": { //获取报类 EMessageType reportType = ProtocolMaps.MessageTypeMap.FindKey(reportTypeString); //获取站类 EStationType stationType = ProtocolHelpers.ProtoStr2StationType(stationTypeString); //包序号暂不处理 string packageNum = data.Substring(10, 4); //接收时间 DateTime recvTime = new DateTime( year: Int32.Parse("20" + data.Substring(14, 2)), month: Int32.Parse(data.Substring(16, 2)), day: Int32.Parse(data.Substring(18, 2)), hour: Int32.Parse(data.Substring(20, 2)), minute: 0, second: 0 ); //电压值:1297=12.97V Decimal voltage = Decimal.Parse(data.Substring(22, 4)) * (Decimal)0.01; //数据段 var lists = data.Substring(26).Replace(" ", ""); var datas = GetData(lists, recvTime, voltage, stationType); report = new CReportStruct() { Stationid = StationId, Type = type, ReportType = reportType, StationType = stationType, RecvTime = recvTime, Datas = datas }; break; } //报讯系统加报 case "21": { if (data.Substring(8, 2) != "11") { // 解析报文类别 EMessageType reportType = ProtocolMaps.MessageTypeMap.FindKey(reportTypeString); // 解析站点类别 EStationType stationType = ProtocolHelpers.ProtoStr2StationType(stationTypeString); // 解析接收时间 DateTime recvTime = new DateTime( year: Int32.Parse("20" + data.Substring(10, 2)), //年 month: Int32.Parse(data.Substring(12, 2)), //月 day: Int32.Parse(data.Substring(14, 2)), //日 hour: Int32.Parse(data.Substring(16, 2)), //时 minute: Int32.Parse(data.Substring(18, 2)), //分 second: 0 //秒 ); var lists = data.Substring(20).Split(CSpecialChars.BALNK_CHAR); var datas = GetAddData(lists, recvTime, stationType); report = new CReportStruct() { Stationid = StationId, Type = type, ReportType = reportType, StationType = stationType, RecvTime = recvTime, Datas = datas }; } else { //1G2111为人工水位 // 解析报文类别 EMessageType reportType = ProtocolMaps.MessageTypeMap.FindKey(reportTypeString); // 解析站点类别 EStationType stationType = EStationType.ERiverWater; // 解析接收时间 DateTime recvTime = new DateTime( year: DateTime.Now.Year, //年 month: DateTime.Now.Month, //月 day: DateTime.Now.Day, //日 hour: Int32.Parse(data.Substring(10, 2)), //时 minute: Int32.Parse(data.Substring(12, 2)), //分 second: 0 //秒 ); var lists = data.Substring(14).Split(CSpecialChars.BALNK_CHAR); var datas = GetMannualData(lists, recvTime); //处理datas为空情况 if (datas.Count == 0) { return(false); } report = new CReportStruct() { Stationid = StationId, Type = type, ReportType = reportType, StationType = stationType, RecvTime = recvTime, Datas = datas }; } break; } //人工报 case "23": { //1G23为人工流量 // 解析报文类别 EMessageType reportType = EMessageType.EMannual; // 解析站点类别 EStationType stationType = EStationType.ERiverWater; // 解析接收时间 DateTime recvTime = new DateTime( year: DateTime.Now.Year, //年 month: DateTime.Now.Month, //月 day: Int32.Parse(data.Substring(8, 2)), //日 hour: Int32.Parse(data.Substring(10, 2)), //时 minute: Int32.Parse(data.Substring(12, 2)), //分 second: 0 //秒 ); var lists = data.Substring(14).Split(CSpecialChars.BALNK_CHAR); var datas = GetWaterData(lists, recvTime); report = new CReportStruct() { Stationid = StationId, Type = type, ReportType = reportType, StationType = stationType, RecvTime = recvTime, Datas = datas }; break; } } return(true); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("数据:" + msg); System.Diagnostics.Debug.WriteLine("数据协议解析不完整" + e.Message); return(false); } }
// 批量数据flash下行解析 public bool Parse_Flash(String msg, EChannelType ctype, out CBatchStruct batch) { batch = new CBatchStruct(); try { string data = string.Empty; if (!ProtocolHelpers.DeleteSpecialChar(msg, out data)) { return(false); } // 解析站点ID , 4位 0001 batch.StationID = data.Substring(0, 4); // 解析命令指令 ,2位 1K batch.Cmd = data.Substring(4, 2); // 解析站点类型, 2位 01 batch.StationType = ProtocolHelpers.ProtoStr2StationType(data.Substring(6, 2)); // 解析传输类型, 2位 02/03 batch.TransType = ProtocolMaps.TransMap.FindKey(data.Substring(8, 2)); //创建返回的数据对象 var datas = new List <CTimeAndData>(); //ByDay ByHour区分 //02 按天传 03按小时 04暂时不需要处理 switch (batch.TransType) { //按天传 02 case ETrans.ByDay: { int dateYear = Int32.Parse("20" + data.Substring(10, 2)); // 年 int dateMonth = Int32.Parse(data.Substring(12, 2)); // 月 //数据处理 data = data.Substring(14); while (data.Length >= 12) { DateTime recvTime = new DateTime( year: dateYear, //年 month: dateMonth, //月 day: Int32.Parse(data.Substring(0, 2)), //日 hour: Int32.Parse(data.Substring(2, 2)), //时 minute: Int32.Parse(data.Substring(4, 2)), //分 second: 0 ); string datasegs = data.Substring(6, 6); datas.Add(new CTimeAndData { Time = recvTime, Data = datasegs }); data = data.Substring(12); } } break; //按小时传 03 case ETrans.ByHour: { //判断是GSM还是GPRS传送方式 //GSM:yymmddhh GPRS:yymm if (ctype == EChannelType.GPRS) { //GPRS类型 int dateYear = Int32.Parse("20" + data.Substring(10, 2)); // 年 int dateMonth = Int32.Parse(data.Substring(12, 2)); // 月 //数据处理 data = data.Substring(14); while (data.Length >= 12) { DateTime recvTime = new DateTime( year: dateYear, //年 month: dateMonth, //月 day: Int32.Parse(data.Substring(0, 2)), //日 hour: Int32.Parse(data.Substring(2, 2)), //时 minute: Int32.Parse(data.Substring(4, 2)), //分 second: 0 ); string datasegs = data.Substring(6, 6); datas.Add(new CTimeAndData { Time = recvTime, Data = datasegs }); data = data.Substring(12); } } else if (ctype == EChannelType.GSM) { //GSM类型 int dateYear = Int32.Parse("20" + data.Substring(10, 2)); // 年 int dateMonth = Int32.Parse(data.Substring(12, 2)); // 月 int dateDay = Int32.Parse(data.Substring(14, 2)); // 日 int dateHour = Int32.Parse(data.Substring(16, 2)); // 时 //数据处理 data = data.Substring(14); while (data.Length >= 12) { DateTime recvTime = new DateTime( year: dateYear, //年 month: dateMonth, //月 day: dateDay, //日 hour: dateHour, //时 minute: Int32.Parse(data.Substring(0, 2)), //分 second: 0 ); string datasegs = data.Substring(2, 6); datas.Add(new CTimeAndData { Time = recvTime, Data = datasegs }); data = data.Substring(8); } } } break; } batch.Datas = datas; return(true); } catch (Exception exp) { System.Diagnostics.Debug.WriteLine("数据:" + msg); System.Diagnostics.Debug.WriteLine("批量数据解析不完整" + exp.Message); } return(false); }
// 批量数据UBatch下行解析 public bool Parse(String msg, out CBatchStruct batch) { batch = new CBatchStruct(); try { string data = string.Empty; if (!ProtocolHelpers.DeleteSpecialChar(msg, out data)) { return(false); } // 解析站点ID , 4位 0001 batch.StationID = data.Substring(0, 4); // 解析命令指令 ,2位 1K batch.Cmd = data.Substring(4, 2); // 解析站点类型, 2位 01 batch.StationType = ProtocolHelpers.ProtoStr2StationType(data.Substring(6, 2)); int DataYear = Int32.Parse(data.Substring(8, 2)); int DataMonth = Int32.Parse(data.Substring(10, 2)); int DataDay = Int32.Parse(data.Substring(12, 2)); //创建返回的数据对象 var datas = new List <CTimeAndData>(); var lists = data.Substring(14).Split(CSpecialChars.BALNK_CHAR); foreach (var item in lists) { string itemData = item; while (itemData.Length > 0) { int DataHour = Int32.Parse(itemData.Substring(0, 2)); int DataMinute = Int32.Parse(itemData.Substring(2, 2)); // 解析出数据时间 DateTime recvTime = new DateTime( year: DataYear, month: DataMonth, day: DataDay, hour: DataHour, minute: DataMinute, second: 0 ); // 解析出16进制的数据 string itemDatas = itemData.Substring(4, 4); // 将16进制数据转化为10进制 itemDatas = int.Parse(itemDatas, System.Globalization.NumberStyles.AllowHexSpecifier).ToString(); // 创建CTimeAndData对象 CTimeAndData result = new CTimeAndData(); result.Time = recvTime; result.Data = itemDatas; // 将对像填充至list中 datas.Add(result); //构成循环 itemData = itemData.Substring(10); } } batch.Datas = datas; return(true); } catch (Exception exp) { System.Diagnostics.Debug.WriteLine("数据:" + msg); System.Diagnostics.Debug.WriteLine("批量数据解析不完整" + exp.Message); } return(false); }