Link to home
Start Free TrialLog in
Avatar of orbitcoms
orbitcoms

asked on

VB.NET Serial communications and multithreading

I have an application that uses the "On Datareceived" event for serial component (VB.NET 2010).

While the program is receiving data from the serial port, if I attempt to move the form while it is receiving I generate a communications error (the reply timer I set up times out).

The program sends a line of text and sets a reply timeout. After timeout, if data is available then it is processed, if not then coms error is indicated.

Though the data return happens in around 30ms (it is single ACK byte), I need to set timer to over 100ms to avoid the movement of the form from interfering with the receive process, but this makes the download very slow.

Is there some way to stop the user being able to move the form around during reception
OR
Some sort of threading arrangement to prevent form movement effecting serial data reception timing
OR
A better way to receive data (I tried to set a longer reply timeout and then inside the Data Received routine setting the timer interval to low value once data arrives so it does not need to wait for the entire timeout period but was not successful.

Thanks in advance for any help or code snippet that shows a better method of handling incoming data.
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

I would think that a BackgroundWorker would be the easiest way to approach a multi-threading process.  I have to say that I don't quite understand the need for the timer...
Avatar of andr_gin
andr_gin

1.) If the SerialPort is created in the same thread as the main Form then the events will also be handled in the same form. If Another event is thrown and the handling of this event takes some time the event of your SerialPort is also delayed.

2.) The SerialPort provides the methods Read(buffer,offset,count) and Write(buffer,offset,count). So you can read/write like a stream.

Warning: Like in a stream the count of the Read method is only a maximum value. If data is received the Read method copies all available bytes into the buffer and returns the number of bytes read. If your connection gets terminated sometimes without a reason then this might be the reason.

3.) In my programs I do not drag the SerialPort on the Form from the toolbox. I create the SerialPort in Code with New System.IO.Ports.SerialPort(PortName,BaudRate...)
Then I start a new communication Thread with New Thread(AddressOf THREAD_SerialCommunication)
The THREAD_SerialCommunication is a Sub that handles the communication with the serial port.
Avatar of orbitcoms

ASKER

Thanks for the useful comment. Would you happen to be able to provide me a snippet of code that shows the setup of the port and datareceived block so that I can understand how it works.

Up until now all my code simply set a timeout and sent a request to the device then after the timeout it looked at what data has been received (adding each incoming received data to a buffer).

Trouble here is the timeout was always set to maximum time and if data returned sooner, the program still waited the entire timeout period. When I attempted to speed up the program without these threads that you use, moving the form during communications reception caused a timeout and invalid data receive.

I also now want to have a system where I can receive data asynchronously without first polling the device (ie. a stream of readings from a test equipment)

thanks in advance

david
ASKER CERTIFIED SOLUTION
Avatar of andr_gin
andr_gin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you very much for the code example: I'll take a detailed look through in the morning. I am interested in as much info regarding reception of Serial data as possible.

Most time the expected packets in my application are very small (20 bytes or less per packet) but packets can come close together (10ms)
This question has been classified as abandoned and is being closed as part of the Cleanup Program.  See my comment at the end of the question for more details.