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())); } }
public void ClearAllEvent() { SerialPortController.GetInstance().ClearAllEvent(); }
public void ExitTask() { SerialPortController.GetInstance().ClosePort(); }