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); }
// 处理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); InvokeMessage("COUT数据 " + dealMsg, "接收"); 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.XmlStationData.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(); string protocol = Manager.XmlStationData.Instance.GetProtocolBySId(sid); if (protocol == "YNXY") { Up = new Data.YNXY.UpParser(); } else if (protocol == "ZYJBX") { Up = new UpParser(); } if (Up.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; }
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).TrimEnd('\0'); string recvData = data.Trim(); InvokeMessage(data, "原始数据"); string temp = data.Trim(); string gprs = System.Text.Encoding.Default.GetString(dat.m_modemId); gprs = gprs.Replace("\0", ""); string sid = Manager.XmlStationData.Instance.GetStationByGprsID(gprs); string result = null; if (temp.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 (temp.Contains("ATE0")) { Debug.WriteLine("接收数据ATE0完成,停止计时器"); //m_timer.Stop(); // InvokeMessage("ATE0", "接收"); if (this.ErrorReceived != null) { this.ErrorReceived.Invoke(null, new ReceiveErrorEventArgs() { Msg = "ATE0" }); } } if (temp.Contains("$")) { result = temp.Substring(temp.IndexOf("$")); int length = int.Parse(result.Substring(11, 4)); //获取报文长度 if (length > MAX_BUFFER) { continue; } result = result.Substring(0, length); 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 == "RG30") { Up = new Data.RG30.UpParser(); Down = new Data.RG30.DownParser(); } if (dataProtocol == "SM100H") { Up = new Data.SM100H.UpParser(); Down = new Data.SM100H.DownParser(); } //时差法 if (dataProtocol == "TDXY") { Up = new Data.TDXY.UpParser(); Down = new Data.TDXY.DownParser(); } //中游局协议 if (dataProtocol == "ZYJBX") { Up = new Data.ZYJBX.UpParser(); Down = new Data.ZYJBX.DownParser(); } //蒸发协议 if (dataProtocol == "ZFXY") { Up = new Data.ZFXY.UpParse(); Down = new Data.ZFXY.DownParse(); } if (dataProtocol == "EN2B") { Up = new Data.EN2B.UpParser(); Down = new Data.EN2B.DownParser(); } if (dataProtocol == "OBS") { Up = new Protocol.Data.OBS.UpParser(); Down = new Protocol.Data.OBS.DownParser(); } //云南协议 if (dataProtocol == "YNXY") { Up = new Data.YNXY.UpParser(); Down = new Data.YNXY.DownParser(); } //批量传输解析 if (temp.Contains("1K")) { var station = FindStationBySID(sid); if (station == null) { throw new Exception("批量传输,站点匹配错误"); } CBatchStruct batch = new CBatchStruct(); InvokeMessage(String.Format("{0,-10} ", "批量传输") + temp, "接收"); if (Down.Parse_Flash(result, EChannelType.GPRS, out batch)) { if (this.BatchDataReceived != null) { this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = temp }); } } else if (Down.Parse_Batch(result, out batch)) { if (this.BatchDataReceived != null) { this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = temp }); } } } //+ 代表的是蒸发报文,需要特殊处理 //数据报文解析 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 }); } } } } if (temp.Contains("BEG")) { Data.ZYJBX.DownParser down1 = new Data.ZYJBX.DownParser(); CSDStruct sd = new CSDStruct(); string id = Manager.XmlStationData.Instance.GetStationByGprsID(gprs); if (down1.Parse_SD(temp, id, out sd)) { InvokeMessage(String.Format("{0,-10} ", "批量SD传输") + temp, "接收"); if (this.BatchSDDataReceived != null) { this.BatchSDDataReceived.Invoke(null, new BatchSDEventArgs() { Value = sd, RawData = temp }); } } } } catch (Exception e) { Debug.WriteLine("" + e.Message); } } } }
// 处理通信输出 COUT类型数据 public void DealCOUT(string msg) { InvokeMessage("通信输出 " + msg, "接收"); 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; } //TODO //需要处理dealbcd码的走这里 // 解析通信输出中的内容 string content = cout.MsgContent; List <CUpReport> reports = new List <CUpReport>(); //TODO //添加调用代码 try { string rawMsg = content; string sid = null; string dealMsg = ProtocolHelpers.dealBCD(rawMsg); InvokeMessage("COUT数据 " + dealMsg, "接收"); WriteToFileClass writeClass = new WriteToFileClass("ReceivedLog"); Thread t = new Thread(new ParameterizedThreadStart(writeClass.WriteInfoToFile)); t.Start("COUT Message: " + " " + dealMsg + "\r\n"); //InvokeMessage("gm3 " + content, "接收"); //sid需要根据北斗卫星号获取 try { string bdid = cout.SenderAddr; sid = Manager.XmlStationData.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); //InvokeMessage("gm4 " + reportType, "接收"); if (reportType == "21" || reportType == "22") // 定时报,加报 { // YAC设备的墒情协议: string stationType = dealMsg.Substring(2, 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.GPRS; soilReport.ListenPort = "COM" + this.Port.PortName; 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(); string protocol = Manager.XmlStationData.Instance.GetProtocolBySId(sid); if (protocol == "YNXY") { Up = new Data.YNXY.UpParser(); } else if (protocol == "ZYJBX") { Up = new UpParser(); } if (Up.Parse_beidou(sid, type, rawMsg, out report)) { //InvokeMessage("gm6 " + rawMsg, "接收"); //$60131G2201161111040003046112271367 report.ChannelType = EChannelType.BeiDou; report.ListenPort = "COM" + this.Port.PortName; 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 = 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); } }