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); } } } }
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 }
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); } } } }