/// <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 static bool Parse_3(String data, out CGSMStruct gsm) { gsm = new CGSMStruct(); try { // 解析GSM号码 gsm.PhoneNumber = data.Substring(5, 11); // 解析时间 string time = data.Substring(21, 20); int year = Int32.Parse("20" + time.Substring(0, 2)); // 年 yy int month = Int32.Parse(time.Substring(3, 2)); // 月 mm int day = Int32.Parse(time.Substring(6, 2)); // 日 dd int hour = Int32.Parse(time.Substring(9, 2)); // 小时 hh int minute = Int32.Parse(time.Substring(12, 2)); // 分钟 mm int second = Int32.Parse(time.Substring(15, 2)); // 秒 second gsm.Time = new DateTime(year, month, day, hour, minute, second); string message = data.Substring(42).Trim(); if (message.EndsWith("+")) { message = message.Substring(0, message.Length - 1); } // 解析数据包 //gsm.Message = data.Substring(42).Trim(); gsm.Message = message; return(true); } catch (Exception ex) { } return(false); }
public static bool ParseGsm(String strTime, String strPhone, String data, out CGSMStruct gsm) { gsm = new CGSMStruct(); try { // 解析GSM号码 // gsm.PhoneNumber = data.Substring(9, 11); gsm.PhoneNumber = strPhone; // 解析时间 // string time = data.Substring(24, 20); string time = strTime; int year = Int32.Parse("20" + time.Substring(0, 2)); // 年 yy int month = Int32.Parse(time.Substring(3, 2)); // 月 mm int day = Int32.Parse(time.Substring(6, 2)); // 日 dd int hour = Int32.Parse(time.Substring(9, 2)); // 小时 hh int minute = Int32.Parse(time.Substring(12, 2)); // 分钟 mm int second = Int32.Parse(time.Substring(15, 2)); // 秒 second gsm.Time = new DateTime(year, month, day, hour, minute, second); // 解析数据包 gsm.Message = data.Trim(); return(true); } catch (Exception ex) { } return(false); }
private void DealData() { //InvokeMessage("协议。。。 ", "进入函数6"); while (true) { //阻塞0.05s,防止过快获取数据 Thread.Sleep(50); m_semaphoreData.WaitOne(); //阻塞当前线程,知道被其它线程唤醒 // 获取对data内存缓存的访问权 m_mutexListDatas.WaitOne(); List <string> dataListTmp = m_listDatas; m_listDatas = new List <string>(); //开辟一快新的缓存区 m_mutexListDatas.ReleaseMutex(); // 开始处理数据 for (int i = 0; i < dataListTmp.Count; ++i) { try { string rawGsm = dataListTmp[i]; if (!rawGsm.Contains("1G")) { continue; } Gprs.WriteToFileClass writeClass = new Gprs.WriteToFileClass("ReceivedLog"); Thread t_gsm = new Thread(new ParameterizedThreadStart(writeClass.WriteInfoToFile)); t_gsm.Start("Web-GSM: " + "长度:" + rawGsm.Length + " " + rawGsm + "\r\n"); StringReader Reader = new StringReader(rawGsm); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(Reader); XmlNodeList nodeList = xmlDoc.SelectSingleNode("datalist").ChildNodes; foreach (XmlNode xnf in nodeList) { XmlElement xe = (XmlElement)xnf; string sid = xe.SelectSingleNode("id").InnerText; string timeStr = xe.SelectSingleNode("uptime").InnerText; DateTime recvtime = DateTime.Parse(timeStr); string addr = xe.SelectSingleNode("destaddr").InnerText; string report = xe.SelectSingleNode("content").InnerText; CGSMStruct gsm = new CGSMStruct() { Message = report, PhoneNumber = addr, Time = recvtime }; DealSMSData(addr, gsm); } } catch (Exception ee) { } } } }
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); } }
/// <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); }