private void Port_DataReceived(object sender, SerialDataReceivedEventArgs e) { int n = ListenPort.BytesToRead; byte[] buf = new byte[n]; ListenPort.Read(buf, 0, n); m_inputBuffer.AddRange(buf); //获取结束符号的位置 var count = (from r in m_inputBuffer where (r == 59) select r).Count(); Debug.WriteLine(count + " ----- " + Encoding.ASCII.GetString(buf)); string tmp = Encoding.ASCII.GetString(m_inputBuffer.ToArray <byte>()); WriteToFileClass writeClass = new WriteToFileClass("ReceivedLog"); Thread t = new Thread(new ParameterizedThreadStart(writeClass.WriteInfoToFile)); t.Start("GPRS: " + "长度:" + tmp.Length + " " + tmp + "\r\n"); if (tmp.Contains("$")) { string data = Encoding.ASCII.GetString(m_inputBuffer.ToArray <byte>()); //判定短信接收的标志符号 if (data.Contains("CMT")) { if (data.EndsWith("\r\n")) { string[] a = new string[] { "CMT" }; //按CMT划分字符串 string[] ArrData = data.Split(a, StringSplitOptions.None); for (int i = 1; i < ArrData.Count(); i++) { //增加一层调用,支持多线程 Thread t1 = new Thread(new ParameterizedThreadStart(parse_Updata)); t1.Start(ArrData[i]); } //如果判定到结束符号,则清空缓冲区 m_inputBuffer.Clear(); } } } }
private void Port_DataReceived_old(object sender, SerialDataReceivedEventArgs e) { int n = ListenPort.BytesToRead; byte[] buf = new byte[n]; ListenPort.Read(buf, 0, n); m_inputBuffer.AddRange(buf); var count = (from r in m_inputBuffer where (r == 59) select r).Count(); Debug.WriteLine(count + " ----- " + Encoding.ASCII.GetString(buf)); string flag = Encoding.ASCII.GetString(m_inputBuffer.ToArray <byte>()); if (flag.Contains("$")) { string data = Encoding.ASCII.GetString(m_inputBuffer.ToArray <byte>()); if (data.Contains("CMT")) { if (data.EndsWith(";")) { string[] a = new string[] { "CMT" }; string[] ArrData = data.Split(a, StringSplitOptions.None); for (int i = 1; i < ArrData.Count(); i++) { Thread t = new Thread(new ParameterizedThreadStart(parse_Updata)); t.Start(ArrData[i]); Debug.WriteLine(ArrData[i]); InvokeMessage(ArrData[i], "GSM接收:"); m_inputBuffer.Clear(); } } } //} } else { // string data = Encoding.ASCII.GetString(m_inputBuffer.ToArray<byte>()).Replace("\n", "").Replace("\t", "").Replace("\r", ""); string data = Encoding.ASCII.GetString(m_inputBuffer.ToArray <byte>()); if (data.Contains("CMT") && data.Contains("1G")) { string data_1 = data.Substring(data.IndexOf("1G")); if (data_1.EndsWith("\r\n")) { string[] a = new string[] { "CMT" }; string[] ArrData = data.Split(a, StringSplitOptions.None); for (int i = 1; i < ArrData.Count(); i++) { Thread t = new Thread(new ParameterizedThreadStart(Parser_2)); t.Start(ArrData[i]); // Parser_2(ArrData[i]); Debug.WriteLine(ArrData[i]); m_inputBuffer.Clear(); } } } else if (data.Contains("CMT") && data.Contains("TRU")) { string data_1 = data.Substring(data.IndexOf("TRU")); if (data_1.EndsWith("\r\n")) { string[] a = new string[] { "CMT" }; string[] ArrData = data.Split(a, StringSplitOptions.None); for (int i = 1; i < ArrData.Count(); i++) { Thread t = new Thread(new ParameterizedThreadStart(Parser_2)); t.Start(ArrData[i]); // Parser_2(ArrData[i]); Debug.WriteLine(ArrData[i]); m_inputBuffer.Clear(); } } } else if (data.Contains("CMT") && data.Contains("\r\n")) { if (data.EndsWith("\r\n")) { InvokeMessage(data, "接收"); m_inputBuffer.Clear(); } } } }