public void Excute() { OnPreExecute(); //会调用重写的方法 SerialPortController mSerialPortController = SerialPortController.GetInstance(); //这个类只会new一次 mSerialPortController.PortName = Param.PortName; mSerialPortController.BaudRate = Param.BaudRate; mSerialPortController.Initialization((object sender, EventArgs e) => { SerialPortEventArgs mSerialPortEventArgs = e as SerialPortEventArgs; //通知异常 OnPostExecute(default(T), new Exception(mSerialPortEventArgs.ErrorMessage)); }, (object sender, EventArgs e) => { SerialPortEventArgs mSerialPortEventArgs = e as SerialPortEventArgs; recvByteArray = mSerialPortEventArgs.Data; if (null != MyProtocol && null != mSerialPortEventArgs) { //会调用重写的方法 OnPostExecute(MyProtocol.Decode(mSerialPortEventArgs.Data) as T, null); } else { //通知异常 OnPostExecute(default(T), new Exception("返回数据为空")); } }, MyProtocol); if (RequestProtocol != null && RequestProtocol.GetCommand() != SerialPortConst.COMMAND_EMPTY_VIRTUAL) { mSerialPortController.SendCommand(RequestProtocol.Encode()); Console.WriteLine("-----------------send value-----------------\n" + Util.ToHexString(RequestProtocol.Encode())); } }
// バイト列に変換して送信データに書き込むメソッド private static short SerializeUint(StreamBuffer outStream, object customObject) { uint value = (uint)customObject; int index = 0; lock (bufferUint) { MyProtocol.Serialize(value, bufferUint, ref index); outStream.Write(bufferUint, 0, index); } return((short)index); // 書き込んだバイト数を返す }
// 受信データからバイト列を読み込んで変換するメソッド private static object DeserializeUint(StreamBuffer inStream, short length) { uint value; int index = 0; lock (bufferUint) { inStream.Read(bufferUint, 0, length); MyProtocol.Deserialize(out value, bufferUint, ref index); } return(value); }
void comm_DataReceived(object sender, SerialDataReceivedEventArgs e) { if (Closing) { return; //如果正在关闭,忽略操作,直接返回,尽快的完成串口监听线程的一次循环 } try { Listening = true; //设置标记,说明我已经开始处理数据,一会儿要使用系统UI的。 int n = comm.BytesToRead; //先记录下来,避免某种原因,人为的原因,操作几次之间时间长,缓存不一致 byte[] buf = new byte[n]; //声明一个临时数组存储当前来的串口数据 received_count += n; //增加接收计数 comm.Read(buf, 0, n); //读取缓冲数据 Console.WriteLine(Util.ToHexString(buf)); ///////////////////////////////////////////////////////////////////////////////////////////////////////////// //<协议解析> bool data_catched = false;//缓存记录数据是否捕获到 byte[] binary_data = null; //1.缓存数据 buffer.AddRange(buf); //2.完整性判断 while (buffer.Count >= MyProtocol.MinLength())//至少要包含头(2字节)+长度(1字节)+校验(1字节) { //请不要担心使用>=,因为>=已经和>,<,=一样,是独立操作符,并不是解析成>和=2个符号 //2.1 查找数据头 if (MyProtocol.StartInProtocol(buffer)) { //2.2 探测缓存数据是否有一条数据的字节,如果不够,就不用费劲的做其他验证了 //前面已经限定了剩余长度>=4,那我们这里一定能访问到buffer[2]这个长度 int len = MyProtocol.DataLength(buffer);//数据长度 //数据完整判断第一步,长度是否足够 //len是数据段长度,4个字节是while行注释的3部分长度 if (buffer.Count < len) { break; //数据不够的时候什么都不做 } //这里确保数据长度足够,数据头标志找到,我们开始计算校验 //2.3 校验数据,确认数据正确 //异或校验,逐个字节异或得到校验码 if (!MyProtocol.CheckOK(buffer)) //如果数据校验失败,丢弃这一包数据 { //buffer.RemoveRange(0, buffer.Count);//从缓存中删除错误数据 buffer.RemoveAt(0); continue;//继续下一次循环 } //至此,已经被找到了一条完整数据。我们将数据直接分析,或是缓存起来一起分析 //我们这里采用的办法是缓存一次,好处就是如果你某种原因,数据堆积在缓存buffer中 //已经很多了,那你需要循环的找到最后一组,只分析最新数据,过往数据你已经处理不及时 //了,就不要浪费更多时间了,这也是考虑到系统负载能够降低。 binary_data = new byte[len]; buffer.CopyTo(0, binary_data, 0, len); //复制一条完整数据到具体的数据缓存 data_catched = true; buffer.RemoveRange(0, len); //正确分析一条数据,从缓存中移除数据。 } else { //这里是很重要的,如果数据开始不是头,则删除数据 buffer.RemoveAt(0); } } //分析数据 if (data_catched) { if (_DataReceivedEvent != null) { SerialPortEventArgs mSerialPortEventArgs = new SerialPortEventArgs(); mSerialPortEventArgs.Data = binary_data; _DataReceivedEvent(this, mSerialPortEventArgs); } } //如果需要别的协议,只要扩展这个data_n_catched就可以了。往往我们协议多的情况下,还会包含数据编号,给来的数据进行 //编号,协议优化后就是: 头+编号+长度+数据+校验 //</协议解析> ///////////////////////////////////////////////////////////////////////////////////////////////////////////// } finally { Listening = false;//我用完了,ui可以关闭串口了。 } }