private void SendCommand(object data) { byte[] datas = (byte[])(data); lock (DisposePort) { try { //写入缓存区准备发送 DisposePort.Write(datas, 0, datas.Length); //串口发送命令成功 if (SendCommandEvent != null) { SendCommandEvent.Invoke(true); } } catch (Exception e) { //串口数据发送失败 if (SendCommandEvent != null) { SendCommandEvent.Invoke(false); } } } }
/// <summary> /// 异步发送数据 /// </summary> /// <param name="command"></param> public void SendCommandToMCU(CommandEnum command) { //开始传输数据,则初始化excel模型 if (command == CommandEnum.BeginTrans) { ExcelDatas = new ExcelDatasModel(); //清理缓存 RightNowCollectDR.Clear(); ConsolePage.UnityIns.AddMessage(AConsoleMessage.MessageKindEnum.Important, "开始采集"); CounterForDatas = 0; } if (!DisposePort.IsOpen) { ConsolePage.UnityIns.AddMessage(AConsoleMessage.MessageKindEnum.Error, "串口还未打开"); //串口还未打开,发送命令失败 if (SendCommandEvent != null) { SendCommandEvent.Invoke(false); } return; } byte[] datas = new byte[CommandByteNumber]; for (int counter = 0; counter < 4; counter++) { datas[counter] = StartBytes; } datas[4] = (byte)command; for (int counter = 5; counter < CommandByteNumber; counter++) { datas[counter] = EndBytes; } ThreadPool.QueueUserWorkItem(SendCommand, datas); }
public void SendCommand(int id, byte[] bytes, PackageCompression packageCompression) { SendCommandEvent?.Invoke(this, Tuple.Create(BitConverter.ToUInt32(bytes, 0), bytes.Skip(4).ToArray())); }