Click here to Skip to main content
15,867,851 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello community,

i have a problem with reading from a device.
how can I read from a serialport continous after opening the port by clicking a button called "receive"? .ReadExisting works fine for me but I need it continously.
Do you have any ideas?

What I have tried:

private void button2_Click(object sender, EventArgs e)
{
textBox2.Text = serialPort1.ReadExisting();
}
Posted
Updated 31-Oct-22 2:33am
v2
Comments
11917640 Member 31-Oct-22 5:59am    
Subscribe to SerialPort DataReceived event.

1 solution

Either:
1) Add a handler to the SerialPort.DataReceived Event (System.IO.Ports) | Microsoft Learn[^] and read it in there.
But ... remember two things:
a) The event fires each time there is data in the port - that doesn't mean "a line of data" - it will fire each time a character arrives. So you can't just assign the data to a sting each time you read it as you will discard all previous characters, probably before your user gets a chance to see them.
b) The event does not fire on the UI thread - so you cannot directly access any controls like TextBoxes: you would need to Invoke them.
Or:
2) Set up a BackgroundWorker thread to read the data, and pass it up to the UI thread via teh progress reporting mechanism for it to display.
 
Share this answer
 

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