// 批量数据主板下行 public String BuildQuery_Batch(string sid, ETrans trans, DateTime beginTime, EChannelType ctype) { try { //获取packageNum和recvtime CEntityPackage package = UpParser.cEntityPackage[sid]; String packageNum = package.PackageNum; DateTime recvtime = package.time; StringBuilder sb = new StringBuilder(); sb.Append(ProtocolMaps.ChannelProtocolStartCharMap.FindValue(ctype)); sb.Append(String.Format("{0:D4}", Int32.Parse(sid.Trim()))); sb.Append("0G"); sb.Append(" 29"); int dayRound = Int32.Parse(packageNum.Substring(0, 2)); int hourRound = Int32.Parse(packageNum.Substring(2, 2)); switch (trans) { case ETrans.ByHour: { int dayRoundTrans = dayRound + beginTime.Day - recvtime.Day; sb.Append(String.Format("{0:D2}", dayRoundTrans)); int hourRoundTrans = hourRound - recvtime.Hour + beginTime.Hour; sb.Append(String.Format("{0:D2}", hourRoundTrans)); } break; case ETrans.ByDay: { int dayRoundTrans = dayRound + beginTime.Day - recvtime.Day; dayRoundTrans = (dayRoundTrans <= 0) ? dayRoundTrans + 5 : dayRoundTrans; sb.Append((dayRoundTrans == dayRound) ? "00" : String.Format("{0:D2}", dayRoundTrans)); sb.Append("00"); } break; default: throw new Exception("传输格式错误"); } ///// 20190410-SYJ-Add //sb.Append(String.Format("{0:D2}", beginTime.Day)); //sb.Append(String.Format("{0:D2}", beginTime.Hour)); sb.Append('\r'); return(sb.ToString()); } catch (Exception e) { return("无对应站点包序号!"); } }
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); }
/// <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); }