private void ComPortSend_DataReceived(object sender, SerialDataReceivedEventArgs e) { // throw new NotImplementedException(); Thread.Sleep(100); byte[] byteRead = new byte[SerialFun.ComPortSend.BytesToRead]; SerialFun.ComPortSend.Read(byteRead, 0, byteRead.Length); string temp = ""; for (int i = 0; i < byteRead.Length; i++) { temp += byteRead[i].ToString("x2"); } Trace.WriteLine("串口收到:" + temp); this.textBox1.BeginInvoke(new Action(() => { textBox1.AppendText(temp + "\n"); })); Data.ReturnStr = temp; if (Data.ReturnStr.Length >= 22) { int pos = Data.ReturnStr.IndexOf("eb90000508"); if (Data.ReturnStr.Substring(pos + 18, 4).ToUpper() == "09D7") { String CmdStr = "1ACF0005F8" + Data.ReturnStr.Substring(pos + 10, 8) + "FC1D";// SerialFun.SendToPort(SerialFun.ComPortSend, CmdStr.ToLower()); MyLog.Info("向串口发送:" + CmdStr.ToLower()); } } }
private void button2_Click(object sender, EventArgs e) { String CmdStr = "1ACF0005F5CDCDCDCDFC1D".ToLower();// SerialFun.SendToPort(SerialFun.ComPortSend, CmdStr); textBox1.AppendText("向串口发送:" + CmdStr + "\n"); }
private void button3_Click(object sender, EventArgs e) { if (Data.ReturnStr.Length >= 22) { int pos = Data.ReturnStr.IndexOf("eb90000508"); if (Data.ReturnStr.Substring(pos + 18, 4).ToUpper() == "09D7") { String CmdStr = "1ACF0005F8" + Data.ReturnStr.Substring(pos + 10, 8) + "FC1D";// SerialFun.SendToPort(SerialFun.ComPortSend, CmdStr.ToLower()); textBox1.AppendText("向串口发送:" + CmdStr.ToLower() + "\n"); } } }
private void button1_Click(object sender, EventArgs e) { if (Data.ReturnStr.Length == 22) { if (Data.ReturnStr.Substring(0, 10).ToUpper() == "EB90000501" && Data.ReturnStr.Substring(18, 4).ToUpper() == "09D7") { String CmdStr = "1ACF0005F1" + Data.ReturnStr.Substring(10, 8) + "FC1D";// SerialFun.SendToPort(SerialFun.ComPortSend, CmdStr.ToLower()); textBox1.AppendText("向串口发送:" + CmdStr.ToLower() + "\n"); } } //String CmdStr = "1ACF0005F1" + Data.ReturnStr.Substring(10, 8) + "FC1D";// //SerialFun.SendToPort(SerialFun.ComPortSend, CmdStr.ToLower()); //textBox1.AppendText("向串口发送:" + CmdStr.ToLower() + "\n"); //MyLog.Info("向串口发送:" + CmdStr.ToLower()); }