private void RemoteSerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { try { int n = 0; while (n < 18) { if (RemoteSerialPort.IsOpen == false) { return; } n = RemoteSerialPort.BytesToRead; } //开始接收数据 byte[] byteRead = new byte[18]; RemoteSerialPort.Read(byteRead, 0, byteRead.Length); if (byteRead[0] == 0x5A && byteRead[17] == 0x33) { //遥控器数据处理 remote_eta = RemoteDataPrecess(byteRead); } else { RemoteSerialPort.DiscardInBuffer(); } ShiftOffestTxtbox.Text = Convert.ToString(Math.Round(remote_eta, effectiveDecimal)); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); RemoteSerialPort.Close(); } }
private void remoteBtn_Click(object sender, EventArgs e) { if (RemoteSerialPort.IsOpen) { RemoteSerialPort.Close(); } else { //电脑只能外接两个串口,其中一个用于遥控器 string[] ports = SerialPort.GetPortNames(); for (int i = 0; i < ports.Length; i++) { if (ports[i] != serialPort.PortName || serialPort.PortName == null) { RemoteSerialPort.PortName = ports[i]; RemoteSerialPort.BaudRate = 115200; RemoteSerialPort.DataBits = 8; RemoteSerialPort.StopBits = StopBits.One; RemoteSerialPort.Parity = Parity.None; } } RemoteSerialPort.Open(); } remoteBtn.Text = RemoteSerialPort.IsOpen ? "关闭遥控器" : "开启遥控器"; }