private void DataReceivedHandlerB(object sender, SerialDataReceivedEventArgs e) { SerialPort sp = (SerialPort)sender; byte[] data = new byte[sp.BytesToRead]; sp.Read(data, 0, data.Length); data = ByteStuffing.Reverse(data, 2); if (data != null) { string message_temp = ""; if (data[0] == 1) { message_temp = "From COM1"; } else { message_temp = "From COM2"; } data[0] = (byte)' '; foreach (var a in data) { message_temp += (char)a; } recvTextB.Invoke(this.myDelegate, new Object[] { message_temp, "B" }); } }
private void sendButtonB_Click(object sender, EventArgs e) { if (!(comPortB.IsOpen)) { MessageBox.Show("COM Port B not open. Cannot send message."); } else //send message { String message = msgBoxB.Text; byte[] data = new byte[message.Length]; int index = 0; foreach (var ch in message) { data[index++] = (byte)ch; } byte[] newData = ByteStuffing.Direct(data, 1, 2); //comPortB.RtsEnable = true; comPortB.Write(newData, 0, newData.Length); //comPortB.RtsEnable = false; msgBoxB.Text = ""; } }
private void sendButtonA_Click(object sender, EventArgs e) { if (!(comPortA.IsOpen)) { MessageBox.Show("COM Port A not open. Cannot send message."); } else //send message { String message = msgBoxA.Text; byte[] data = new byte[message.Length]; int index = 0; foreach (var ch in message) { data[index++] = (byte)ch; } byte[] newData = ByteStuffing.Direct(data, 2, 1); //comPortA.RtsEnable = true; comPortA.Write(newData, 0, newData.Length); //Thread.Sleep(100); // пауза для корректного завершения работы передатчика //comPortA.RtsEnable = false; msgBoxA.Text = ""; } }