/// <summary> /// 仓道串口的回复数据处理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Cdport_DongEvent(object sender, EventArgs e) { Com.newEventStr eventFromCom = (newEventStr)e; byte[] data = (byte[])eventFromCom.tys; BasisEventCommand serialportevent = new BasisEventCommand(); serialportevent.Session = this; serialportevent.EventCommandID = "serial"; serialportevent.Content = data; CdSerialportCmdExecutorchainheader.Execute(serialportevent); }
/// <summary> /// 作为一个委托方法被另外一个线程调用。以达到异步接收的目的 /// 接收的过程中要做2件事 /// 1、监测接收的数据是否完整,如果不完整就继续等待。 /// 2、如果是完整的话 是否对应了命令预期的数据,如果不对应的话 ,重发 /// 3、重发次数是否查过设定?超过了则丢弃。 /// ManualResetEvent的Set()和Reset()方法即上面的功能,将状态分别设成绿灯和红灯。 ///红灯的状态下 WaitOne的线程将停止 /// </summary> /// <param name="c"></param> protected override void OnRxChar(byte c) { try { if (c == 0x25) { recflag = true; } if (recflag) { this.strRecive.Add(c); //if (this.strRecive.Count > 60) //{ // strRecive.Clear(); // recflag = false; //} //if (c == 0x23) //{ if (strRecive.Count > 4) { int startIndex = (int)strRecive[2]; if ((strRecive.Count == startIndex + 3) && strRecive[startIndex + 2] == 0x23) { //if (tools.Crc(strRecive.ToArray(), 3, startIndex) == strRecive[startIndex + 1])//校验码 //{ } newEventStr newsms = new newEventStr(); newsms.EventType = 0x00; newsms.tys = strRecive.ToArray(); TransFlag.Set(); //TransFlag.WaitOne(1000, false); strRecive.Clear(); recflag = false; if (this.DongEvent != null) { DongEvent(this, newsms); } //TransFlag.Set(); ////TransFlag.WaitOne(1000, false); //strRecive.Clear(); //recflag = false; //} //else //{ // strRecive.Clear(); //} } else if (strRecive.Count > 100) { strRecive.Clear(); recflag = false; } // string tempS = Encoding.ASCII.GetString(strRecive.ToArray()); this.flag = true;//回复不正确 //} //else //{ // this.strRecive.Clear(); // recflag = false; //} } } } catch (Exception exception) { } }