Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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:

C#
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.
Posted
Comments
Sergey Alexandrovich Kryukov 19-Dec-11 2:26am    
"Update" is not clear. Do you want to read from the port every 5 second or write to it?
--SA
fahfahfah 19-Dec-11 2:57am    
Thanks for your reply. Update meaning I'll continuously received(Read) the com port data every 5 seconds. I'm sorry for the strange codes, I'm still new in c#. I'm able to display the first data on the listbox but not the subsequent data from the com port.

1 solution

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
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900