I've been programming in .NET for years now, and already know how to use background workers and on_comm events when working with serial ports.
There is one thing, however, that I'm sure could be done far more efficiently and elegantly than the way I've been doing it up until now. And that is, wait for a character to arrive at the serial port.
To make a long story short, these are the couple of lines of code that I've been using to accomplish that.
'wait until the controller sends a character to the serial port
While SPort.BytesToRead < 1 : Threading.Thread.Sleep(300) : End While
xx = SPort.ReadByte()
Is there a better way to do this? I'm trying to avoid using the on_comm event for this purpose because doing so requires splitting the code in several segments.
This is what I do not want to do:
Doing all of that is not only cumbersome, but also makes the code hard to follow. My technique, however, uses more CPU resources and makes the program run slower and the computer feel jumpy and glitchy.
Is there a simpler, and more fluid way of doing this?
There is one thing, however, that I'm sure could be done far more efficiently and elegantly than the way I've been doing it up until now. And that is, wait for a character to arrive at the serial port.
To make a long story short, these are the couple of lines of code that I've been using to accomplish that.
'wait until the controller sends a character to the serial port
While SPort.BytesToRead < 1 : Threading.Thread.Sleep(300) : End While
xx = SPort.ReadByte()
Is there a better way to do this? I'm trying to avoid using the on_comm event for this purpose because doing so requires splitting the code in several segments.
This is what I do not want to do:
- Set up and activate the on_comm event
- Once the event is triggered, remove its handler to prevent it from triggering aditional events if more characters arrive
- Perform several not so easy to understand Invoke instructions from within the event handler so as to execute a procedure and change the state of the form's controls.
- After this is done re-assign the event handler and exit the event handling routine.
- Continue the program in a different routine
Doing all of that is not only cumbersome, but also makes the code hard to follow. My technique, however, uses more CPU resources and makes the program run slower and the computer feel jumpy and glitchy.
Is there a simpler, and more fluid way of doing this?
Last edited: