byte[] SendCompactMaestroCommand(byte cmd, int respByteCount = 0, byte addr = 0xFF, int data = -1) { byte[] buffer; if (addr == 0xFF) { buffer = new byte[] { cmd } } ; else if (data < 0) { buffer = new byte[] { cmd, addr } } ; else { buffer = new byte[] { cmd, addr, (byte)(data & 0x7F), (byte)((data >> 7) & 0x7F) } }; ComPort.DiscardInBuffer(); ComPort.Write(buffer, 0, buffer.Length); if (respByteCount > 0) { buffer = new byte[respByteCount]; while (ComPort.BytesToRead < respByteCount) { } ComPort.Read(buffer, 0, respByteCount); } return(buffer); } }
private void OnComReceive(object sender, SerialDataReceivedEventArgs e)//接收数据 中断只标志有数据需要读取,读取操作在中断外进行 { if (recStaus) { try { var recBuffer = new byte[ComPort.BytesToRead]; ComPort.Read(recBuffer, 0, recBuffer.Length); ComRec(recBuffer); } catch (Exception ex) { if (ComPort.IsOpen == false) { SerialLostEvent?.Invoke(this, new EventArgs()); } } } else { ComPort.DiscardInBuffer(); } }