Q) c#多串口接收解决方案?
在这里输入代码 接收的数据长度为18字节。 我用一个串口通信做了很多。 如果您实现了两个或更多串行通信,请给我一个提示 它总共使用四个“ReceivedEvent”,并使用单独的线程处理数据。 如果使用两个或多个端口,数据通常从一个端口发送到另一个端口,并且数据不会中断或接收不到数据 我们尝试使用“multi-serialport”方法连接总共四个端口。 您希望按顺序接收从每个数字到20毫秒样本的数据。 每个“SerialDataReceiveEvent”都会发生,数据每秒显示一次。 我不知道如何纠正它,但请帮助我。 谢谢你在这里阅读。 我期待着你的帮助Q) c#多串口接收解决方案?,c#,multithreading,serial-port,C#,Multithreading,Serial Port,在这里输入代码 接收的数据长度为18字节。 我用一个串口通信做了很多。 如果您实现了两个或更多串行通信,请给我一个提示 它总共使用四个“ReceivedEvent”,并使用单独的线程处理数据。 如果使用两个或多个端口,数据通常从一个端口发送到另一个端口,并且数据不会中断或接收不到数据 我们尝试使用“multi-serialport”方法连接总共四个端口。 您希望按顺序接收从每个数字到20毫秒样本的数据。 每个“SerialDataReceiveEvent”都会发生,数据每秒显示一次。 我不知道如
private void Serial_Event_1(object sender, SerialDataReceivedEventArgs e)
{
bytes1 = 0;
if (received_Sequence == 0)
{
SerialPort sData = sender as SerialPort;
var th1 = new Thread(Serial_Event_Thread1);
try
{
bytes1 = sData.BytesToRead;
}
catch { }
byte[] read_data_ = new byte[bytes1];
linef_1 = new byte[bytes1];
try
{
// sData.Read(read_data_, 0, bytes1);
sData.Read(linef_1, 0, bytes2);
// linef_1 = read_data_;
}
catch
{
MessageBox.Show("1 handle error");
}
Invoke((MethodInvoker)delegate
{
th1.Start();
});
if (port_numbering > 1)
{
received_Sequence ++;
}
}
if (port_numbering == 1)
received_Sequence = 0;
}
private void Serial_Event_2(object sender, SerialDataReceivedEventArgs e)
{
//Same as receivedevent 1
}
private void Serial_Event_Thread1()
{
// The incoming data packets are analyzed and written to the richtextbox.
//Even when using two or more serial communication without `enter code here`analyzing packets, only one port can communicate smoothly.
//The `enter code here`other ports are not well received or do not work.
}
计时器速度为15毫秒。
已更改为上述来源。
当我使用计时器接收数据时,是否会遇到问题?请格式化您的代码,并检查这是否是一个简单的示例。谢谢您的回答。正如你所说,代码已经尽可能地简化了。简单地说,一个串行端口通信工作,而两个或更多串行端口通信不工作。请帮助我..为什么不使用SerialPort类中内置的
DataReceived
事件?每个SeriaPort对象都可以有一个。响应已经在它自己的线程上。查看是否使用了多个“DataReceivedEvent”,只有一个“DataReceivedEvent”被激活,并且没有其他事件被激活。因此我尝试为每个“DataReceivedEvent”使用一个线程,但它不起作用。您应该为每个要发送/接收数据的COM端口创建一个SerialPort
类。每个SerialPort
都应该订阅自己的DataReceived
事件。我有几个异步读取/写入串行数据的应用程序,SerialPort
类在自己的线程中处理所有这些数据。
private void Serial_btn_1_Click(object sender, EventArgs e)
{
Test_Timer.Interval = 15;
Test_Timer.Tick += new System.EventHandler(TimerEventProcessor);
Test_Timer.Start();
}
private void TimerEventProcessor(object sender, EventArgs e)
{
try
{
bytes1 = serialPort1.BytesToRead;
bytes1 = serialPort2.BytesToRead;
bytes1 = serialPort3.BytesToRead;
bytes1 = serialPort4.BytesToRead;
linef_1 = new byte[bytes1];
linef_2 = new byte[bytes2];
linef_3 = new byte[bytes3];
linef_4 = new byte[bytes4];
}
catch { }
try
{
serialPort1.Read(linef_1, 0, bytes1);
serialPort2.Read(linef_2, 0, bytes2);
serialPort3.Read(linef_3, 0, bytes3);
serialPort4.Read(linef_4, 0, bytes4);
}
catch
{
// MessageBox.Show("Read_data no");
}
Invoke((MethodInvoker)delegate
{
richTextBox1.AppendText(ByteToHex(linef_1));
richTextBox2.AppendText(ByteToHex(linef_2));
richTextBox3.AppendText(ByteToHex(linef_3));
richTextBox4.AppendText(ByteToHex(linef_4));
});
}