在c#中连续读取COM端口 [英] Read com ports continuously in c#

查看:99
本文介绍了在c#中连续读取COM端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试在列表框中显示我的com端口数据。我想在Listbox上使用多线程每5秒更新一次com端口数据。但是,显示的数据不会更新,它只读取第一个数据而不读取后续读数。我怎么做?



这是我读取数据的编码:



Hi,

I''m trying to display my com port data in a Listbox. I would like to update the com port data every 5 seconds using multi threading on the Listbox. However, the data displayed is not updated as in, it only read the first data but not the subsequent reading. How do I do that?

Here''s my coding to read the data:

void datareceived(object sender, SerialDataReceivedEventArgs e)
    {
        myDelegate d = new myDelegate(update);
        listBox1.Invoke(d, new object[] { });

    }


    public void update()
    {

        Console.WriteLine("Number of bytes:" + serialPort.BytesToRead); // it shows 155, I would like to receive 125 bytes in every transmission

            while (serialPort.BytesToRead > 0)
            bBuffer.Add((byte)serialPort.ReadByte());
            ProcessBuffer(bBuffer);

    }

    private void ProcessBuffer(List<byte> bBuffer)
    {

        // Create a byte array buffer to hold the incoming data
        byte[] buffer = bBuffer.ToArray();


        // Show the user the incoming data // Display mode
        for (int i = 0; i < buffer.Length; i++)
        {
            listBox1.Items.Add("SP: " + (bBuffer[43].ToString()) + "  " + " HR: " +                        (bBuffer[103].ToString()) + " Time: ");


        }





全部谢谢。



Thanks all.

推荐答案

奇怪的纠缠码。我无法看到您在事件实例 DataRecieved 的调用列表中添加处理程序的位置。为什么要尝试读取事件处理程序中的数据?在接收数据时应该调用它本身?为什么要调用它?调用将导致UI线程中的调用,但您使用阻塞(读取)调用,这不应该在UI线程中执行。这一切都是错的。



另外,实际读取的内容取决于RS-232电缆另一端的设备。你怎么能确定从那一端发送数据?



你知道吗?当线程是一个常见的地方时,我不认为异步API有意义。做一件简单的事情:为串行I / O创建一个单独的线程。在此线程中,您可以执行任何阻塞调用,例如串行端口读取操作。我将使您的代码简单明了,每个线程都是顺序的。更容易实现和调试。



要使用UI,无论如何都需要调用。您不能从非UI线程调用与UI相关的任何内容。相反,您需要使用 Invoke System.Windows.Threading的方法。 Dispatcher (对于Forms或WPF)或 System.Windows.Forms.Control (仅限表单)。



您将在我过去的答案中找到有关其工作原理和代码示例的详细说明:

Control.Invoke()与Control.BeginInvoke() [ ^ ],

使用Treeview扫描仪和MD5的问题 [ ^ ]。



另请参阅有关线程的更多参考资料:

如何让keydown事件在不同的操作上运行vb.net中的线程 [ ^ ],

在启用禁用+多线程后控制事件未触发 [ ^ ]。



-SA
Strange entangled code. I cannot see where you add a handler to an invocation list of the instance of the event DataRecieved. Why are you trying to read data in the event handler which itself should be called when data is received? Why you invoke it? Invocation will cause a call in the UI thread, but you use a blocking (read) call, which you should not do in a UI thread. All this is wrong.

Also, what is actually read depends on the device on the other end of RS-232 cable. How can you be sure the data is sent from that end?

You know what? I don''t think asynchronous APIs makes sense these days when threading is a common place. Do a simple thing: create a separate thread for your serial I/O. In this thread, you can do any blocking calls, such as serial port read operations. I will make your code straightforward, each thread will be sequential. Much easier to implement and debug.

To work with UI, you will need invocation anyway. You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


这篇关于在c#中连续读取COM端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆