/// <summary> /// 处理上行数据报文 /// </summary> /// <param name="updata"></param> private void parse_Updata(object updata) { //初始话 string datagram = updata as string; var gsm = new CGSMStruct(); if (!GsmHelper.Parse_2(datagram, out gsm)) { return; } IUp upParser = new UpParse(); //TODO 增加router,根据站点绑定调用协议 CReportStruct report = new CReportStruct(); string data = datagram.Substring(datagram.IndexOf("$")).Replace(";", ""); if (Up.Parse(data, out report)) /* 解析成功 */ { report.ChannelType = EChannelType.GSM; report.ListenPort = "COM" + this.ListenPort.PortName; report.flagId = gsm.PhoneNumber; if (this.UpDataReceived != null) { InvokeMessage(datagram, "[GSM]接收"); this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = datagram }); } } }
public CReportStruct Parse(String data) { // 判断是否是合法输入 Debug.Assert(data.StartsWith(CSpecialChars.StartCh.ToString()), "数据以非'$'字符开始"); Debug.Assert(data.EndsWith(CSpecialChars.EndCh.ToString()), "数据以非'\r'字符结束"); Debug.Assert(data.Length.Equals(36), ""); Debug.WriteLine(data); var result = new CReportStruct() { Sid = data.Substring(1, 4), // 解析站号 Type = data.Substring(5, 2), // 解析类别 RType = data.Substring(7, 2), // 解析报类 SType = data.Substring(9, 2), // 解析站类 RecvTime = DateTime.Now, // 接收时间 Datas = new List <CReportData>() // 实例化Datas属性 }; // 获取数据段,不包含站号、类别、报类、站类信息 var allData = data.Substring(11); allData = allData.Substring(0, allData.Length - 1); result.Datas.Add(UpHelper.GetData(allData, result.SType)); Debug.WriteLine("-------------------------"); return(result); }
// 北斗信道数据解析 public bool Parse_beidou(string sid, EMessageType type, string msg, out CReportStruct report) { // 这里stationId和type暂时不能获取,写成固定值。 //string stationId = "9999"; //string type = "1G"; string appendMsg = "$" + sid + type + ProtocolHelpers.dealBCD(msg) + CSpecialChars.ENTER_CHAR; return(Parse(msg, out report)); }
private void DealData() { while (true) { m_semaphoreData.WaitOne(); //阻塞当前线程,知道被其它线程唤醒 // 获取对data内存缓存的访问权 m_mutexListDatas.WaitOne(); List <HDModemDataStruct> dataListTmp = m_listDatas; m_listDatas = new List <HDModemDataStruct>(); //开辟一快新的缓存区 m_mutexListDatas.ReleaseMutex(); for (int i = 0; i < dataListTmp.Count; ++i) { try { HDModemDataStruct dat = dataListTmp[i]; string data = System.Text.Encoding.Default.GetString(dat.m_data_buf); string temp = data.Trim(); WriteToFileClass writeClass = new WriteToFileClass("ReceivedLog"); Thread t = new Thread(new ParameterizedThreadStart(writeClass.WriteInfoToFile)); t.Start("GPRS: " + "长度:" + data.Length + " " + data + "\r\n"); if (temp.Contains("$")) { string[] dataList = temp.Split('$'); //数据解析 for (int j = 0; i < dataList.Length; j++) { if (dataList[j].Length < 20) { continue; } string dataGram = dataList[j].Substring(0, dataList[j].IndexOf(';')); CReportStruct report = new CReportStruct(); Protocol.Data.ZFXY.UpParse up = new Data.ZFXY.UpParse(); if (up.Parse(dataGram, out report)) { report.ChannelType = EChannelType.GPRS; report.ListenPort = this.GetListenPort().ToString(); if (this.UpDataReceived != null) { InvokeMessage(dataGram, "[GPRS]接收"); this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = temp }); } } } } } catch (Exception e) { Debug.WriteLine("" + e.Message); } } } }
public bool Parse_2(String msg, out CReportStruct report) { // //6013 $60131G23 030200 012345 4 1 123 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(6, 2); if (type != "23") { return(false); } // 解析报文类别 //EMessageType reportType = ProtocolMaps.MessageTypeMap.FindKey(data.Substring(8, 2)); // 解析接收时间 DateTime recvTime = DateTime.Now; string day = data.Substring(8, 2); string hour = data.Substring(10, 2); string minute = data.Substring(12, 2); DateTime collectTine = new DateTime(recvTime.Year, recvTime.Month, int.Parse(day), int.Parse(hour), int.Parse(minute), 0); string water = data.Substring(14, 6); string WaterPotential = data.Substring(20, 1); string acc = data.Substring(21, 1); string num = data.Substring(22, 3); string method = data.Substring(25, 1); // 获取数据段,不包含站号、类别、报类、站类信息 report = new CReportStruct() { RecvTime = recvTime, Stationid = stationID }; 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) { //$ 1900020919060308101G21??????????1066@@ QTFM0164 11 0.163 1.171 2.546 0.000 0.334 0.000 0.000 -999.0 -0.2 0.0 0.0 14.7 1.5 99** report = new CReportStruct(); List <CReportData> dataList = new List <CReportData>(); try { string data = string.Empty; //去除起始符'$' if (!ProtocolHelpers.DeleteSpecialCharLong(msg, out data)) { return(false); } //站号(4位) string StationId = data.Substring(0, 10).Trim(); //类别(长度):1G string length = data.Substring(10, 4); DateTime recvTime; 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 ); //上下行标志 string type = data.Substring(24, 2); //报类(2位):22-定时报 string reportTypeString = data.Substring(26, 2); //水位 string waterStr = data.Substring(28, 6); Decimal?water; if (waterStr.Contains("?")) { water = null; } else { water = Decimal.Parse(waterStr) / 100; } //雨量 string rainStr = data.Substring(34, 4); Decimal?rain; if (rainStr.Contains("?")) { rain = null; } else { rain = Decimal.Parse(rainStr) / 100; } //电压 string voltageStr = data.Substring(38, 4); Decimal?voltage; if (voltageStr.Contains("?")) { voltage = null; } else { voltage = Decimal.Parse(voltageStr) / 100; } //报类 EMessageType reportType; reportType = ProtocolMaps.MessageTypeMap.FindKey(reportTypeString); //站类 EStationType stationType; stationType = EStationType.EO; string allElmtData = data.Substring(42); //1.处理时差法数据 CReportData speedData = new CReportData(); int flagIndex = allElmtData.IndexOf("@@ QTFM"); if (flagIndex >= 0) { int keyLength = int.Parse(allElmtData.Substring(8, 4)); string elmtData = allElmtData.Substring(flagIndex, keyLength); //判定要素1的开始符号和结束符号 if (elmtData.StartsWith("@@ QTFM") && elmtData.EndsWith("**")) { elmtData = elmtData.Substring(12, keyLength - 14).Trim(); //判定时差法数据的开始符号和接受符号 if (elmtData.StartsWith("11") && elmtData.EndsWith("99")) { try { elmtData = new System.Text.RegularExpressions.Regex("[\\s]+").Replace(elmtData, " "); string[] elmtDataList = elmtData.Split(' '); speedData.Voltge = voltage; speedData.Vm = Decimal.Parse(elmtDataList[1]); speedData.W1 = Decimal.Parse(elmtDataList[2]); speedData.Q = Decimal.Parse(elmtDataList[3]); speedData.v1 = Decimal.Parse(elmtDataList[4]); speedData.v2 = Decimal.Parse(elmtDataList[5]); speedData.v3 = Decimal.Parse(elmtDataList[6]); speedData.v4 = Decimal.Parse(elmtDataList[7]); speedData.beta1 = Decimal.Parse(elmtDataList[8]); speedData.beta2 = Decimal.Parse(elmtDataList[9]); speedData.beta3 = Decimal.Parse(elmtDataList[10]); speedData.beta4 = Decimal.Parse(elmtDataList[11]); speedData.W2 = Decimal.Parse(elmtDataList[12]); speedData.errorCode = elmtDataList[13]; dataList.Add(speedData); } catch (Exception ee) { //解析失败 return(false); } } else { return(false); //11开头 99结束 } report.Stationid = StationId; report.Type = type; report.ReportType = reportType; report.StationType = stationType; report.RecvTime = recvTime; report.Datas = dataList; } else { return(false); //TODO 要素1开始符号和结束符合不匹配 } } } catch (Exception eee) { return(false); } return(true); }
/// <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); }
private bool ParseData(string msg, string gprs) { // InvokeMessage("协议。。。 ", "进入函数7"); try { string rawData = msg; if (msg.Contains("$")) { string data = string.Empty; if (!ProtocolHelpers.DeleteSpecialChar(msg, out data)) { return(false); } msg = data; string sid = msg.Substring(0, 4); string type = msg.Substring(4, 2); #region 1G if (type == "1G") { string reportType = msg.Substring(6, 2); if (reportType == "21" || reportType == "22") // 定时报,加报 { // YAC设备的墒情协议: string stationType = msg.Substring(8, 2); switch (stationType) { // 站类为04时墒情站 05墒情雨量站 06,16墒情水位站 07,17墒情水文站 case "04": case "05": case "06": case "07": case "17": { //CEntitySoilData soilStruct = new CEntitySoilData(); //if (Soil.Parse(msg, out soilStruct)) //{ // soilStruct.ChannelType = EChannelType.GPRS; // //soilStruct.ListenPort = this.GetListenPort().ToString(); // //string temp = soilStruct.ReportType == EMessageType.EAdditional ? "加报" : "定时报"; // //InvokeMessage(String.Format("{0,-10} ", temp) + rawData, "接收"); // // 抛出YAC设备墒情事件 // if (null != this.SoilDataReceived) // this.SoilDataReceived.Invoke(null, new YACSoilEventArg() // { // RawData = rawData, // Value = soilStruct // }); //} CEntitySoilData soil = new CEntitySoilData(); CReportStruct soilReport = new CReportStruct(); if (Soil.Parse(rawData, out soil, out soilReport)) { soil.ChannelType = EChannelType.GPRS; if (null != this.SoilDataReceived) { this.SoilDataReceived(null, new CEventSingleArgs <CEntitySoilData>(soil)); } //1111gm string temp = soilReport.ReportType == EMessageType.EAdditional ? "加报" : "定时报"; InvokeMessage(" gprs号码: " + gprs + String.Format(" {0,-10} ", temp) + rawData, "接收"); if (null != soilReport && null != this.UpDataReceived) { soilReport.ChannelType = EChannelType.GPRS; soilReport.ListenPort = this.GetListenPort().ToString(); soilReport.flagId = gprs; this.UpDataReceived(null, new UpEventArgs() { RawData = rawData, Value = soilReport }); } } else { //string temp = soilReport.ReportType == EMessageType.EAdditional ? "加报" : "定时报"; InvokeMessage(" gprs号码: " + gprs + " " + rawData, "接收"); } } break; // 站类为01,02,03,12,13时,不是墒情站 case "01": case "02": case "03": case "12": case "13": { CReportStruct report = new CReportStruct(); if (Up.Parse(msg, out report)) { report.ChannelType = EChannelType.GPRS; report.ListenPort = this.GetListenPort().ToString(); report.flagId = gprs; string temp = report.ReportType == EMessageType.EAdditional ? "加报" : "定时报"; InvokeMessage(" gprs号码: " + gprs + String.Format(" {0,-10} ", temp) + rawData, "接收"); if (this.UpDataReceived != null) { this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = rawData }); } // InvokeMessage("12333", "接收"); } else { //string temp = report.ReportType == EMessageType.EAdditional ? "加报" : "定时报"; InvokeMessage(" gprs号码: " + gprs + " 可疑数据 " + rawData, "接收"); } } break; case "11": { CReportStruct report = new CReportStruct(); //CReportArtificalWater report = new CReportArtificalWater(); if (Up.Parse_1(msg, out report)) { report.ChannelType = EChannelType.GPRS; report.ListenPort = this.GetListenPort().ToString(); report.ReportType = EMessageType.Batch; string temptype = "人工水位"; InvokeMessage(String.Format("{0,-10} ", temptype) + rawData, "接收"); //if (this.UpDataReceived != null) this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = rawData }); } } break; default: break; } } else if (reportType == "23") // 人工流量 { CReportStruct report = new CReportStruct(); if (Up.Parse_2(msg, out report)) { report.ChannelType = EChannelType.GPRS; report.ListenPort = this.GetListenPort().ToString(); report.ReportType = EMessageType.Batch; string temptype = "人工流量"; InvokeMessage(String.Format("{0,-10} ", temptype) + rawData, "接收"); this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = rawData }); } } else if (reportType == "32") // 人工报送水位 { CReportStruct report = new CReportStruct(); report.ChannelType = EChannelType.GPRS; report.ListenPort = this.GetListenPort().ToString(); string temptype = "人工报送水位"; InvokeMessage(String.Format("{0,-10} ", temptype) + rawData, "接收"); WriteToFileClass writeClass = new WriteToFileClass("RGwater"); //Thread t = new Thread(new ParameterizedThreadStart(writeClass.WriteInfoToFile)); //t.Start("GPRS: " + "长度:" + data.Length + " " + rawData + "\r\n"); string a1 = rawData.Substring(9, 1); string a2 = rawData.Substring(10, 1); if (a1 == "P" || a1 == "H" || a1 == "K" || a1 == "Z" || a1 == "D" || a1 == "T" || a1 == "M" || a1 == "G" || a1 == "Y" || a1 == "F" || a1 == "R") { if (a2 == "A") { if (rawData.Contains("ST")) { WriteToFileClass writeClass1 = new WriteToFileClass("sharewater"); Thread t1 = new Thread(new ParameterizedThreadStart(writeClass1.WriteInfoToFile)); t1.Start("GPRS: " + "长度:" + data.Length + " " + rawData + "\r\n"); } } } } else if (reportType == "53") // 人工报送时段雨量 日雨量 旬雨量 水库水位 蓄水量 入库流量 出库流量 { CReportStruct report = new CReportStruct(); report.ChannelType = EChannelType.GPRS; report.ListenPort = this.GetListenPort().ToString(); string temptype = "人工报送雨量"; InvokeMessage(String.Format("{0,-10} ", temptype) + rawData, "接收"); WriteToFileClass writeClass = new WriteToFileClass("RGRain"); Thread t = new Thread(new ParameterizedThreadStart(writeClass.WriteInfoToFile)); t.Start("GPRS: " + "长度:" + data.Length + " " + rawData + "\r\n"); } else if (reportType == "25") { //CEntitySoilData readSoilStruct = new CEntitySoilData(); //if (Soil.Parse(msg, out readSoilStruct)) //{ // readSoilStruct.ChannelType = EChannelType.GPRS; // //readSoilStruct.ListenPort = this.GetListenPort().ToString(); // //string temp = readSoilStruct.ReportType == EMessageType.EAdditional ? "加报" : "定时报"; // //InvokeMessage(String.Format("{0,-10} ", temp) + rawData, "接收"); // // 抛出读墒情事件 // if (null != this.SoilDataReceived) // this.SoilDataReceived.Invoke(null, new YACSoilEventArg() // { // RawData = rawData, // Value = readSoilStruct // }); //} CEntitySoilData soil = new CEntitySoilData(); CReportStruct soilReport = new CReportStruct(); if (Soil.Parse(rawData, out soil, out soilReport)) { soil.ChannelType = EChannelType.GPRS; if (null != this.SoilDataReceived) { this.SoilDataReceived(null, new CEventSingleArgs <CEntitySoilData>(soil)); } if (null != soilReport && null != this.UpDataReceived) { soilReport.ChannelType = EChannelType.GPRS; soilReport.ListenPort = this.GetListenPort().ToString(); soilReport.flagId = gprs; this.UpDataReceived(null, new UpEventArgs() { RawData = rawData, Value = soilReport }); } } } else // 下行指令 { CDownConf downconf = new CDownConf(); if (Down.Parse(msg, out downconf)) { InvokeMessage(String.Format("{0,-10} ", "下行指令读取参数") + rawData, "接收"); if (this.DownDataReceived != null) { this.DownDataReceived.Invoke(null, new DownEventArgs() { Value = downconf, RawData = rawData }); } } } } #endregion #region 1K if (type == "1K") { var station = FindStationBySID(sid); if (station == null) { throw new Exception("批量传输,站点匹配错误"); } //EStationBatchType batchType = station.BatchTranType; //if (batchType == EStationBatchType.EFlash) //{ // CBatchStruct batch = new CBatchStruct(); // if (FlashBatch.Parse(msg, out batch)) // { // InvokeMessage(String.Format("{0,-10} ", "Flash批量传输") + rawData, "接收"); // if (this.BatchDataReceived != null) // this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = rawData }); // } //} //else if (batchType == EStationBatchType.EUPan) //{ // CBatchStruct batch = new CBatchStruct(); // if (UBatch.Parse(msg, out batch)) // { // InvokeMessage(String.Format("{0,-10} ", "U盘批量传输") + rawData, "接收"); // if (this.BatchDataReceived != null) // this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = rawData }); // } //} CBatchStruct batch = new CBatchStruct(); if (FlashBatch.Parse(msg, out batch)) { InvokeMessage(String.Format("{0,-10} ", "批量传输") + rawData, "接收"); if (this.BatchDataReceived != null) { this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = rawData }); } } } #endregion #region 1S if (type == "1S") { CDownConf downconf = new CDownConf(); if (Down.Parse(msg, out downconf)) { InvokeMessage(String.Format("{0,-10} ", "下行指令设置参数") + rawData, "接收"); if (this.DownDataReceived != null) { this.DownDataReceived.Invoke(null, new DownEventArgs() { Value = downconf, RawData = rawData }); } } } #endregion } else if (msg.Contains("#")) { CEntitySoilData soil = new CEntitySoilData(); CReportStruct soilReport = new CReportStruct(); if (Soil.Parse(rawData, out soil, out soilReport)) { soil.ChannelType = EChannelType.GPRS; if (null != this.SoilDataReceived) { this.SoilDataReceived(null, new CEventSingleArgs <CEntitySoilData>(soil)); } if (null != soilReport && null != this.UpDataReceived) { soilReport.ChannelType = EChannelType.GPRS; soilReport.ListenPort = this.GetListenPort().ToString(); soilReport.flagId = gprs; this.UpDataReceived(null, new UpEventArgs() { RawData = rawData, Value = soilReport }); } } } else { return(false); } return(true); } catch (Exception exp) { //System.Diagnostics.Debug.WriteLine("GPRS 数据解析出错 !" + msg + "\r\n" + exp.Message); } return(false); }
public bool Parse(string msg, out CReportStruct report) { report = new CReportStruct(); List <CReportData> dataList = new List <CReportData>(); List <CReportPwdData> dataPwdList = new List <CReportPwdData>(); List <CReportFsfx> dataFsfxList = new List <CReportFsfx>(); try { string data = string.Empty; //去除起始符'$' if (!ProtocolHelpers.DeleteSpecialCharLong(msg, out data)) { return(false); } //站号(4位) string StationId = data.Substring(0, 10).Trim(); //长度:1G string length = data.Substring(10, 4); DateTime recvTime; 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 ); //上下行标志 string type = data.Substring(24, 2); //报类(2位):22-定时报 string reportTypeString = data.Substring(26, 2); //站类 string stationTypeString = data.Substring(28, 2); //水位 string waterStr = data.Substring(30, 6); Decimal?water; if (waterStr.Contains("?")) { water = null; } else { water = Decimal.Parse(waterStr) / 100; } //雨量 string rainStr = data.Substring(36, 4); Decimal?rain; if (rainStr.Contains("?")) { rain = null; } else { rain = Decimal.Parse(rainStr) / 100; } //电压 string voltageStr = data.Substring(40, 4); Decimal?voltage; if (voltageStr.Contains("?")) { voltage = null; } else { voltage = Decimal.Parse(voltageStr) / 100; } //报类 EMessageType reportType; reportType = ProtocolMaps.MessageTypeMap.FindKey(reportTypeString); //站类 EStationType stationType; stationType = ProtocolMaps.StationTypeMap.FindKey(stationTypeString); report.Stationid = StationId; report.Type = type; report.ReportType = reportType; report.StationType = stationType; report.RecvTime = recvTime; string allElmtData = data.Substring(44); //1.处理风速风向数据和散射仪数据 CReportData speedData = new CReportData(); CReportPwdData pwdData = new CReportPwdData(); CReportFsfx fsfxData = new CReportFsfx(); //1.1 风速风向信息 int flagIndex = allElmtData.IndexOf("@@ EN2B"); if (flagIndex >= 0) { int keyLength = int.Parse(allElmtData.Substring(flagIndex + 8, 4)); string elmtData = allElmtData.Substring(flagIndex, keyLength); //判定要素1的开始符号和结束符号 if (elmtData.StartsWith("@@ EN2B") && elmtData.EndsWith("**")) { elmtData = elmtData.Substring(12, keyLength - 14).Trim(); //判定时差法数据的开始符号和接受符号 if (elmtData.Length == (keyLength - 14)) { CReportFsfx dataFsfx = new CReportFsfx(); try { string strtflag = elmtData.Substring(0, 1); //开始标志 string stationid = elmtData.Substring(1, 5); //站点ID string msgTime = elmtData.Substring(6, 12); //时间 string shfx = elmtData.Substring(18, 4); //瞬时风向 string shfs = elmtData.Substring(22, 4); //顺时风速 string yxszdshfx = elmtData.Substring(26, 4); //一小时最大瞬时风向 string yxszdshfs = elmtData.Substring(30, 4); //一小时最大瞬时风速 string maxTime = elmtData.Substring(34, 4); //一小时最大瞬时风速出现时间 string avg2fx = elmtData.Substring(38, 4); //2分钟平均风向 string avg2fs = elmtData.Substring(42, 4); //2分钟平均风速 string avg10fx = elmtData.Substring(46, 4); //10分钟平均风向 string avg10fs = elmtData.Substring(50, 4); //10分钟平均风速 string max10fx = elmtData.Substring(54, 4); //10分钟平均最大风向 string max10fs = elmtData.Substring(58, 4); //10分钟平均最大风速 string max10tm = elmtData.Substring(62, 4); //10分钟最大风速出现时间 fsfxData.Water = water; fsfxData.Time = recvTime; fsfxData.shfx = decimal.Parse(shfx); fsfxData.shfs = decimal.Parse(shfx) / 10; fsfxData.yxszdshfx = decimal.Parse(yxszdshfx); fsfxData.yxszdshfs = decimal.Parse(shfx) / 10; fsfxData.maxTime = new DateTime(recvTime.Year, recvTime.Month, recvTime.Day, int.Parse(maxTime.Substring(0, 2)), int.Parse(maxTime.Substring(2, 2)), 0); fsfxData.avg2fx = decimal.Parse(avg2fx); fsfxData.avg2fs = decimal.Parse(avg2fs) / 10; fsfxData.avg10fx = decimal.Parse(avg10fx); fsfxData.avg10fs = decimal.Parse(avg10fs) / 10; fsfxData.max10fx = decimal.Parse(max10fx); fsfxData.max10fs = decimal.Parse(max10fs) / 10; fsfxData.max10tm = new DateTime(recvTime.Year, recvTime.Month, recvTime.Day, int.Parse(max10tm.Substring(0, 2)), int.Parse(max10tm.Substring(2, 2)), 0); fsfxData.Voltge = voltage; dataFsfxList.Add(fsfxData); } catch (Exception e) { return(false); } } else { return(false); //11开头 99结束 } report.fsfxDatas = dataFsfxList; } else { return(false); //TODO 要素1开始符号和结束符合不匹配 } } //1.2 散射仪数据 //015057203031023033202F2F2F2F2F2F202F2F2F2F2F030D0A int flagIndex2 = allElmtData.IndexOf("@@ PWD"); if (flagIndex2 >= 0) { int keyLength = int.Parse(allElmtData.Substring(flagIndex2 + 8, 4)); string elmtData = allElmtData.Substring(flagIndex2, keyLength); if (elmtData.StartsWith("@@ PWD") && elmtData.EndsWith("**")) { elmtData = elmtData.Substring(12, keyLength - 14).Trim(); elmtData = elmtData.Substring(0, elmtData.Length - 1).Trim(); elmtData = new System.Text.RegularExpressions.Regex("[\\s]+").Replace(elmtData, " "); string[] elmtDataList = elmtData.Split(' '); pwdData.Time = recvTime; try { pwdData.Visi1min = decimal.Parse(elmtDataList[2].Trim()); } catch { pwdData.Visi1min = null; } try { pwdData.Visi10min = decimal.Parse(elmtDataList[3]); } catch { pwdData.Visi10min = null; } pwdData.Voltge = voltage; dataPwdList.Add(pwdData); } else { return(false); } report.pwdDatas = dataPwdList; //report.Datas = dataList; } } catch (Exception eee) { return(false); } return(true); }
// 处理COUT类型数据 private void DealCOUT(string msg) // private String DealCOUT(string msg) { string result = null; InvokeMessage("通信输出 " + msg, "接收"); SendBackTTCAString = msg; if (msg.Contains("COUT")) { if (msg.Contains("COUT")) { num = num + 1; FileStream fs = new FileStream("numbd.txt", FileMode.Create); StreamWriter sw = new StreamWriter(fs); //开始写入 sw.Write(num); //清空缓冲区 sw.Flush(); //关闭流 sw.Close(); fs.Close(); } } // InvokeString(msg); var cout = BeidouHelper.GetCOUTInfo(msg); if (cout == null) { return; } // return result; // 发送COSS指令 // 必须在1秒内发送给卫星终端,否则会重新发送“通信输出” var coss = new CCOSSStruct(); coss.SuccessStatus = true; // 终端接收到外设通信申请,并校验成功 SendCOSS(coss); // 发送CACA指令 // 每隔一分钟发送回执$CACA // 65秒左右发一条 var caca = new CCACAStruct(); caca.SenderID = "1"; // 发信方ID为1,表示本机ID,默认 caca.RecvType = cout.SenderType; // 回执的收信方类型 == 通信输出中的发信方类型 caca.RecvAddr = cout.SenderAddr; // 回执的收信方地址 == 通信输出中的发信方地址 caca.Requirements = "1"; // 不保密 caca.ReceiptMsgSequenceNum = cout.MsgSequenceNum; // 回执的报文顺序号 == 通信输出中的报文顺序号 caca.ReceiptContent = "1"; SendCACA(caca); // 解析通信输出中的内容 string content = cout.MsgContent; //通信输出gm8 $60131G2201161111040003046112271367 try { string rawMsg = content; string sid = null; string dealMsg = ProtocolHelpers.dealBCD(rawMsg); WriteToFileClass writeClass = new WriteToFileClass("ReceivedLog"); Thread t = new Thread(new ParameterizedThreadStart(writeClass.WriteInfoToFile)); t.Start("COUT Message: " + " " + dealMsg + "\r\n"); string bdid = cout.SenderAddr; //InvokeMessage("gm3 " + content, "接收"); //sid需要根据北斗卫星号获取 try { sid = Manager.XmlStationDataSerializer.Instance.GetStationByBDID(bdid); } catch (Exception e) { sid = "5712"; Debug.WriteLine("获取站号失败" + e.Message); } EMessageType type = ProtocolMaps.MessageTypeMap.FindKey(dealMsg.Substring(0, 2)); string reportType = dealMsg.Substring(0, 2); if (reportType == "21" || reportType == "22") // 定时报,加报 { // YAC设备的墒情协议: string stationType = dealMsg.Substring(2, 2); switch (stationType) { // 站类为04时墒情站 05墒情雨量站 06,16墒情水位站 07,17墒情水文站 case "04": case "05": case "06": case "07": case "17": { // var station = FindStationByBeidouID(cout.SenderAddr); // string currentMsg = rawMsg.Insert(0, "$" + station.StationID + "1G"); // CEntitySoilData soil = new CEntitySoilData(); // CReportStruct soilReport = new CReportStruct(); // if (Soil.Parse(currentMsg, out soil, out soilReport)) // { // soil.ChannelType = EChannelType.BeiDou; // if (null != this.SoilDataReceived) // this.SoilDataReceived(null, new CEventSingleArgs<CEntitySoilData>(soil)); // if (null != soilReport && null != this.UpDataReceived) // { // soilReport.ChannelType = EChannelType.BeiDou; // soilReport.ListenPort = "COM" + this.Port.PortName; // this.UpDataReceived(null, new UpEventArgs() { RawData = rawMsg, Value = soilReport }); // } // } //} //1111gm string newMsg = dealMsg.Substring(1, dealMsg.Length - 1); CEntitySoilData soil = new CEntitySoilData(); CReportStruct soilReport = new CReportStruct(); if (Soil.Parse(newMsg, out soil, out soilReport)) { soilReport.ChannelType = EChannelType.BeiDou; if (null != this.SoilDataReceived) { this.SoilDataReceived(null, new CEventSingleArgs <CEntitySoilData>(soil)); } if (null != soilReport && null != this.UpDataReceived) { soilReport.ChannelType = EChannelType.GPRS; soilReport.ListenPort = "COM" + this.Port.PortName; soilReport.flagId = bdid; this.UpDataReceived(null, new UpEventArgs() { RawData = newMsg, Value = soilReport }); } } } break; // 站类为01,02,03,12,13时,不是墒情站 case "01": case "02": case "03": case "12": case "13": { //1111gm //string newMsg = dealMsg.Substring(1, dealMsg.Length - 1); //CReportStruct report = new CReportStruct(); //if (Up.Parse(newMsg, out report)) //{ // //6013 $60131G2201161111040003046112271367 // report.ChannelType = EChannelType.BeiDou; // report.ListenPort = "COM" + this.Port.PortName; // if (this.UpDataReceived != null) // { // this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = newMsg }); // } //} CReportStruct report = new CReportStruct(); UpParser Up1 = new UpParser(); if (Up1.Parse_beidou(sid, type, rawMsg, out report)) { //InvokeMessage("gm6 " + rawMsg, "接收"); //$60131G2201161111040003046112271367 report.ChannelType = EChannelType.BeiDou; report.ListenPort = "COM" + this.Port.PortName; report.flagId = bdid; if (this.UpDataReceived != null) { //InvokeMessage("gm7 " + rawMsg, "接收"); this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = rawMsg }); } } } break; default: break; } } else if (reportType == "11") // 人工水位 { } else if (reportType == "23") // 人工流量 { } else if (reportType == "25") { var station = FindStationByBeidouID(cout.SenderAddr); string currentMsg = dealMsg.Insert(0, "$" + station.StationID + "1G"); CEntitySoilData soil = new CEntitySoilData(); CReportStruct soilReport = new CReportStruct(); if (Soil.Parse(currentMsg, out soil, out soilReport)) { soil.ChannelType = EChannelType.BeiDou; if (null != this.SoilDataReceived) { this.SoilDataReceived(null, new CEventSingleArgs <CEntitySoilData>(soil)); } if (null != soilReport && null != this.UpDataReceived) { soilReport.ChannelType = EChannelType.BeiDou; soilReport.ListenPort = "COM" + this.Port.PortName; soilReport.flagId = bdid; this.UpDataReceived(null, new UpEventArgs() { RawData = dealMsg, Value = soilReport }); } } } } catch (Exception exp) { System.Diagnostics.Debug.WriteLine("Beidou 数据解析出错 !" + content + "\r\n" + exp.Message); } //result = msg; //return result; }
/// <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); } }
public bool Parse_2(string msg, out CReportStruct report) { throw new NotImplementedException(); }
// 处理通信输出 COUT类型数据 private void DealCOUT(string msg) { InvokeMessage("通信输出 " + msg.Trim(), "接收"); string str = msg.Trim(); if (str.Contains("COUT")) { if (str.Contains("COUT")) { num = num + 1; FileStream fs = new FileStream("numbd.txt", FileMode.Create); StreamWriter sw = new StreamWriter(fs); //开始写入 sw.Write(num); //清空缓冲区 sw.Flush(); //关闭流 sw.Close(); fs.Close(); } } var cout = Beidou500Helper.GetCOUTInfo(msg); //InvokeMessage("gm1 " + "通过截取", "接收"); if (cout == null) { //InvokeMessage("gm2 " + "通过截取", "接收"); return; } // 解析通信输出中的内容 string content = cout.MsgContent; try { string rawMsg = content; //InvokeMessage("gm3 " + content, "接收"); string reportType = rawMsg.Substring(6, 2); //InvokeMessage("gm4 " + reportType, "接收"); if (reportType == "21" || reportType == "22") // 定时报,加报 { // YAC设备的墒情协议: string stationType = rawMsg.Substring(8, 2); //InvokeMessage("gm5 " + stationType, "接收"); switch (stationType) { // 站类为04时墒情站 05墒情雨量站 06,16墒情水位站 07,17墒情水文站 case "04": case "05": case "06": case "07": case "17": { //var station = FindStationByBeidouID(cout.SenderAddr); //string currentMsg = rawMsg.Insert(0, "$" + station.StationID + "1G"); //CEntitySoilData soil = new CEntitySoilData(); //CReportStruct soilReport = new CReportStruct(); //if (Soil.Parse(currentMsg, out soil, out soilReport)) //{ // soil.ChannelType = EChannelType.BeiDou; // if (null != this.SoilDataReceived) // this.SoilDataReceived(null, new CEventSingleArgs<CEntitySoilData>(soil)); // if (null != soilReport && null != this.UpDataReceived) // { // soilReport.ChannelType = EChannelType.BeiDou; // soilReport.ListenPort = "COM" + this.Port.PortName; // this.UpDataReceived(null, new UpEventArgs() { RawData = rawMsg, Value = soilReport }); // } //} // string newMsg = rawMsg.Substring(1, rawMsg.Length - 1); CEntitySoilData soil = new CEntitySoilData(); CReportStruct soilReport = new CReportStruct(); if (Soil.Parse(rawMsg, out soil, out soilReport)) { soilReport.ChannelType = EChannelType.BeiDou; if (null != this.SoilDataReceived) { this.SoilDataReceived(null, new CEventSingleArgs <CEntitySoilData>(soil)); } if (null != soilReport && null != this.UpDataReceived) { soilReport.ChannelType = EChannelType.BeiDou; soilReport.ListenPort = "COM" + this.Port.PortName; soilReport.flagId = cout.SenderAddr; this.UpDataReceived(null, new UpEventArgs() { RawData = rawMsg, Value = soilReport }); } } } break; // 站类为01,02,03,12,13时,不是墒情站 case "01": case "02": case "03": case "12": case "13": { //var station = FindStationByBeidouID(cout.SenderAddr); //rawMsg = rawMsg.Insert(0, station.StationID + " "); //CReportStruct report = new CReportStruct(); //if (Up.Parse(rawMsg, out report)) //{ // report.ChannelType = EChannelType.BeiDou; // report.ListenPort = "COM" + this.Port.PortName; // if (this.UpDataReceived != null) // this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = msg }); //} //string newMsg = rawMsg.Substring(1, rawMsg.Length - 1); CReportStruct report = new CReportStruct(); if (Up.Parse(rawMsg, out report)) { //InvokeMessage("gm6 " + rawMsg, "接收"); //$60131G2201161111040003046112271367 report.ChannelType = EChannelType.BeiDou; report.ListenPort = "COM" + this.Port.PortName; report.flagId = cout.SenderAddr; if (this.UpDataReceived != null) { //InvokeMessage("Cout Test " + rawMsg, "接收"); this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = rawMsg }); } } } break; default: break; } } else if (reportType == "11") // 人工水位 { } else if (reportType == "23") // 人工流量 { } else if (reportType == "25") { var station = FindStationByBeidouID(cout.SenderAddr); string currentMsg = rawMsg.Insert(0, "$" + station.StationID + "1G"); CEntitySoilData soil = new CEntitySoilData(); CReportStruct soilReport = new CReportStruct(); if (Soil.Parse(currentMsg, out soil, out soilReport)) { soil.ChannelType = EChannelType.BeiDou; if (null != this.SoilDataReceived) { this.SoilDataReceived(null, new CEventSingleArgs <CEntitySoilData>(soil)); } if (null != soilReport && null != this.UpDataReceived) { soilReport.ChannelType = EChannelType.BeiDou; soilReport.ListenPort = "COM" + this.Port.PortName; this.UpDataReceived(null, new UpEventArgs() { RawData = rawMsg, Value = soilReport }); } } } } catch (Exception exp) { System.Diagnostics.Debug.WriteLine("北斗卫星指挥机 数据解析出错 !" + content + "\r\n" + exp.Message); } }
//23001G221812291500????12980064@@Z1001234Z2001234Z3001234Z4001234Z5001234Z6001234Z7001234Z8001234\r\n public bool Parse(String msg, out List <CReportStruct> reportList) { reportList = new List <CReportStruct>(); CReportStruct report = new CReportStruct(); try { //站号(4位) string StationId = msg.Substring(0, 4); //类别(2位):1G string type = msg.Substring(4, 2); //报类(2位):22-定时报 string reportTypeString = msg.Substring(6, 2); EMessageType reportType; EStationType stationType; DateTime recvTime; Decimal Voltage = 0; Decimal rain = 0; switch (reportTypeString) { case "21": case "22": reportType = ProtocolMaps.MessageTypeMap.FindKey(reportTypeString); stationType = EStationType.EReservoir; recvTime = new DateTime( year: Int32.Parse("20" + msg.Substring(8, 2)), month: Int32.Parse(msg.Substring(10, 2)), day: Int32.Parse(msg.Substring(12, 2)), hour: Int32.Parse(msg.Substring(14, 2)), minute: Int32.Parse(msg.Substring(16, 2)), second: 0 ); try { rain = Decimal.Parse(msg.Substring(18, 4)); } catch (Exception e) { rain = -1; } try { Voltage = Decimal.Parse(msg.Substring(22, 4)) * (Decimal)0.01; }catch (Exception e) { Voltage = 0; } if (msg.Contains("@@")) { int flagIndex = msg.IndexOf("@@"); if (flagIndex > 0) { //水位1 int stationId1 = int.Parse(StationId); string waterStr1 = msg.Substring(flagIndex + 2, 8); if (waterStr1 != "-2000000") { Decimal water1 = decimal.Parse(waterStr1) * (Decimal)0.01; if (water1 < 0 || water1 > 655) { water1 = 0; } List <CReportData> datas = new List <CReportData>(); CReportData data = new CReportData(); data.Rain = rain; data.Voltge = Voltage; data.Water = water1; data.Time = recvTime; datas.Add(data); report = new CReportStruct() { Stationid = stationId1.ToString(), Type = type, ReportType = reportType, StationType = stationType, RecvTime = DateTime.Now, Datas = datas }; reportList.Add(report); } //水位2 int stationId2 = int.Parse(StationId) + 1; string waterStr2 = msg.Substring(flagIndex + 2 + 8 * 1, 8); if (waterStr2 != "-2000000") { Decimal water2 = decimal.Parse(waterStr2) * (Decimal)0.01; if (water2 < 0 || water2 > 655) { water2 = 0; } List <CReportData> datas = new List <CReportData>(); CReportData data = new CReportData(); data.Rain = rain; data.Voltge = Voltage; data.Water = water2; data.Time = recvTime; datas.Add(data); report = new CReportStruct() { Stationid = stationId2.ToString(), Type = type, ReportType = reportType, StationType = stationType, RecvTime = DateTime.Now, Datas = datas }; reportList.Add(report); } //水位3 int stationId3 = int.Parse(StationId) + 2; string waterStr3 = msg.Substring(flagIndex + 2 + 8 * 2, 8); if (waterStr3 != "-2000000") { Decimal water3 = decimal.Parse(waterStr3) * (Decimal)0.01; if (water3 < 0 || water3 > 655) { water3 = 0; } List <CReportData> datas = new List <CReportData>(); CReportData data = new CReportData(); data.Rain = rain; data.Voltge = Voltage; data.Water = water3; data.Time = recvTime; datas.Add(data); report = new CReportStruct() { Stationid = stationId3.ToString(), Type = type, ReportType = reportType, StationType = stationType, RecvTime = DateTime.Now, Datas = datas }; reportList.Add(report); } //水位4 int stationId4 = int.Parse(StationId) + 3; string waterStr4 = msg.Substring(flagIndex + 2 + 8 * 3, 8); if (waterStr4 != "-2000000") { Decimal water4 = decimal.Parse(waterStr4) * (Decimal)0.01; if (water4 < 0 || water4 > 655) { water4 = 0; } List <CReportData> datas = new List <CReportData>(); CReportData data = new CReportData(); data.Rain = rain; data.Voltge = Voltage; data.Water = water4; data.Time = recvTime; datas.Add(data); report = new CReportStruct() { Stationid = stationId4.ToString(), Type = type, ReportType = reportType, StationType = stationType, RecvTime = DateTime.Now, Datas = datas }; reportList.Add(report); } //水位5 int stationId5 = int.Parse(StationId) + 4; string waterStr5 = msg.Substring(flagIndex + 2 + 8 * 4, 8); if (waterStr5 != "-2000000") { Decimal water5 = decimal.Parse(waterStr5) * (Decimal)0.01; if (water5 < 0 || water5 > 655) { water5 = 0; } List <CReportData> datas = new List <CReportData>(); CReportData data = new CReportData(); data.Rain = rain; data.Voltge = Voltage; data.Water = water5; data.Time = recvTime; datas.Add(data); report = new CReportStruct() { Stationid = stationId5.ToString(), Type = type, ReportType = reportType, StationType = stationType, RecvTime = DateTime.Now, Datas = datas }; reportList.Add(report); } //水位6 int stationId6 = int.Parse(StationId) + 5; string waterStr6 = msg.Substring(flagIndex + 2 + 8 * 5, 8); if (waterStr6 != "-2000000") { Decimal water6 = decimal.Parse(waterStr6) * (Decimal)0.01; if (water6 < 0 || water6 > 655) { water6 = 0; } List <CReportData> datas = new List <CReportData>(); CReportData data = new CReportData(); data.Rain = rain; data.Voltge = Voltage; data.Water = water6; data.Time = recvTime; datas.Add(data); report = new CReportStruct() { Stationid = stationId6.ToString(), Type = type, ReportType = reportType, StationType = stationType, RecvTime = DateTime.Now, Datas = datas }; reportList.Add(report); } //水位7 int stationId7 = int.Parse(StationId) + 6; string waterStr7 = msg.Substring(flagIndex + 2 + 8 * 6, 8); if (waterStr7 != "-2000000") { Decimal water7 = decimal.Parse(waterStr7) * (Decimal)0.01; if (water7 < 0 || water7 > 655) { water7 = 0; } List <CReportData> datas = new List <CReportData>(); CReportData data = new CReportData(); data.Rain = rain; data.Voltge = Voltage; data.Water = water7; data.Time = recvTime; datas.Add(data); report = new CReportStruct() { Stationid = stationId7.ToString(), Type = type, ReportType = reportType, StationType = stationType, RecvTime = DateTime.Now, Datas = datas }; reportList.Add(report); } //水位8 int stationId8 = int.Parse(StationId) + 7; string waterStr8 = msg.Substring(flagIndex + 2 + 8 * 7, 8); if (waterStr8 != "-2000000") { Decimal water8 = decimal.Parse(waterStr8) * (Decimal)0.01; if (water8 < 0 || water8 > 655) { water8 = 0; } List <CReportData> datas = new List <CReportData>(); CReportData data = new CReportData(); data.Rain = rain; data.Voltge = Voltage; data.Water = water8; data.Time = recvTime; datas.Add(data); report = new CReportStruct() { Stationid = stationId8.ToString(), Type = type, ReportType = reportType, StationType = stationType, RecvTime = DateTime.Now, Datas = datas }; reportList.Add(report); } } } break; default: break; } } catch (Exception e) { } return(true); }
public bool Parse(string resp, out CEntitySoilData soil, out CReportStruct report) { throw new NotImplementedException(); }
public bool Parse(string msg, out CReportStruct report) { report = new CReportStruct(); List <CReportData> dataList = new List <CReportData>(); try { string data = string.Empty; //去除起始符'$' if (!ProtocolHelpers.DeleteSpecialCharLong(msg, out data)) { return(false); } //站号(4位) string StationId = data.Substring(0, 10).Trim(); //长度:1G string length = data.Substring(10, 4); DateTime recvTime; 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 ); //上下行标志 string type = data.Substring(24, 2); //报类(2位):22-定时报 string reportTypeString = data.Substring(26, 2); //站类 string stationTypeString = data.Substring(28, 2); //水位 string waterStr = data.Substring(30, 6); Decimal?water; if (waterStr.Contains("?")) { water = null; } else { water = Decimal.Parse(waterStr) / 100; } //雨量 string rainStr = data.Substring(36, 4); Decimal?rain; if (rainStr.Contains("?")) { rain = null; } else { rain = Decimal.Parse(rainStr) / 100; } //电压 string voltageStr = data.Substring(40, 4); Decimal?voltage; if (voltageStr.Contains("?")) { voltage = null; } else { voltage = Decimal.Parse(voltageStr) / 100; } //报类 EMessageType reportType; reportType = ProtocolMaps.MessageTypeMap.FindKey(reportTypeString); //站类 EStationType stationType; stationType = ProtocolMaps.StationTypeMap.FindKey(stationTypeString); string allElmtData = data.Substring(44); //1.处理风速风向数据和散射仪数据 CReportData speedData = new CReportData(); //1.1 风速风向信息 int flagIndex = allElmtData.IndexOf("@@ EN2B"); if (flagIndex >= 0) { int keyLength = int.Parse(allElmtData.Substring(8, 4)); string elmtData = allElmtData.Substring(flagIndex, keyLength); //判定要素1的开始符号和结束符号 if (elmtData.StartsWith("@@ EN2B") && elmtData.EndsWith("**")) { elmtData = elmtData.Substring(12, keyLength - 14).Trim(); //判定时差法数据的开始符号和接受符号 if (elmtData.Length == (keyLength - 14)) { try { string strtflag = elmtData.Substring(0, 1); //开始标志 string stationid = elmtData.Substring(1, 5); //站点ID string msgTime = elmtData.Substring(6, 12); //时间 string shfx = elmtData.Substring(18, 4); //瞬时风向 string shfs = elmtData.Substring(22, 4); //顺时风速 string yxszdshfx = elmtData.Substring(26, 4); //一小时最大瞬时风向 string yxszdshfs = elmtData.Substring(30, 4); //一小时最大瞬时风速 string maxTime = elmtData.Substring(34, 4); //一小时最大瞬时风速出现时间 string avg2fx = elmtData.Substring(38, 4); //2分钟平均风向 string avg2fs = elmtData.Substring(42, 4); //2分钟平均风速 string avg10fx = elmtData.Substring(46, 4); //10分钟平均风向 string avg10fs = elmtData.Substring(50, 4); //10分钟平均风速 string max10fx = elmtData.Substring(54, 4); //10分钟平均最大风向 string max10fs = elmtData.Substring(58, 4); //10分钟平均最大风速 string max10tm = elmtData.Substring(62, 4); //10分钟最大风速出现时间 } catch (Exception e) { return(false); } } else { return(false); //11开头 99结束 } report.Stationid = StationId; report.Type = type; report.ReportType = reportType; report.StationType = stationType; report.RecvTime = recvTime; report.Datas = dataList; } else { return(false); //TODO 要素1开始符号和结束符合不匹配 } } //1.2 散射仪数据 int flagIndex2 = allElmtData.IndexOf("@@ PWD"); if (flagIndex >= 0) { int keyLength = int.Parse(allElmtData.Substring(8, 4)); string elmtData = allElmtData.Substring(flagIndex, keyLength); if (elmtData.StartsWith("@@ EN2B") && elmtData.EndsWith("**")) { } } } catch (Exception eee) { return(false); } return(true); }
/// <summary> /// 数据格式 /// /// <?xml version="1.0" encoding="utf-8" ?> ///<string xmlns = "http://tempuri.org/" ><? xml version = "1.0" encoding="UTF-8" ?> ///<datalist> ///<data> ///<id>1000</id> ///<uptime>2018-10-09 09:10:10</uptime> ///<destaddr>1064810588860</destaddr> ///<content>检测点上行短信内容测试111</content></data> ///<data><id>1001</id><uptime>2018-10-09 10:31:30</uptime> ///<destaddr>1064810589867</destaddr> ///<content>检测点上行短信内容测试222</content></data></datalist></string> /// /// </summary> /// <returns></returns> private bool DealSMSData(string addr, CGSMStruct gsm) { bool isSoil = FindStationByGSMID(addr); string rawdata = gsm.Message; try { if (isSoil) { // 是墒情站 CEntitySoilData soil = new CEntitySoilData(); CReportStruct soilReport = new CReportStruct(); if (Soil.Parse(rawdata, out soil, out soilReport)) { soil.ChannelType = EChannelType.GSM; if (null != this.SoilDataReceived) { this.SoilDataReceived(null, new CEventSingleArgs <CEntitySoilData>(soil)); } string temp = soilReport.ReportType == EMessageType.EAdditional ? "加报" : "定时报"; if (null != soilReport && null != this.UpDataReceived) { soilReport.ChannelType = EChannelType.GSM; soilReport.ListenPort = "WebService-GSM"; soilReport.flagId = gsm.PhoneNumber; this.UpDataReceived(null, new UpEventArgs() { RawData = rawdata, Value = soilReport }); } } else { InvokeMessage(" " + rawdata, "接收"); } } else { // 是水情站 CReportStruct report = new CReportStruct(); if (Up.Parse(rawdata, out report)) /* 解析成功 */ { report.ChannelType = EChannelType.GSM; report.ListenPort = "WebService-GSM"; report.flagId = gsm.PhoneNumber; if (this.UpDataReceived != null) { this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = rawdata }); } } } } catch (Exception) { return(false); } return(true); }
private bool ParseData(string msg, string gprs, uint modemId) { // InvokeMessage("协议。。。 ", "进入函数7"); try { string rawData = msg; string sid; try { sid = Manager.XmlStationData.Instance.GetStationByGprsID(gprs); } catch (Exception e) { Debug.WriteLine("查找站点id失败,赋值为9999" + e.ToString()); sid = "9999"; } if (msg.Contains("$")) { string data = string.Empty; if (!ProtocolHelpers.DeleteSpecialChar(msg, out data)) { return(false); } msg = data; CReportStruct report = new CReportStruct(); CDownConf downReport = new CDownConf(); string protocol = Manager.XmlStationData.Instance.GetProtocolBySId(sid); if (protocol == "YNXY") { Up = new Data.YNXY.UpParser(); Down = new Data.YNXY.DownParser(); } else if (protocol == "ZYJBX") { Up = new Data.ZYJBX.UpParser(); Down = new Data.ZYJBX.DownParser(); } #region 1K //批量传输解析 if (msg.Contains("1K")) { var station = FindStationBySID(sid); if (station == null) { throw new Exception("批量传输,站点匹配错误"); } CBatchStruct batch = new CBatchStruct(); InvokeMessage(String.Format("{0,-10} ", "批量传输") + msg, "接收"); if (Down.Parse_Flash(msg, EChannelType.GPRS, out batch)) { if (this.BatchDataReceived != null) { this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = msg }); } } } #endregion #region 1G if (msg.Contains("1G")) { //if (msg.Contains("RG")) //{ // var lists = msg.Split('$'); // foreach (var segMsg in lists) // { // if (segMsg.Length < 5) // { // continue; // } // string plusMsg = "$" + segMsg; // string rtype = "人工报"; // InvokeMessage("gprs号码: " + gprs + " " + String.Format("{0,-10} ", rtype) + plusMsg, "接收"); // string package = Up.Parse_1(plusMsg, out report); // if (package != "") // { // report.ChannelType = EChannelType.GPRS; // report.ListenPort = this.GetListenPort().ToString(); // report.flagId = gprs; // SendTru(modemId, package); // //TODO 重新定义事件 // if (this.UpDataReceived != null) // { // this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = plusMsg }); // } // } // else // { // InvokeMessage(" " + rawData, "接收"); // } // } //} //else if (msg.Contains("1G21") || msg.Contains("1G22") || msg.Contains("1G25")) // 定时报,加报 { if (msg.Contains("1G29")) { var station = FindStationBySID(sid); if (station == null) { throw new Exception("批量传输,站点匹配错误"); } CBatchStruct batch = new CBatchStruct(); InvokeMessage(String.Format("{0,-10} ", "批量传输") + msg, "接收"); if (Down.Parse_Batch(msg, out batch)) { if (this.BatchDataReceived != null) { this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = msg }); } } } else { var lists = msg.Split('$'); foreach (var segMsg in lists) { if (segMsg.Length < 5) { continue; } string plusMsg = "$" + segMsg; if (Up.Parse(plusMsg, out report)) { report.ChannelType = EChannelType.GPRS; report.ListenPort = this.GetListenPort().ToString(); report.flagId = gprs; string rtype = report.ReportType == EMessageType.EAdditional ? "加报" : "定时报"; InvokeMessage("gprs号码: " + gprs + " " + String.Format("{0,-10} ", rtype) + plusMsg, "接收"); //TODO 重新定义事件 if (this.UpDataReceived != null) { this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = plusMsg }); } } else { InvokeMessage(" " + rawData, "接收"); } } } } else { Down.Parse(rawData, out downReport); if (downReport != null) { InvokeMessage(String.Format("{0,-10} ", "下行指令读取参数") + rawData, "接收"); if (this.DownDataReceived != null) { this.DownDataReceived.Invoke(null, new DownEventArgs() { Value = downReport, RawData = rawData }); } } } #endregion } } else if (msg.Contains("BEG")) { CSDStruct sd = new CSDStruct(); if (Down.Parse_SD(msg, sid, out sd)) { InvokeMessage(String.Format("{0,-10} ", "批量SD传输") + msg, "接收"); if (this.BatchSDDataReceived != null) { this.BatchSDDataReceived.Invoke(null, new BatchSDEventArgs() { Value = sd, RawData = msg }); } } } else if (msg.Contains("#")) { CEntitySoilData soil = new CEntitySoilData(); CReportStruct soilReport = new CReportStruct(); if (Soil.Parse(rawData, out soil, out soilReport)) { soil.ChannelType = EChannelType.GPRS; if (null != this.SoilDataReceived) { this.SoilDataReceived(null, new CEventSingleArgs <CEntitySoilData>(soil)); } if (null != soilReport && null != this.UpDataReceived) { soilReport.ChannelType = EChannelType.GPRS; soilReport.ListenPort = this.GetListenPort().ToString(); this.UpDataReceived(null, new UpEventArgs() { RawData = rawData, Value = soilReport }); } } } else { return(false); } return(true); } catch (Exception exp) { System.Diagnostics.Debug.WriteLine("GPRS 数据解析出错 !" + msg + "\r\n" + exp.Message); } return(false); }
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); }
private void DealData() { while (true) { m_semaphoreData.WaitOne(); //阻塞当前线程,知道被其它线程唤醒 // 获取对data内存缓存的访问权 m_mutexListDatas.WaitOne(); List <HDModemDataStruct> dataListTmp = m_listDatas; m_listDatas = new List <HDModemDataStruct>(); //开辟一快新的缓存区 m_mutexListDatas.ReleaseMutex(); for (int i = 0; i < dataListTmp.Count; ++i) { try { HDModemDataStruct dat = dataListTmp[i]; //1获取gprs号码,并根据gprs号码获取站点id //StringBuilder gprsId = new StringBuilder(); //foreach (byte b in dat.m_data_buf) //{ // gprsId.AppendFormat("{0:x2}", b); //} string gprs = System.Text.Encoding.Default.GetString(dat.m_modemId); gprs = gprs.Replace("\0", ""); string sid = Manager.XmlStationData.Instance.GetStationByGprsID(gprs); //1.2 获取ascii原始数据,获取报文头 string data = System.Text.Encoding.Default.GetString(dat.m_data_buf).TrimEnd('\0'); InvokeMessage(data, "原始数据"); data = data.Trim(); if (data.Contains("TRU")) { Debug.WriteLine("接收数据TRU完成,停止计时器"); //m_timer.Stop(); InvokeMessage("TRU " + System.Text.Encoding.Default.GetString(dat.m_modemId), "接收"); if (this.ErrorReceived != null) { this.ErrorReceived.Invoke(null, new ReceiveErrorEventArgs() { // Msg = "TRU " + dat.m_modemId Msg = "TRU " + System.Text.Encoding.Default.GetString(dat.m_modemId) }); } } if (data.Contains("ATE0")) { Debug.WriteLine("接收数据ATE0完成,停止计时器"); //m_timer.Stop(); // InvokeMessage("ATE0", "接收"); if (this.ErrorReceived != null) { this.ErrorReceived.Invoke(null, new ReceiveErrorEventArgs() { Msg = "ATE0" }); } } string result = null; if (data.Contains("$")) { result = data.Substring(data.IndexOf("$"), data.IndexOf("\0")); if (!(result.StartsWith("$") && result.EndsWith("\r\n"))) { InvokeMessage(result + "报文开始符结束符不合法", "接收"); } String dataProtocol = Manager.XmlStationData.Instance.GetProtocolBySId(sid); CReportStruct report = new CReportStruct(); CDownConf downReport = new CDownConf(); if (dataProtocol == "ZYJBX") { Up = new Data.ZYJBX.UpParser(); Down = new Data.ZYJBX.DownParser(); } if (dataProtocol == "RG30") { //回复TRU,确认接收数据 InvokeMessage("TRU " + gprs, "发送"); byte[] bts = new byte[] { 84, 82, 85, 13, 10 }; this.sendHex(gprs.Trim(), bts, (uint)bts.Length, null); Up = new Data.RG30.UpParser(); Down = new Data.RG30.DownParser(); //2.1 如果是RG30,则需获取16进制字符串 StringBuilder adcpMsg = new StringBuilder(); foreach (byte b in dat.m_data_buf) { adcpMsg.AppendFormat("{0:x2}", b); } string temp = adcpMsg.ToString().Trim(); if (temp.Length < 200) { return; } InvokeMessage(temp, "原始数据"); //2.2 获取封装的头部信息 string head = data.Substring(0, 57); int length = int.Parse(data.Substring(53, 4)); //2.3 根据头部信息获取数据类型 HADCP OR VADCP string type = ""; if (head.Contains("HADCP")) { type = "H"; } else if (head.Contains("VADCP")) { type = "V"; } string hdt0 = ""; string vdt0 = ""; //2.4 根据头部信息截图DT0数据 if (type == "H") { hdt0 = temp.Substring(57, length * 2); //hdt0 = temp.Substring(57); //写入DT0文件 Write2File writeClass = new Write2File("hdt0"); Thread t = new Thread(new ParameterizedThreadStart(writeClass.WriteInfoToFile)); t.Start(hdt0 + "\r\n"); //调用dll解析计算 //TODO需要调试 //ReadPD0FileTest(); } else if (type == "V") { vdt0 = temp.Substring(57, length * 2); Write2File writeClass = new Write2File("vdt0"); Thread t = new Thread(new ParameterizedThreadStart(writeClass.WriteInfoToFile)); t.Start(vdt0 + "\r\n"); } return; } //批量传输解析 if (data.Contains("1K")) { var station = FindStationBySID(sid); if (station == null) { throw new Exception("批量传输,站点匹配错误"); } CBatchStruct batch = new CBatchStruct(); InvokeMessage(String.Format("{0,-10} ", "批量传输") + data, "接收"); if (Down.Parse_Flash(result, EChannelType.GPRS, out batch)) { if (this.BatchDataReceived != null) { this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = data }); } } else if (Down.Parse_Batch(result, out batch)) { if (this.BatchDataReceived != null) { this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = data }); } } } //+ 代表的是蒸发报文,需要特殊处理 //数据报文解析 if (result.Contains("1G21") || result.Contains("1G22") || result.Contains("1G23") || result.Contains("1G25") || result.Contains("1G29") || result.Contains("+")) { //回复TRU,确认接收数据 InvokeMessage("TRU " + gprs, "发送"); byte[] bts = new byte[] { 84, 82, 85, 13, 10 }; this.sendHex(gprs.Trim(), bts, (uint)bts.Length, null); //根据$将字符串进行分割 var lists = result.Split('$'); foreach (var msg in lists) { if (msg.Length < 10) { continue; } string plusMsg = "$" + msg.TrimEnd(); bool ret = Up.Parse(plusMsg, out report); if (ret && report != null) { report.ChannelType = EChannelType.GPRS; report.ListenPort = this.GetListenPort().ToString(); report.flagId = gprs; string rtype = report.ReportType == EMessageType.EAdditional ? "加报" : "定时报"; InvokeMessage("gprs号码: " + gprs + " " + String.Format("{0,-10} ", rtype) + plusMsg, "接收"); //TODO 重新定义事件 if (this.UpDataReceived != null) { this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = plusMsg }); } } } } //其他报文 else { Down.Parse(result, out downReport); if (downReport != null) { InvokeMessage(String.Format("{0,-10} ", "下行指令读取参数") + result, "接收"); if (this.DownDataReceived != null) { this.DownDataReceived.Invoke(null, new DownEventArgs() { Value = downReport, RawData = result }); } } } } #region 报文解析部分 //报文解析 直接调用 //if (temp.StartsWith("24") && (temp.Contains("7F7F") || temp.Contains("7f7f")) && (temp.Contains("2A2A") || temp.Contains("2a2a"))) ////if ((temp.Contains("7F7F") || temp.Contains("7f7f")) && (temp.Contains("2A2A") || temp.Contains("2a2a"))) //{ // //1.获取头部信息 // string headInfo = temp.Substring(0, 114); // //2.集合头信息 // //2.1 计算集合头信息总长度 // string setHeadInfoID = temp.Substring(114, 4); // string setHeadInfoMsgLength = temp.Substring(118, 4); // string setHeadInfoStay = temp.Substring(122, 2); // string setHeadInfoNum = temp.Substring(123, 2); // int setHeadInfoLength = 4 + 4 + 2 + 2 + 2 * (Convert.ToInt32(setHeadInfoNum,16)); // string setHeadInof = temp.Substring(114, setHeadInfoLength); // //3.固定头信息 // string fixHeadInfo = temp.Substring(114 + setHeadInfoLength, 118); // //3.1 获取层数信息 // string speedLayersStr = fixHeadInfo.Substring(18, 2); // int speedLayers = Convert.ToInt32(speedLayersStr, 16); // //3.2 获取层厚 // decimal thickness = Convert.ToInt32(fixHeadInfo.Substring(26, 2) + fixHeadInfo.Substring(24, 2), 16); // //3.3 获取盲区 // decimal blindzone = Convert.ToInt32(fixHeadInfo.Substring(30, 2) + fixHeadInfo.Substring(28, 2), 16); // //4.可变头信息 // string variableHeadInfo = temp.Substring(114 + 118 + setHeadInfoLength, 112); // //4.1获取数据时间 // string year = "20" + Convert.ToInt32(variableHeadInfo.Substring(8, 2), 16).ToString(); // string month = Convert.ToInt32(variableHeadInfo.Substring(10, 2), 16).ToString(); // string day = Convert.ToInt32(variableHeadInfo.Substring(12, 2), 16).ToString(); // string hour = Convert.ToInt32(variableHeadInfo.Substring(14, 2), 16).ToString(); // string minute = Convert.ToInt32(variableHeadInfo.Substring(16, 2), 16).ToString(); // string second = Convert.ToInt32(variableHeadInfo.Substring(18, 2), 16).ToString(); // DateTime datatime = new DateTime(int.Parse(year), int.Parse(month), int.Parse(day), int.Parse(hour), int.Parse(minute),int.Parse(second)); // //5.流速数据 // string speedInfo = temp.Substring(114 + 118 + 112 + setHeadInfoLength, speedLayers * 16 + 4); // //6.相关系数 // string coefficientInfo = temp.Substring(114 + 118 + 112 + setHeadInfoLength + speedLayers * 16 + 4, speedLayers * 8 + 4); // //7.回波强度 // string echoIntensityInfo = temp.Substring(114 + 118 + 112 + setHeadInfoLength + speedLayers * 16 + 4 + speedLayers * 8 + 4, speedLayers * 8 + 4); // //5.1 6.1 7.1 解析获得每层的数据 // List<CentityLayerSpeed> layerInfoList = new List<CentityLayerSpeed>(); // int speedFlag = 4; // int cfctFlag = 4; // int echoFlag = 4; // for(int layer=0;i< speedLayers; layer++) // { // speedFlag = layer * 16 + 4; // cfctFlag = layer * 8 + 4; // cfctFlag = layer * 8 + 4; // CentityLayerSpeed layerInfo = new CentityLayerSpeed(); // try // { // layerInfo.layers = layer + 1; // layerInfo.StationID = "6000"; // layerInfo.datatime = datatime; // layerInfo.speed1 = Convert.ToInt32(speedInfo.Substring(speedFlag + 2, 2) + speedInfo.Substring(speedFlag, 2), 16); // layerInfo.speed2 = Convert.ToInt32(speedInfo.Substring(speedFlag + 6, 2) + speedInfo.Substring(speedFlag+4, 2), 16); // layerInfo.speed3 = Convert.ToInt32(speedInfo.Substring(speedFlag + 10, 2) + speedInfo.Substring(speedFlag+8, 2), 16); // layerInfo.speed4 = Convert.ToInt32(speedInfo.Substring(speedFlag + 14, 2) + speedInfo.Substring(speedFlag+12, 2), 16); // layerInfo.cfct1 = Convert.ToInt32(coefficientInfo.Substring(cfctFlag, 2), 16); // layerInfo.cfct2 = Convert.ToInt32(coefficientInfo.Substring(cfctFlag+2, 2), 16); // layerInfo.cfct3 = Convert.ToInt32(coefficientInfo.Substring(cfctFlag+4, 2), 16); // layerInfo.cfct4 = Convert.ToInt32(coefficientInfo.Substring(cfctFlag+6, 2), 16); // layerInfo.echo1 = Convert.ToInt32(echoIntensityInfo.Substring(echoFlag, 2), 16); // layerInfo.echo2 = Convert.ToInt32(echoIntensityInfo.Substring(echoFlag+2, 2), 16); // layerInfo.echo3 = Convert.ToInt32(echoIntensityInfo.Substring(echoFlag+4, 2), 16); // layerInfo.echo4 = Convert.ToInt32(echoIntensityInfo.Substring(echoFlag+6, 2), 16); // layerInfoList.Add(layerInfo); // } // catch(Exception e) // { // } // } // //8.比例因子 // string scaleFactorInfo = temp.Substring(114 + 118 + 112 + setHeadInfoLength + speedLayers * 16 + 4 + speedLayers * 8 + 4 + speedLayers * 8 + 4, 22); // //8.1 相关参数 // List<CEntityAdcpParam> adcpParamList = new List<CEntityAdcpParam>(); // CEntityAdcpParam adcpParam = new CEntityAdcpParam(); // adcpParam.StationID = "6000"; // adcpParam.datatime = datatime; // adcpParam.layers = speedLayers; // adcpParam.thickness = thickness; // adcpParam.blindzone = blindzone; // try // { // adcpParam.cfct = Convert.ToInt32(scaleFactorInfo.Substring(4, 2), 16); // adcpParam.voltage = Convert.ToInt32(scaleFactorInfo.Substring(6, 2), 16); // adcpParam.height1 = Convert.ToInt32(scaleFactorInfo.Substring(8, 2), 16); // adcpParam.height2 = Convert.ToInt32(scaleFactorInfo.Substring(10, 2), 16); // adcpParam.height3 = Convert.ToInt32(scaleFactorInfo.Substring(12, 2), 16); // adcpParam.height4 = Convert.ToInt32(scaleFactorInfo.Substring(14, 2), 16); // adcpParam.v1 = Convert.ToInt32(scaleFactorInfo.Substring(16, 2), 16); // adcpParam.v2 = Convert.ToInt32(scaleFactorInfo.Substring(18, 2), 16); // adcpParam.echo = Convert.ToInt32(scaleFactorInfo.Substring(20, 2), 16); // adcpParamList.Add(adcpParam); // }catch(Exception e) // { // } #endregion //调用计算dll //TODO //11.将数据写入返回数据结构 //CReportStruct report = new CReportStruct(); //report.Stationid = "6000"; //report.Type = "01"; //report.ReportType = EMessageType.EAdditional; //report.StationType = EStationType.EHydrology; //report.ChannelType = EChannelType.GPRS; //report.ListenPort = this.GetListenPort().ToString(); //report.flagId = gprs; //string rtype = report.ReportType == EMessageType.EAdditional ? "加报" : "定时报"; //List<CReportData> datas = new List<CReportData>(); //foreach(CentityLayerSpeed layerInfo in layerInfoList) //{ // CReportData data = new CReportData(); // data.Time = layerInfo.datatime; // data.layer = layerInfo.layers; // data.speed1 = layerInfo.speed1; // data.speed2 = layerInfo.speed2; // data.speed3 = layerInfo.speed3; // data.speed4 = layerInfo.speed4; // data.cfct1 = layerInfo.cfct1; // data.cfct2 = layerInfo.cfct2; // data.cfct3 = layerInfo.cfct3; // data.cfct4 = layerInfo.cfct4; // data.echo1 = layerInfo.echo1; // data.echo2 = layerInfo.echo2; // data.echo3 = layerInfo.echo3; // data.echo4 = layerInfo.echo4; // datas.Add(data); //} //foreach(CEntityAdcpParam param in adcpParamList) //{ // CReportData data = new CReportData(); // data.Time = param.datatime; // data.layers = param.layers; // data.thickness = param.thickness; // data.blindzone = param.blindzone; // data.cfct = param.cfct; // data.voltage = param.voltage; // data.height1 = param.height1; // data.height2 = param.height2; // data.height3 = param.height3; // data.height4 = param.height4; // data.v1 = param.v1; // data.v2 = param.v2; // data.echo = param.echo; // datas.Add(data); //} //if (this.UpDataReceived != null) //{ // this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = temp }); //} } catch (Exception e) { Debug.WriteLine("" + e.Message); } } } }
private bool ParseData(string msg) { // InvokeMessage("协议。。。 ", "进入函数7"); try { string rawData = msg; if (msg.Contains("$")) { string data = string.Empty; if (!ProtocolHelpers.DeleteSpecialChar(msg, out data)) { return(false); } msg = data; string sid = msg.Substring(0, 4); string type = msg.Substring(4, 2); #region 1G if (type == "1G") { // InvokeMessage("1: " + DateTime.Now, "函数"); string reportType = msg.Substring(6, 2); if (reportType == "21" || reportType == "22") // 定时报,加报 { // YAC设备的墒情协议: string stationType = msg.Substring(8, 2); switch (stationType) { // 站类为04时墒情站 05墒情雨量站 06,16墒情水位站 07,17墒情水文站 case "04": case "05": case "06": case "07": case "17": { //CEntitySoilData soilStruct = new CEntitySoilData(); //if (Soil.Parse(msg, out soilStruct)) //{ // soilStruct.ChannelType = EChannelType.GPRS; // //soilStruct.ListenPort = this.GetListenPort().ToString(); // //string temp = soilStruct.ReportType == EMessageType.EAdditional ? "加报" : "定时报"; // //InvokeMessage(String.Format("{0,-10} ", temp) + rawData, "接收"); // // 抛出YAC设备墒情事件 // if (null != this.SoilDataReceived) // this.SoilDataReceived.Invoke(null, new YACSoilEventArg() // { // RawData = rawData, // Value = soilStruct // }); //} CEntitySoilData soil = new CEntitySoilData(); CReportStruct soilReport = new CReportStruct(); if (Soil.Parse(rawData, out soil, out soilReport)) { soil.ChannelType = EChannelType.GPRS; if (null != this.SoilDataReceived) { this.SoilDataReceived(null, new CEventSingleArgs <CEntitySoilData>(soil)); } //1111gm string temp = soilReport.ReportType == EMessageType.EAdditional ? "加报" : "定时报"; InvokeMessage(String.Format("{0,-10} ", temp) + rawData, "接收"); if (null != soilReport && null != this.UpDataReceived) { soilReport.ChannelType = EChannelType.GPRS; soilReport.ListenPort = this.GetListenPort().ToString(); this.UpDataReceived(null, new UpEventArgs() { RawData = rawData, Value = soilReport }); } } else { //string temp = soilReport.ReportType == EMessageType.EAdditional ? "加报" : "定时报"; InvokeMessage(" " + rawData, "接收"); } } break; // 站类为01,02,03,12,13时,不是墒情站 case "01": case "02": case "03": case "12": case "13": { CReportStruct report = new CReportStruct(); if (Up.Parse(msg, out report)) { report.ChannelType = EChannelType.GPRS; report.ListenPort = this.GetListenPort().ToString(); string temp = report.ReportType == EMessageType.EAdditional ? "加报" : "定时报"; InvokeMessage(String.Format("{0,-10} ", temp) + rawData, "接收"); //by LH 10.05 //读取数据 //ModemDataStruct dat = new ModemDataStruct(); //while (this.GetNextData(out dat)) //{ //uint dtuID = 0; //var gprs = FindGprsByUserid(userID); //if (gprs.FindByID(userID, out dtuID)) //{ // //query = gprs.Down.BuildQuery(stationID, cmds, EChannelType.GPRS); // gprs.SendHex(dtuID, bts); //} //InvokeMessage("TRU,modemId: " + dat.m_modemId, "发送"); // InvokeMessage("bts: " + bts, "发送"); // //InvokeMessage("bts.length: " + (ushort)bts.Length, "发送"); // // 发送回执 //InvokeMessage("3: " + DateTime.Now, "函数"); // } //DTUdll.Instance.SendHex(m, bts, (ushort)bts.Length); //byte[] bts = new byte[] { 84, 82, 85, 13, 10 }; //uint m = 1610637336; //InvokeMessage("2: " + DateTime.Now, "函数"); //DTUdll.Instance.SendHex(m, bts, (ushort)bts.Length); // report.Datas[0].Voltge = Decimal.Parse("111111") * (Decimal)0.01; // InvokeMessage(report.Datas[0].Voltge.ToString(), "接收"); if (this.UpDataReceived != null) { this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = rawData }); } // InvokeMessage("12333", "接收"); } else { //string temp = report.ReportType == EMessageType.EAdditional ? "加报" : "定时报"; InvokeMessage(" " + rawData, "接收"); } } break; case "11": { CReportStruct report = new CReportStruct(); //CReportArtificalWater report = new CReportArtificalWater(); if (Up.Parse_1(msg, out report)) { report.ChannelType = EChannelType.GPRS; report.ListenPort = this.GetListenPort().ToString(); report.ReportType = EMessageType.Batch; string temptype = "人工水位"; InvokeMessage(String.Format("{0,-10} ", temptype) + rawData, "接收"); //if (this.UpDataReceived != null) this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = rawData }); } } break; default: break; } } else if (reportType == "23") // 人工流量 { CReportStruct report = new CReportStruct(); if (Up.Parse_2(msg, out report)) { report.ChannelType = EChannelType.GPRS; report.ListenPort = this.GetListenPort().ToString(); report.ReportType = EMessageType.Batch; string temptype = "人工流量"; InvokeMessage(String.Format("{0,-10} ", temptype) + rawData, "接收"); this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = rawData }); } } else if (reportType == "32") // 人工报送水位 { CReportStruct report = new CReportStruct(); report.ChannelType = EChannelType.GPRS; report.ListenPort = this.GetListenPort().ToString(); string temptype = "人工报送水位"; InvokeMessage(String.Format("{0,-10} ", temptype) + rawData, "接收"); WriteToFileClass writeClass = new WriteToFileClass("RGwater"); Thread t = new Thread(new ParameterizedThreadStart(writeClass.WriteInfoToFile)); t.Start("GPRS: " + "长度:" + data.Length + " " + rawData + "\r\n"); string a1 = rawData.Substring(9, 1); string a2 = rawData.Substring(10, 1); if (a1 == "P" || a1 == "H" || a1 == "K" || a1 == "Z" || a1 == "D" || a1 == "T" || a1 == "M" || a1 == "G" || a1 == "Y" || a1 == "F" || a1 == "R") { if (a2 == "A") { if (rawData.Contains("ST")) { WriteToFileClass writeClass1 = new WriteToFileClass("sharewater"); Thread t1 = new Thread(new ParameterizedThreadStart(writeClass1.WriteInfoToFile)); t1.Start("GPRS: " + "长度:" + data.Length + " " + rawData + "\r\n"); } } } } else if (reportType == "53") // 人工报送时段雨量 日雨量 旬雨量 水库水位 蓄水量 入库流量 出库流量 { CReportStruct report = new CReportStruct(); report.ChannelType = EChannelType.GPRS; report.ListenPort = this.GetListenPort().ToString(); string temptype = "人工报送雨量"; InvokeMessage(String.Format("{0,-10} ", temptype) + rawData, "接收"); WriteToFileClass writeClass = new WriteToFileClass("RGRain"); Thread t = new Thread(new ParameterizedThreadStart(writeClass.WriteInfoToFile)); t.Start("GPRS: " + "长度:" + data.Length + " " + rawData + "\r\n"); } else if (reportType == "25") { //CEntitySoilData readSoilStruct = new CEntitySoilData(); //if (Soil.Parse(msg, out readSoilStruct)) //{ // readSoilStruct.ChannelType = EChannelType.GPRS; // //readSoilStruct.ListenPort = this.GetListenPort().ToString(); // //string temp = readSoilStruct.ReportType == EMessageType.EAdditional ? "加报" : "定时报"; // //InvokeMessage(String.Format("{0,-10} ", temp) + rawData, "接收"); // // 抛出读墒情事件 // if (null != this.SoilDataReceived) // this.SoilDataReceived.Invoke(null, new YACSoilEventArg() // { // RawData = rawData, // Value = readSoilStruct // }); //} CEntitySoilData soil = new CEntitySoilData(); CReportStruct soilReport = new CReportStruct(); if (Soil.Parse(rawData, out soil, out soilReport)) { soil.ChannelType = EChannelType.GPRS; if (null != this.SoilDataReceived) { this.SoilDataReceived(null, new CEventSingleArgs <CEntitySoilData>(soil)); } if (null != soilReport && null != this.UpDataReceived) { soilReport.ChannelType = EChannelType.GPRS; soilReport.ListenPort = this.GetListenPort().ToString(); this.UpDataReceived(null, new UpEventArgs() { RawData = rawData, Value = soilReport }); } } } else // 下行指令 { CDownConf downconf = new CDownConf(); if (Down.Parse(msg, out downconf)) { InvokeMessage(String.Format("{0,-10} ", "下行指令读取参数") + rawData, "接收"); if (this.DownDataReceived != null) { this.DownDataReceived.Invoke(null, new DownEventArgs() { Value = downconf, RawData = rawData }); } } } } #endregion #region 1K if (type == "1K") { var station = FindStationBySID(sid); if (station == null) { throw new Exception("批量传输,站点匹配错误"); } //EStationBatchType batchType = station.BatchTranType; //if (batchType == EStationBatchType.EFlash) //{ // CBatchStruct batch = new CBatchStruct(); // if (FlashBatch.Parse(msg, out batch)) // { // InvokeMessage(String.Format("{0,-10} ", "Flash批量传输") + rawData, "接收"); // if (this.BatchDataReceived != null) // this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = rawData }); // } //} //else if (batchType == EStationBatchType.EUPan) //{ // CBatchStruct batch = new CBatchStruct(); // if (UBatch.Parse(msg, out batch)) // { // InvokeMessage(String.Format("{0,-10} ", "U盘批量传输") + rawData, "接收"); // if (this.BatchDataReceived != null) // this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = rawData }); // } //} CBatchStruct batch = new CBatchStruct(); if (FlashBatch.Parse(msg, out batch)) { InvokeMessage(String.Format("{0,-10} ", "批量传输") + rawData, "接收"); if (this.BatchDataReceived != null) { this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = rawData }); } } } #endregion #region 1S if (type == "1S") { CDownConf downconf = new CDownConf(); if (Down.Parse(msg, out downconf)) { InvokeMessage(String.Format("{0,-10} ", "下行指令设置参数") + rawData, "接收"); if (this.DownDataReceived != null) { this.DownDataReceived.Invoke(null, new DownEventArgs() { Value = downconf, RawData = rawData }); } } } #endregion } else if (msg.Contains("#")) { CEntitySoilData soil = new CEntitySoilData(); CReportStruct soilReport = new CReportStruct(); if (Soil.Parse(rawData, out soil, out soilReport)) { soil.ChannelType = EChannelType.GPRS; if (null != this.SoilDataReceived) { this.SoilDataReceived(null, new CEventSingleArgs <CEntitySoilData>(soil)); } if (null != soilReport && null != this.UpDataReceived) { soilReport.ChannelType = EChannelType.GPRS; soilReport.ListenPort = this.GetListenPort().ToString(); this.UpDataReceived(null, new UpEventArgs() { RawData = rawData, Value = soilReport }); } } } else { return(false); } return(true); } catch (Exception exp) { System.Diagnostics.Debug.WriteLine("GPRS 数据解析出错 !" + msg + "\r\n" + exp.Message); } return(false); }
public bool Parse(string msg, out CReportStruct report) { report = new CReportStruct(); List <CReportData> dataList = new List <CReportData>(); try { string data = string.Empty; //去除起始符'$' if (!ProtocolHelpers.DeleteSpecialCharLong(msg, out data)) { return(false); } //站号(4位) string StationId = data.Substring(0, 10).Trim(); //长度:1G string length = data.Substring(10, 4); DateTime recvTime; 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 ); //上下行标志 string type = data.Substring(24, 2); //报类(2位):22-定时报 string reportTypeString = data.Substring(26, 2); //站类 string stationTypeString = data.Substring(28, 2); //水位 string waterStr = data.Substring(30, 6); Decimal?water; if (waterStr.Contains("?")) { water = null; } else { water = Decimal.Parse(waterStr) / 100; } //雨量 string rainStr = data.Substring(36, 4); Decimal?rain; if (rainStr.Contains("?")) { rain = null; } else { rain = Decimal.Parse(rainStr) / 100; } //电压 string voltageStr = data.Substring(40, 4); Decimal?voltage; if (voltageStr.Contains("?")) { voltage = null; } else { voltage = Decimal.Parse(voltageStr) / 100; } //报类 EMessageType reportType; reportType = ProtocolMaps.MessageTypeMap.FindKey(reportTypeString); //站类 EStationType stationType; stationType = ProtocolMaps.StationTypeMap.FindKey(stationTypeString); string allElmtData = data.Substring(44); //1.处理SM100H数据 CReportData speedData = new CReportData(); int flagIndex = allElmtData.IndexOf("@@SM100H"); if (flagIndex >= 0) { int keyLength = int.Parse(allElmtData.Substring(8, 4)); string elmtData = allElmtData.Substring(flagIndex, keyLength); //判定要素1的开始符号和结束符号 if (elmtData.StartsWith("@@SM100H") && elmtData.EndsWith("**")) { elmtData = elmtData.Substring(12, keyLength - 14).Trim(); //判定时差法数据的开始符号和接受符号 if (elmtData.Length == 78) { try { string waterSpeedStr = elmtData.Substring(38, 8); string waterFlowStr = elmtData.Substring(48, 8); string waterFlow2Str = elmtData.Substring(58, 8); //字符串转16进制32位无符号整数 UInt32 waterSpeedInt = Convert.ToUInt32(waterSpeedStr, 16); UInt32 waterFlowInt = Convert.ToUInt32(waterFlowStr, 16); UInt32 waterFlow2Int = Convert.ToUInt32(waterFlow2Str, 16); //IEEE754 字节转换float float waterSpeed = BitConverter.ToSingle(BitConverter.GetBytes(waterSpeedInt), 0); float waterFlow = BitConverter.ToSingle(BitConverter.GetBytes(waterFlowInt), 0); float waterFlow2 = BitConverter.ToSingle(BitConverter.GetBytes(waterFlow2Int), 0); //UInt32 x = Convert.ToUInt32(waterFlowStr, 16);//字符串转16进制32位无符号整数 //float fy = BitConverter.ToSingle(BitConverter.GetBytes(x), 0);//IEEE754 字节转换float //UInt32 x2 = Convert.ToUInt32(waterFlow2Str, 16);//字符串转16进制32位无符号整数 //float fy2 = BitConverter.ToSingle(BitConverter.GetBytes(x2), 0);//IEEE754 字节转换float speedData.v1 = (decimal?)waterSpeed; speedData.Q = (decimal?)waterFlow; speedData.Q2 = (decimal?)waterFlow2; speedData.Voltge = voltage; dataList.Add(speedData); } catch (Exception e) { return(false); } } else { return(false); //11开头 99结束 } report.Stationid = StationId; report.Type = type; report.ReportType = reportType; report.StationType = stationType; report.RecvTime = recvTime; report.Datas = dataList; } else { return(false); //TODO 要素1开始符号和结束符合不匹配 } } } catch (Exception eee) { return(false); } return(true); }
private void Parser_3(object str) { try { string data = str as string; /* 删除 '\r\n' 字符串 */ while (data.StartsWith("\r\n")) { data = data.Substring(2); } /* * 解析数据,获取CGSMStruct */ var gsm = new CGSMStruct(); if (!GsmHelper.Parse_3(data, out gsm)) { return; } /* 如果解析成功,触发GSM数据接收完成事件 */ //string rawdata = ""; //string rawdata1 = gsm.Message; //if (!rawdata1.Contains('$')) //{ // rawdata = "$" + rawdata1; //} if (data.EndsWith("+")) { data = data.Substring(0, data.Length - 1); } InvokeMessage(data, "接收"); string msg = string.Empty; if (!ProtocolHelpers.DeleteSpecialChar(gsm.Message, out msg)) { return; } msg = gsm.Message; string rawdata = msg; if (msg.Contains('$')) { msg = msg.Substring(1); } //if (msg.Contains('9')) //{ // msg = msg.Substring(1); //} if (!msg.ToUpper().Contains("TRU")) { string sid = msg.Substring(0, 4); string type = msg.Substring(4, 2); /* * 上行指令信息, * 或者读取参数返回的信息 */ #region 1G if (type == "1G") { string reportType = msg.Substring(6, 2); /* 定时报,加报 */ if (reportType == "21" || reportType == "22") { // YAC设备的墒情协议: string stationType = msg.Substring(8, 2); switch (stationType) { // 站类为04时墒情站 05墒情雨量站 06,16墒情水位站 07,17墒情水文站 case "04": case "05": case "06": case "07": //case "17": // { // CEntitySoilData soil = new CEntitySoilData(); // CReportStruct soilReport = new CReportStruct(); // Soil = new Protocol.Data.Lib.SoilParser(); // if (Soil.Parse(rawdata, out soil, out soilReport)) // { // soil.ChannelType = EChannelType.GSM; // if (null != this.SoilDataReceived) // this.SoilDataReceived(null, new CEventSingleArgs<CEntitySoilData>(soil)); // //1111gm // string temp = soilReport.ReportType == EMessageType.EAdditional ? "加报" : "定时报"; // //InvokeMessage(String.Format("{0,-10} ", temp) + rawdata, "接收"); // if (null != soilReport && null != this.UpDataReceived) // // if (null != soilReport) // { // soilReport.ChannelType = EChannelType.GSM; // soilReport.ListenPort = "COM" + this.ListenPort.PortName; // this.UpDataReceived(null, new UpEventArgs() { RawData = rawdata, Value = soilReport }); // } // } // else // { // //string temp = soilReport.ReportType == EMessageType.EAdditional ? "加报" : "定时报"; // InvokeMessage(" " + rawdata, "接收"); // } // } break; // 站类为01,02,03,12,13时,不是墒情站 case "01": case "02": case "03": case "12": case "13": { CReportStruct report = new CReportStruct(); if (Up.Parse(msg, out report)) /* 解析成功 */ { report.ChannelType = EChannelType.GSM; report.ListenPort = "COM" + this.ListenPort.PortName; report.flagId = gsm.PhoneNumber; if (this.UpDataReceived != null) { this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = data }); } } } break; default: break; } } else if (reportType == "11") // 人工水位 { } else if (reportType == "23") // 人工流量 { } else if (reportType == "25") { } else /* 下行指令 */ { CDownConf downconf = new CDownConf(); if (Down.Parse(msg, out downconf)) /* 解析成功 */ { if (this.DownDataReceived != null) { this.DownDataReceived.Invoke(null, new DownEventArgs() { Value = downconf, RawData = data }); } } } } #endregion #region 1K if (type == "1K") /* 批量传输 */ { var station = FindStationBySID(sid); if (station == null) { throw new Exception("批量传输,站点传输类型匹配错误"); } // EStationBatchType batchType = station.BatchTranType; //if (batchType == EStationBatchType.EFlash) /* Flash传输 */ //{ CBatchStruct batch = new CBatchStruct(); if (FlashBatch.Parse(gsm.Message, out batch)) /* 解析成功 */ { if (this.BatchDataReceived != null) { this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = data }); } } //} //else if (batchType == EStationBatchType.EUPan) /* U盘传输 */ //{ // CBatchStruct batch = new CBatchStruct(); // if (UBatch.Parse(gsm.Message, out batch)) /* 解析成功 */ // { // if (this.BatchDataReceived != null) // this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = data }); // } //} else { throw new Exception("批量传输,站点传输类型匹配错误"); } } #endregion #region 1S if (type == "1S") /* 远地下行指令,设置参数 */ { CDownConf downconf = new CDownConf(); if (Down.Parse(gsm.Message, out downconf))/* 解析成功 */ { if (this.DownDataReceived != null) { this.DownDataReceived.Invoke(null, new DownEventArgs() { Value = downconf, RawData = data }); } } } #endregion } else { if (this.ErrorReceived != null) { this.ErrorReceived.Invoke(null, new ReceiveErrorEventArgs() { Msg = data }); } } } catch (Exception exp) { Debug.WriteLine(exp.Message); } }
private void DealData() { while (true) { try { m_semephoreData.WaitOne(); Thread.Sleep(1000); string data = string.Empty; string rawdata = System.Text.Encoding.ASCII.GetString(m_inputBuffer.ToArray()); WriteToFileClass writeClass = new WriteToFileClass("ReceivedLog"); Thread t = new Thread(new ParameterizedThreadStart(writeClass.WriteInfoToFile)); t.Start("GPRS: " + "长度:" + rawdata.Length + " " + rawdata + "\r\n"); //InvokeMessage(rawdata, "原始数据"); if (rawdata == null || rawdata == "") { continue; } if (!rawdata.EndsWith(";") && !rawdata.Contains(";") && !rawdata.Contains(";") && !rawdata.Contains(";")) { InvokeMessage("未包含结束符号", "输出"); continue; } m_inputBuffer.Clear(); string temp = rawdata.Trim(); string result = string.Empty; //InvokeMessage(temp, "原始数据"); //TODO 判定结束符 if (rawdata.Contains("$")) { data = rawdata; string[] dataList = data.Split('$'); //上行报文接收需回复TRU //数据解析 for (int i = 0; i < dataList.Count(); i++) { string oneGram = dataList[i]; CReportStruct report = new CReportStruct(); Protocol.Data.ZFXY.UpParse up1 = new Data.ZFXY.UpParse(); //UpPaser up = new UpPaser(); if (up1.Parse(oneGram, out report)) { report.ChannelType = EChannelType.Cable; report.ListenPort = "COM" + this.Port.PortName; if (this.UpDataReceived != null) { InvokeMessage(oneGram, "[CABLE]接收"); this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = oneGram }); } } } } } catch (Exception exp) { Debug.WriteLine(exp.Message); m_inputBuffer.Clear(); //InvokeMessage(rawdata, "接收"); } }//end of while }
public bool Parse_beidou(string sid, EMessageType type, string msg, out CReportStruct upReport) { throw new NotImplementedException(); }
/// <summary> /// #S1209110500705141749 000.87001.06000.98000.76000.82 /// #S1209110500705141755 000.67000.96000.90 /// </summary> /// <param name="rawStr"></param> /// <param name="soil"></param> /// <returns></returns> /// public bool Parse(string resp, out CEntitySoilData soil, out CReportStruct report) { soil = null; report = null; report = new CReportStruct(); try { string rawStr = resp.Trim(); if (rawStr.StartsWith("#S1")) { soil = new CEntitySoilData(); // 终端机号 soil.StrDeviceNumber = rawStr.Substring(3, 8); // 数据采集时间 int year = Int32.Parse("20" + rawStr.Substring(11, 2)); int month = Int32.Parse(rawStr.Substring(13, 2)); int day = Int32.Parse(rawStr.Substring(15, 2)); int hour = Int32.Parse(rawStr.Substring(17, 2)); int minute = Int32.Parse(rawStr.Substring(19, 2)); soil.DataTime = new DateTime(year, month, day, hour, minute, 0); // #S1209110500705141749 000.87001.06000.98000.76000.82 if (rawStr.Length.Equals(22 + 30)) { soil.Voltage10 = float.Parse(rawStr.Substring(22, 6)); soil.Voltage20 = float.Parse(rawStr.Substring(28, 6)); soil.Voltage30 = float.Parse(rawStr.Substring(34, 6)); soil.Voltage40 = float.Parse(rawStr.Substring(40, 6)); soil.Voltage60 = float.Parse(rawStr.Substring(46, 6)); } // #S1209110500705141755 000.67000.96000.90 if (rawStr.Length.Equals(22 + 18)) { soil.Voltage10 = float.Parse(rawStr.Substring(22, 6)); soil.Voltage20 = float.Parse(rawStr.Substring(28, 6)); soil.Voltage40 = float.Parse(rawStr.Substring(34, 6)); } return(true); } else if (rawStr.StartsWith("$") && "1G" == rawStr.Substring(5, 2)) { soil = new CEntitySoilData(); string StationID = rawStr.Substring(1, 4); soil.StationID = StationID; // $70521G25142523453124 string cmd = rawStr.Substring(7, 2); if ("25" == cmd) { var now = DateTime.Now; soil.DataTime = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, 0); // $70521G25142523453124 if (21 == rawStr.Length) { soil.Voltage10 = float.Parse(rawStr.Substring(9, 4)) / 100; soil.Voltage20 = float.Parse(rawStr.Substring(13, 4)) / 100; soil.Voltage40 = float.Parse(rawStr.Substring(17, 4)) / 100; } else if (29 == rawStr.Length)// $70521G2514252345312412345678 { soil.Voltage10 = float.Parse(rawStr.Substring(9, 4)) / 100; soil.Voltage20 = float.Parse(rawStr.Substring(13, 4)) / 100; soil.Voltage30 = float.Parse(rawStr.Substring(17, 4)) / 100; soil.Voltage40 = float.Parse(rawStr.Substring(21, 4)) / 100; soil.Voltage60 = float.Parse(rawStr.Substring(25, 4)) / 100; } else { return(false); } return(true); } else if ("22" == cmd || "21" == cmd) { // $70521G2217yymmddhhmm12345600011256000100020003 EStationType stationType = EStationType.EHydrology; decimal water = decimal.Parse(rawStr.Substring(21, 6)) / 100; decimal rain = decimal.Parse(rawStr.Substring(27, 4)); decimal volegate = decimal.Parse(rawStr.Substring(31, 4)) / 100; CReportData reportData = new CReportData(); switch (rawStr.Substring(9, 2)) { case "04": stationType = EStationType.ESoil; if ("21" == cmd) { report.ReportType = EMessageType.EAdditional; } if ("22" == cmd) { report.ReportType = EMessageType.ETimed; } break; case "05": stationType = EStationType.ESoilRain; reportData.Rain = rain; reportData.Voltge = volegate; break; case "06": case "16": stationType = EStationType.ESoilWater; reportData.Water = water; reportData.Voltge = volegate; break; case "07": case "17": reportData.Rain = rain; reportData.Water = water; reportData.Voltge = volegate; stationType = EStationType.ESoilHydrology; break; default: throw new Exception(); break; } int year = Int32.Parse("20" + rawStr.Substring(11, 2)); int month = Int32.Parse(rawStr.Substring(13, 2)); int day = Int32.Parse(rawStr.Substring(15, 2)); int hour = Int32.Parse(rawStr.Substring(17, 2)); int minute = Int32.Parse(rawStr.Substring(19, 2)); DateTime collectTime = new DateTime(year, month, day, hour, minute, 0); soil.DataTime = collectTime; //gm 20161030 下1行 soil.DVoltage = volegate; if (47 == rawStr.Length) { // $7052 1G2217 yymmddhhmm 123456 0001 1256 0001 0002 0003 soil.Voltage10 = float.Parse(rawStr.Substring(35, 4)) / 100; soil.Voltage20 = float.Parse(rawStr.Substring(39, 4)) / 100; soil.Voltage40 = float.Parse(rawStr.Substring(43, 4)) / 100; // report = new CReportStruct(); report.Datas = new List <CReportData>(); report.Stationid = StationID; report.StationType = stationType; report.RecvTime = DateTime.Now; reportData.Time = DateTime.Now; report.Datas.Add(reportData); } else if (47 + 8 == rawStr.Length) { // $70521G2217yymmddhhmm123456 0001 1256 0001 0002 0003 0004 0005 soil.Voltage10 = float.Parse(rawStr.Substring(35, 4)) / 100; soil.Voltage20 = float.Parse(rawStr.Substring(39, 4)) / 100; soil.Voltage30 = float.Parse(rawStr.Substring(43, 4)) / 100; soil.Voltage40 = float.Parse(rawStr.Substring(47, 4)) / 100; soil.Voltage60 = float.Parse(rawStr.Substring(51, 4)) / 100; report = new CReportStruct(); report.Stationid = StationID; report.StationType = stationType; report.RecvTime = DateTime.Now; reportData.Time = DateTime.Now; report.Datas = new List <CReportData>(); report.Datas.Add(reportData); } else { return(false); } return(true); } } } catch (Exception exp) { } 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 DealData(string msg, string reportType, out CReportStruct report) { EStationType type = new EStationType(); report = new CReportStruct(); try { string data = msg; // 丢弃流水号(4位) data = data.Substring(4); // 丢弃发报时间(12位) DateTime sendTime = new DateTime( year: int.Parse("20" + data.Substring(0, 2)), month: int.Parse(data.Substring(2, 2)), day: int.Parse(data.Substring(4, 2)), hour: int.Parse(data.Substring(6, 2)), minute: int.Parse(data.Substring(8, 2)), second: int.Parse(data.Substring(10, 2)) ); //删除发送时间 data = data.Substring(12); //删除ST 站号引导符号 data = data.Substring(2); //删除空格 data = data.Substring(1); //获取站点ID string stationId = data.Substring(0, 10); //删除站点ID data = data.Substring(10); //删除空格 data = data.Substring(1); string stationTypeString = data.Substring(0, 1); if (stationTypeString == "H") { type = EStationType.EH; } if (stationTypeString == "P") { type = EStationType.ERainFall; } //删除站点类型 data = data.Substring(1); // report中的数据初始化 List <CReportData> datas = new List <CReportData>(); TimeSpan span = new TimeSpan(0); DateTime dataTime = sendTime; //int dataAccuracyLength; //int dataLength; //Decimal dataAccuracy; //Decimal dayRain; Nullable <Decimal> diffRain = null; Nullable <Decimal> totalRain = null; Nullable <Decimal> waterStage = null; Nullable <Decimal> voltage = null; //string dataDefine, dataDefine1, dataDefine2; //int flag = 0; if (data.Length >= 10) { string[] dataArr = Regex.Split(data, "TT", RegexOptions.IgnoreCase); for (int i = 0; i < dataArr.Length; i++) { string oneGram = dataArr[i].Trim(); List <decimal> rainList = new List <decimal>(); List <decimal> waterList = new List <decimal>(); if (oneGram.Length < 10) { continue; } dataTime = new DateTime( year: int.Parse("20" + oneGram.Substring(0, 2)), month: int.Parse(oneGram.Substring(2, 2)), day: int.Parse(oneGram.Substring(4, 2)), hour: int.Parse(oneGram.Substring(6, 2)), minute: int.Parse(oneGram.Substring(8, 2)), second: 0 ); //观测时间引导符 if (oneGram.Contains("TT")) { } if (oneGram.Contains("RGZS")) { } if (oneGram.Contains("PIC")) { } if (oneGram.Contains("DRP")) { int index = oneGram.IndexOf("DRP"); string rainListStr = oneGram.Substring(index + 4, 24).Trim(); for (int j = 0; j < 12; j++) { rainList.Add((System.Int32.Parse(rainListStr.Substring(j * 2, 2), System.Globalization.NumberStyles.HexNumber)) * (decimal)(0.1)); } } if (oneGram.Contains("DRZ")) { } if (oneGram.Contains("DRZ1")) { int index = oneGram.IndexOf("DRZ1"); string waterListStr = oneGram.Substring(index + 5, 48).Trim(); for (int j = 0; j < 12; j++) { waterList.Add((System.Int32.Parse(waterListStr.Substring(j * 4, 4), System.Globalization.NumberStyles.HexNumber)) * (decimal)0.01); } } if (oneGram.Contains("DATA")) { } if (oneGram.Contains("AC")) { } if (oneGram.Contains("AI")) { } if (oneGram.Contains("C")) { } if (oneGram.Contains("DRxnn")) { } if (oneGram.Contains("DT")) { } if (oneGram.Contains("ED")) { } if (oneGram.Contains("EJ")) { } if (oneGram.Contains("FL")) { } if (oneGram.Contains("GH")) { } if (oneGram.Contains("GN")) { } if (oneGram.Contains("GS")) { } if (oneGram.Contains("GT")) { } if (oneGram.Contains("GTP")) { } if (oneGram.Contains("H")) { } if (oneGram.Contains("HW")) { } if (oneGram.Contains("M10")) { } if (oneGram.Contains("M20")) { } if (oneGram.Contains("M30")) { } if (oneGram.Contains("M40")) { } if (oneGram.Contains("M50")) { } if (oneGram.Contains("M60")) { } if (oneGram.Contains("M80")) { } if (oneGram.Contains("M100")) { } if (oneGram.Contains("MST")) { } if (oneGram.Contains("P1")) { } if (oneGram.Contains("P2")) { } if (oneGram.Contains("P3")) { } if (oneGram.Contains("P6")) { } if (oneGram.Contains("P12")) { } //日降水量 if (oneGram.Contains("PD")) { } //当前降水量 if (oneGram.Contains("PJ")) { int index = oneGram.IndexOf("PJ"); string endStr = oneGram.Substring(index).Trim(); string[] endStrList = endStr.Split(' '); string diffRainString = endStrList[1]; try { diffRain = Decimal.Parse(diffRainString); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("规约协议数据截取错误" + e.ToString()); } } //1分钟时段降水量 if (oneGram.Contains("PN01")) { } //5分钟时段降水量 if (oneGram.Contains("PN05")) { } //10分钟时段降水量 if (oneGram.Contains("PN10")) { } //30分钟时段降水量 if (oneGram.Contains("PN30")) { } //暴雨量 if (oneGram.Contains("PR")) { } if (oneGram.Contains("PT")) { int index = oneGram.IndexOf("PT"); string endStr = oneGram.Substring(index).Trim(); string[] endStrList = endStr.Split(' '); string totalRainString = endStrList[1]; try { totalRain = Decimal.Parse(totalRainString); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("规约协议数据截取错误" + e.ToString()); } } //瞬时河道水位、潮位 if (oneGram.Contains("Z ")) { int index = oneGram.IndexOf("Z "); string endStr = oneGram.Substring(index).Trim(); string[] endStrList = endStr.Split(' '); string waterStageString = endStrList[1]; try { waterStage = Decimal.Parse(waterStageString); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("规约协议数据截取错误" + e.ToString()); } } //电压 if (oneGram.Contains("VT")) { int index = oneGram.IndexOf("VT"); string endStr = oneGram.Substring(index).Trim(); string[] endStrList = endStr.Split(' '); string voltageString = endStrList[1]; try { voltage = Decimal.Parse(voltageString); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("规约协议数据截取错误" + e.ToString()); } } //定时报和均匀时段报处理 // 雨量组 和 水位组 均有数据 且数量相等。 if (rainList != null && rainList.Count > 0 && waterList != null && rainList.Count == waterList.Count) { CReportData oneData = new CReportData(); for (int k = 0; k < rainList.Count; k++) { DateTime tmpDate = dataTime.AddMinutes(5 * k); oneData.Time = tmpDate; oneData.DiffRain = rainList[k]; oneData.Water = waterList[k]; datas.Add(DeepClone(oneData)); } } // 雨量组数据 和 水位组数据不匹配 else { // if (rainList != null && rainList.Count > 0) { CReportData oneData = new CReportData(); for (int k = 0; k < rainList.Count; k++) { DateTime tmpDate = dataTime.AddMinutes(5 * k); oneData.Time = tmpDate; oneData.DiffRain = rainList[k]; datas.Add(DeepClone(oneData)); } } if (waterList != null && waterList.Count > 0) { CReportData oneData = new CReportData(); for (int k = 0; k < waterList.Count; k++) { DateTime tmpDate = dataTime.AddMinutes(5 * k); oneData.Time = tmpDate; oneData.Water = waterList[k]; datas.Add(DeepClone(oneData)); } } } //普通报文处理 if (diffRain != null || totalRain != null || voltage != null) { CReportData oneData = new CReportData(); oneData.Time = dataTime; oneData.Rain = totalRain; oneData.DiffRain = diffRain; oneData.Water = waterStage; oneData.Voltge = voltage; datas.Add(DeepClone(oneData)); } } } #region 废码代码 //while (data.Length >= 2) //{ // // 截取要素标识符 // string sign = data.Substring(0, 2); // flag = flag + 1; // if(flag > 100) // { // break; // } // // 根据要素标识符取数据 // switch (sign) // { // case "ST": // // 丢弃标识符 // data = data.Substring(2); // // 丢弃一个字节 // data = data.Substring(1); // // 丢弃一个测站地址 // data = data.Substring(10); // // 丢弃一个字节 // data = data.Substring(1); // // 遥测站分类码,不确定是不是一定在这个后面 // string stationTypeString = data.Substring(0,1); // if(stationTypeString == "H") // { // type = EStationType.EH; // } // if(stationTypeString == "P") // { // type = EStationType.ERainFall; // } // data = data.Substring(1); // //type = stationTypeString == "50" ? EStationType.ERainFall : EStationType.EHydrology; // data = data.Substring(1); // break; // case "TT": // // 丢弃标识符 // data = data.Substring(2); // //丢弃一个字节 // data = data.Substring(1); // dataTime = new DateTime( // year: int.Parse("20" + data.Substring(0, 2)), // month: int.Parse(data.Substring(2, 2)), // day: int.Parse(data.Substring(4, 2)), // hour: int.Parse(data.Substring(6, 2)), // minute: int.Parse(data.Substring(8, 2)), // second: 0 // ); // data = data.Substring(10); // //丢弃一个字节 // data = data.Substring(1); // break; // case "PD": // // 丢弃标识符 // data = data.Substring(2); // // 丢弃数据定义19 3个字节、精度1 // dataDefine1 = Convert.ToString(int.Parse(data.Substring(0, 1)), 2); // dataDefine2 = Convert.ToString(int.Parse(data.Substring(1, 1)), 2); // dataDefine = dataDefine1.PadLeft(4, '0') + dataDefine2.PadLeft(4, '0'); // dataLength = Convert.ToInt32(dataDefine.Substring(0, 5), 2) * 2; // dataAccuracyLength = Convert.ToInt32(dataDefine.Substring(5, 3), 2); // dataAccuracy = 1; // while (dataAccuracyLength > 0) // { // dataAccuracy *= (decimal)0.1; // dataAccuracyLength--; // } // data = data.Substring(2); // string dayRainString = data.Substring(0, dataLength); // data = data.Substring(dataLength); // try // { // dayRain = Decimal.Parse(dayRainString) * dataAccuracy; // } // catch (Exception e) // { // System.Diagnostics.Debug.WriteLine("规约协议数据截取错误" + e.ToString()); // } // break; // case "PJ": // // 丢弃标识符 // data = data.Substring(2); // // 丢掉一个字节 // data = data.Substring(1); // string[] rainArr = data.Split('.'); // int rainLen = rainArr[0].Length; // string rainStr = data.Substring(0,rainLen + 2); // data = data.Substring(rainLen + 2); // data = data.Substring(1); // // 丢弃数据定义19 3个字节、精度1 // string diffRainString = rainStr; // try // { // diffRain = Decimal.Parse(diffRainString); // } // catch (Exception e) // { // System.Diagnostics.Debug.WriteLine("规约协议数据截取错误" + e.ToString()); // } // break; // case "PT": // // 丢弃标识符 // data = data.Substring(2); // // 丢掉一个字节 // data = data.Substring(1); // string[] dataArr = data.Split('.'); // int tmp = dataArr[0].Length; // string totalRain = data.Substring(0,tmp + 2); // data = data.Substring(tmp + 2); // data = data.Substring(1); // // 根据长度精度解析数据 // if (reportType == "31") // { // for (int i = 0; i < 13; i++) // { // try // { // // 数据截取 // string rainString = totalRain; // if (i != 0) // { // dataTime = dataTime + span; // } // Decimal rain = 0; // rain = Decimal.Parse(rainString); // // 数据封包 // bool isExists = false; // if (datas.Count != 0) // { // foreach (var d in datas) // { // if (d.Time == dataTime) // { // isExists = true; // d.Rain = rain; // } // } // } // if (isExists == false) // { // CReportData reportData = new CReportData // { // Rain = rain, // Time = dataTime // }; // datas.Add(reportData); // } // } // catch (Exception e) // { // System.Diagnostics.Debug.WriteLine("规约协议数据截取错误" + e.ToString()); // } // } // } // else if (reportType == "32" || reportType == "33" || reportType == "30") // { // try // { // // 数据截取 // string rainString = totalRain; // Decimal rain = 0; // rain = Decimal.Parse(rainString); // // 数据封包 // bool isExists = false; // if (datas.Count != 0) // { // foreach (var d in datas) // { // if (d.Time == dataTime) // { // isExists = true; // d.Rain = rain; // } // } // } // if (isExists == false) // { // CReportData reportData = new CReportData // { // Rain = rain, // Time = dataTime // }; // datas.Add(reportData); // } // } // catch (Exception e) // { // System.Diagnostics.Debug.WriteLine("规约协议数据截取错误" + e.ToString()); // } // } // break; // case "VT": // // 丢弃标识符 // data = data.Substring(2); // //丢掉一个位置 // data = data.Substring(1); // // 丢弃数据定义?? ?个字节、精度? // string[] voltageArr = data.Split('.'); // int voltageLen = voltageArr[0].Length; // string voltageStr = data.Substring(0,voltageLen + 3); // data = data.Substring(voltageLen + 3); // data = data.Substring(1); // // 根据长度精度解析数据 // if (reportType == "31") // { // for (int i = 0; i < 13; i++) // { // try // { // // 数据截取 // string voltageString = voltageStr; // if (i != 0) // { // dataTime = dataTime + span; // } // Decimal voltage = 0; // voltage = Decimal.Parse(voltageString); // // 数据封包 // bool isExists = false; // if (datas.Count != 0) // { // foreach (var d in datas) // { // if (d.Time == dataTime) // { // isExists = true; // d.Voltge = voltage; // } // } // } // if (isExists == false) // { // CReportData reportData = new CReportData // { // Voltge = voltage, // Time = dataTime // }; // datas.Add(reportData); // } // } // catch (Exception e) // { // System.Diagnostics.Debug.WriteLine("规约协议数据截取错误" + e.ToString()); // } // } // } // else if (reportType == "32" || reportType == "33" ||reportType == "30") // { // try // { // // 数据截取 // string voltageString = voltageStr; // Decimal voltage = 0; // voltage = Decimal.Parse(voltageString); // // 数据封包 // bool isExists = false; // if (datas.Count != 0) // { // foreach (var d in datas) // { // if (d.Time == dataTime) // { // isExists = true; // d.Voltge = voltage; // } // } // } // if (isExists == false) // { // CReportData reportData = new CReportData // { // Voltge = voltage, // Time = dataTime // }; // datas.Add(reportData); // } // } // catch (Exception e) // { // System.Diagnostics.Debug.WriteLine("规约协议数据截取错误" + e.ToString()); // } // } // break; // default: // if (data.StartsWith("Z")) // { // // 丢弃标识符 // data = data.Substring(1); // //丢掉一个字节 // data = data.Substring(1); // string[] dataArr2 = data.Split('.'); // int tmp2 = dataArr2[0].Length; // // 丢弃数据定义23 4个字节、精度3 // string waterStr = data.Substring(0,tmp2 + 4); // data = data.Substring(tmp2 + 4); // data = data.Substring(1); // // 根据长度精度解析数据 // if (reportType == "31") // { // for (int i = 0; i < 13; i++) // { // try // { // // 数据截取 // string waterString = waterStr; // if (i != 0) // { // dataTime = dataTime + span; // } // Decimal water = 0; // water = Decimal.Parse(waterString); // // 数据封包 // bool isExists = false; // if (datas.Count != 0) // { // foreach (var d in datas) // { // if (d.Time == dataTime) // { // isExists = true; // d.Water = water; // } // } // } // if (isExists == false) // { // CReportData reportData = new CReportData // { // Water = water, // Time = dataTime // }; // datas.Add(reportData); // } // } // catch (Exception e) // { // System.Diagnostics.Debug.WriteLine("规约协议数据截取错误" + e.ToString()); // } // } // } // else if (reportType == "32" || reportType == "33" || reportType == "30") // { // try // { // // 数据截取 // string waterString = waterStr; // Decimal water = 0; // water = Decimal.Parse(waterString); // // 数据封包 // bool isExists = false; // if (datas.Count != 0) // { // foreach (var d in datas) // { // if (d.Time == dataTime) // { // isExists = true; // d.Water = water; // } // } // } // if (isExists == false) // { // CReportData reportData = new CReportData // { // Water = water, // Time = dataTime // }; // datas.Add(reportData); // } // } // catch (Exception e) // { // System.Diagnostics.Debug.WriteLine("规约协议数据截取错误" + e.ToString()); // } // } // } // else if (data.StartsWith("DRxnn")) // { // // 丢弃标识符 // data = data.Substring(2); // // 丢弃数据定义18 3个字节、精度0 // data = data.Substring(2); // // 时间步长 // string timeSpanString = data.Substring(0, 6); // data = data.Substring(6); // TimeSpan timeSpan = new TimeSpan(Int32.Parse(timeSpanString.Substring(0, 2)), Int32.Parse(timeSpanString.Substring(2, 2)), Int32.Parse(timeSpanString.Substring(4, 2)), 0); // span = span + timeSpan; // } // break; // } //} #endregion foreach (var d in datas) { if (!d.Rain.HasValue) { d.Rain = -1; } if (!d.Water.HasValue) { d.Water = -20000; } if (d.Voltge <= 0) { d.Voltge = -20; } } report = new CReportStruct { StationType = type, Datas = datas }; return(true); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("数据:" + msg); System.Diagnostics.Debug.WriteLine("规约协议正文解析错误" + e.Message); return(false); } }
// 数据批量主板下行解析 public bool Parse_Batch(String msg, out CBatchStruct batch) { batch = new CBatchStruct(); UpParser up = new UpParser(); CReportStruct report = new CReportStruct(); try { if (up.Parse(msg.Substring(msg.IndexOf("1G29") + 5), out report)) { batch.StationID = report.Stationid; batch.StationType = report.StationType; List <CTimeAndData> datas = new List <CTimeAndData>(); List <CTimeAndData> rains = new List <CTimeAndData>(); List <CTimeAndData> waters = new List <CTimeAndData>(); if (report.StationType == EStationType.ERainFall) { foreach (var d in report.Datas) { CTimeAndData rain = new CTimeAndData(); rain.Time = d.Time; if (d.Rain != -1) { int rainData = (int)(d.Rain); rain.Data = string.Format("{0:D4}", rainData); datas.Add(rain); } } } else if (report.StationType == EStationType.ERiverWater) { foreach (var d in report.Datas) { CTimeAndData water = new CTimeAndData(); water.Time = d.Time; if (d.Water != -20000 && d.Water != -200) { int waterData = (int)(d.Water * 100); water.Data = string.Format("{0:D6}", waterData); datas.Add(water); } } } else if (report.StationType == EStationType.EHydrology) { foreach (var d in report.Datas) { CTimeAndData rain = new CTimeAndData(); rain.Time = d.Time; CTimeAndData water = new CTimeAndData(); water.Time = d.Time; if (d.Water != -20000 && d.Water != -200) { int waterData = (int)(d.Water * 100); water.Data = string.Format("{0:D6}", waterData); waters.Add(water); } if (d.Rain != -1) { int rainData = (int)(d.Rain); rain.Data = string.Format("{0:D4}", rainData); rains.Add(rain); } } } batch.Datas = datas; batch.Rains = rains; batch.Waters = waters; return(true); } } catch (Exception e) { System.Diagnostics.Debug.WriteLine("批量主板异常:" + e); } return(false); }
/// <summary> /// 规约协议上行数据解析 /// </summary> /// <param name="msg"></param> /// <param name="report"></param> /// <returns></returns> public bool Parse(string msg, out CReportStruct report) { report = null; try { if (msg == "") { return(false); } // 数据格式 ////7E7E 已经在通讯协议刨去了 //00 //0060405210 //04D2 //31 //0045 //02 //534E //180410000000 //F1 //28 //0060405210 //48 //F0 //28 //1804092300 //04 //18 //000005 //26 //19 //041130 041130 041130 041130 041130 041130 041130 041130 041130 041130 041130 041130 041130 //03 //0A13 /// 数据格式结束 string data = msg; // 2018-12-11 gaoming 添加 string funcCode = data.Substring(20, 2); //丢弃包头 (4位) //data = data.Substring(4); //丢弃中心站地址(2位) data = data.Substring(2); //遥测站地址(10位)00+8位站号 string id = data.Substring(0, 10); data = data.Substring(10); //丢弃密码(4位) data = data.Substring(4); //功能码即报类(2位) 31、32、33、34 分别是均匀时段报、定时报、加报、小时报 string type = data.Substring(0, 2); data = data.Substring(2); //报文上下文标识及长度(4位) string context = data.Substring(0, 1); string contextLengthString = data.Substring(1, 3); if (context != "0") { // 不是上行报文 return(false); } // 报文正文长度 16进制转10进制(字节数)*2 int contextLength = (int.Parse(contextLengthString, System.Globalization.NumberStyles.AllowHexSpecifier)); data = data.Substring(4); // 丢弃起始符 02 data = data.Substring(1); // 报文数据 string message = data.Substring(0, contextLength); data = data.Substring(contextLength); bool result = DealData(message, type, out report); report.Stationid = id; report.Type = "1G"; report.RecvTime = DateTime.Now; switch (type) { case "2F": break; //链路维持报 case "30": //测试报 report.ReportType = EMessageType.ETest; break; case "31": report.ReportType = EMessageType.EUinform; break; case "32": report.ReportType = EMessageType.ETimed; break; case "33": report.ReportType = EMessageType.EAdditional; break; case "34": report.ReportType = EMessageType.EHour; break; } // 丢弃结束符 03 data = data.Substring(1); // 丢弃校验 data = data.Substring(4); return(result); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("数据:" + msg); System.Diagnostics.Debug.WriteLine("规约协议解析不完整" + e.Message); } return(false); }