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 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(); 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("$"), temp.IndexOf("\0")); string sid = result.Substring(1, 4); string type = result.Substring(5, 2); //if (sid == "\0\0\0\0") //{ // continue; //} ////批量传输解析 //if (type == "1K") //{ // var station = FindStationBySID(sid); // if (station == null) // throw new Exception("批量传输,站点匹配错误"); // CBatchStruct batch = new CBatchStruct(); // if (Down.Parse_Flash(result, out batch)) // { // InvokeMessage(String.Format("{0,-10} ", "批量传输") + temp, "接收"); // if (this.BatchDataReceived != null) // this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = temp }); // } // if (Down.Parse_Batch(result, out batch)) // { // InvokeMessage(String.Format("{0,-10} ", "批量传输") + temp, "接收"); // if (this.BatchDataReceived != null) // this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = temp }); // } //} String dataProtocol = null; if (HdProtocolMap.ContainsKey("HD-GPRS")) { dataProtocol = HdProtocolMap["HD-GPRS"]; } switch (dataProtocol) { //case "LN": // break; //case "SXDZ": // { // CReportStruct report = new CReportStruct(); // CDownConf downReport = new CDownConf(); // Data.SXDZ.UpParser Up1 = new Data.SXDZ.UpParser(); // Data.SXDZ.DownParser down1 = new Data.SXDZ.DownParser(); // //批量传输解析 // if (type == "1K") // { // var station = FindStationBySID(sid); // if (station == null) // throw new Exception("批量传输,站点匹配错误"); // CBatchStruct batch = new CBatchStruct(); // if (down1.Parse_Flash(result, out batch)) // { // InvokeMessage(String.Format("{0,-10} ", "批量传输") + temp, "接收"); // if (this.BatchDataReceived != null) // this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = temp }); // } // //if (down1.Parse_Batch(result, out batch)) // //{ // // InvokeMessage(String.Format("{0,-10} ", "批量传输") + temp, "接收"); // // if (this.BatchDataReceived != null) // // this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = temp }); // //} // } // Up1.Parse(result, out report); // down1.Parse(result, out downReport); // if (report != null) // { // report.ChannelType = EChannelType.GPRS; // report.ListenPort = this.GetListenPort().ToString(); // InvokeMessage(String.Format("{0,-10} ", "") + result, "接收"); // //TODO 重新定义事件 // if (this.UpDataReceived != null) // { // this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = result }); // } // } // if (downReport != null) // { // InvokeMessage(String.Format("{0,-10} ", "下行指令读取参数") + result, "接收"); // if (this.DownDataReceived != null) // this.DownDataReceived.Invoke(null, new DownEventArgs() { Value = downReport, RawData = result }); // } // break; // } //case "HJJBX": // //需要处理新老协议 // //TODO 判断新老协议 // bool isNew = false; // if (isNew) // { // List<CUpReport> reports = new List<CUpReport>(); // Up = new Protocol.Data.HJJBX.UpParser(); // Up.Parse_New(result, out reports); // foreach(var report2 in reports) // { // report2.ChannelType = EChannelType.GPRS; // report2.ListenPort = this.GetListenPort().ToString(); // } // InvokeMessage(String.Format("{0,-10} ","") + result, "接收"); // //TODO 重新定义事件 // if (this.UpDataReceived_new != null) // { // this.UpDataReceived_new.Invoke(null, new UpEventArgs_new() { Value = reports, RawData = result }); // } // } // else // { // List<CUpReport> reports = new List<CUpReport>(); // Up = new Protocol.Data.HJJBX.UpParser(); // Up.Parse_Old(result, out reports); // foreach (var report1 in reports) // { // report1.ChannelType = EChannelType.GPRS; // report1.ListenPort = this.GetListenPort().ToString(); // } // InvokeMessage(String.Format("{0,-10} ", "") + result, "接收"); // //测试代码 // Dictionary<string, HDModemInfoStruct> data11 = new Dictionary<string, HDModemInfoStruct>(); // this.getDTUList(out data11); // //TODO 重新定义事件 // if (this.UpDataReceived_new != null) // { // this.UpDataReceived_new.Invoke(null, new UpEventArgs_new() { Value = reports, RawData = result }); // } // } // break; case "ZYJBX": { CReportStruct report = new CReportStruct(); CDownConf downReport = new CDownConf(); Data.ZYJBX.UpParser Up1 = new Data.ZYJBX.UpParser(); Data.ZYJBX.DownParser down1 = new Data.ZYJBX.DownParser(); //批量传输解析 if (type == "1K") { var station = FindStationBySID(sid); if (station == null) { throw new Exception("批量传输,站点匹配错误"); } CBatchStruct batch = new CBatchStruct(); InvokeMessage(String.Format("{0,-10} ", "批量传输") + temp, "接收"); if (down1.Parse_Flash(result, EChannelType.GPRS, out batch)) { if (this.BatchDataReceived != null) { this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = temp }); } } else if (down1.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("1G25")) { var lists = result.Split('$'); foreach (var msg in lists) { if (msg.Length < 5) { continue; } string plusMsg = "$" + msg; Up1.Parse(plusMsg, out report); if (report != null) { string gprs = System.Text.Encoding.Default.GetString(dat.m_modemId); gprs = gprs.Contains("\0")? gprs.Replace("\0", "") : gprs; 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 }); } InvokeMessage("TRU " + gprs, "发送"); byte[] bts = new byte[] { 84, 82, 85, 13, 10 }; this.sendHex(gprs.Trim(), bts, (uint)bts.Length, null); } } } else { down1.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 }); } } } break; } //case "XYJBX": // { // CReportStruct report = new CReportStruct(); // CDownConf downReport = new CDownConf(); // Data.XYJBX.UpParser Up1 = new Data.XYJBX.UpParser(); // Data.XYJBX.DownParser down1 = new Data.XYJBX.DownParser(); // //批量传输解析 // if (type == "1K") // { // var station = FindStationBySID(sid); // if (station == null) // throw new Exception("批量传输,站点匹配错误"); // CBatchStruct batch = new CBatchStruct(); // //if (down1.Parse_Flash(result, out batch)) // //{ // // InvokeMessage(String.Format("{0,-10} ", "批量传输") + temp, "接收"); // // if (this.BatchDataReceived != null) // // this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = temp }); // //} // //if (down1.Parse_Batch(result, out batch)) // //{ // // InvokeMessage(String.Format("{0,-10} ", "批量传输") + temp, "接收"); // // if (this.BatchDataReceived != null) // // this.BatchDataReceived.Invoke(null, new BatchEventArgs() { Value = batch, RawData = temp }); // //} // } // Up1.Parse(result, out report); // down1.Parse(result, out downReport); // if (report != null) // { // report.ChannelType = EChannelType.GPRS; // report.ListenPort = this.GetListenPort().ToString(); // InvokeMessage(String.Format("{0,-10} ", "") + result, "接收"); // //TODO 重新定义事件 // if (this.UpDataReceived != null) // { // this.UpDataReceived.Invoke(null, new UpEventArgs() { Value = report, RawData = result }); // } // } // if (downReport != null) // { // InvokeMessage(String.Format("{0,-10} ", "下行指令读取参数") + result, "接收"); // if (this.DownDataReceived != null) // this.DownDataReceived.Invoke(null, new DownEventArgs() { Value = downReport, RawData = result }); // } // break; // } default: break; } } //List<CUpReport> reports = new List<CUpReport>(); //Up = new UpParser(); //Up.Parse_New(result, out reports); //获取DTU列表的函数,DTU在线列表测试 //uint amount = this.getDTUAmount(); //HDModemInfoStruct data1= new HDModemInfoStruct(); //this.getDTUByPosition(0, out data1); //Dictionary<string, HDModemInfoStruct> datas = new Dictionary<string, HDModemInfoStruct>(); //this.getDTUList(out datas); if (temp.Contains("BEG")) { Data.ZYJBX.DownParser down1 = new Data.ZYJBX.DownParser(); CSDStruct sd = new CSDStruct(); string gprs = System.Text.Encoding.Default.GetString(dat.m_modemId); gprs = gprs.Replace("\0", ""); //string gprs = dat.m_modemId.ToString(); string id = Manager.XmlStationDataSerializer.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); } } } }
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); }
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); } } } }