Exemple #1
0
        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()));
            }
        }
Exemple #2
0
 public void ClearAllEvent()
 {
     SerialPortController.GetInstance().ClearAllEvent();
 }
Exemple #3
0
 public void ExitTask()
 {
     SerialPortController.GetInstance().ClosePort();
 }