private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { CheckForIllegalCrossThreadCalls = false; byte[] a = new byte[1024]; //int Count; int Count = this.serialPort1.Read(a, 0, 50); //string s = serialPort1.ReadLine(); string data = null; int i = 0; for (i = 0; i < 8; i++) { data += a[i].ToString ("x")+ " "; } textBox2.Text = id +" "+ data;a的数据里面有头有尾,头是两个oxaa,尾是一个oxaa,中间数据是要用的
正则表达式匹配 string s 表达式"^oxaaoxaa.*?oxaa$" (^是开头 $是结尾 中间表示任意字符) 如果匹配到了就说明数据完整, 如果有换行注意改写 追问 怎么匹配呢? 追答 string myStr = System.Text.RegularExpressions.Regex.Match(s, "^oxaaoxaa.*?oxaa$" ).Value;