繁体   English   中英

Q)c#多串口接收解决方案?

[英]Q) c# Multi serialport receive solution?

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.  
}

在这里输入代码

接收到的数据长度为18个字节。 我通过一个Serialport通信做了很多事情。 如果您已实现两个或多个串行通信,请给我一个提示。

它总共使用四个“ ReceivedEvent”,并使用单独的线程处理数据。 如果使用两个或多个端口,则数据通常会从一个端口发送到另一个端口,并且数据不会损坏或不会接收到数据。

我们尝试使用“ multi-serialport”方法连接总共四个端口。 您希望从每个数字到20 ms的样本顺序接收数据。 每次发生“ serialdatareceivevent”,并且每秒显示一次数据。 我不确定如何纠正它,但请帮助我。 感谢您在这里阅读。 我期待着您的帮助。

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));
    });
}

计时器速度为15ms。 更改为上述来源。 使用计时器接收数据时会出现问题吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM