multithreading - Serial Port Constant Monitoring in C# -
I have a program, starts when the search for serial ports available on the computer starts and if found For those for data they will be trying to open and listen.
As of now, I have a configuration file that provides all port settings with port number.
The first time I get the user, enter the first number where the device is connected. It is recorded in config and used from there.
Now I would like to change it, so that when it starts, it scans the ports and receives the list of available serial ports. I can do this part with the SerialPort.GetPortNames () method.
However, now I would like to ping each port and check that who is connected to the device, open that port and listen to that port.
I can check with my device manufacturer to get the order to return the ping and ACK from the device.
My question is for experts
- Is it the right time to put it off (portable / technically)
- done it in separate thread And probably the thread should be kept for serial port communication only, while the main thread does all other work.
- What if many devices connected to a single PC are
It is normal that it is normal [div class = "post-text" itemprop = "text">
is it correct (archietecturally / Technically)
Do it. Continuous opening and closing of ports is an expensive and useless operation. Sometimes it is used as a message framing protocol, ie open / send / stop, but a protocol that keeps the port open is definitely going to perform better.
It should be done in separate thread and possibly only thread for the serial port communication should be threaded. The main thread works all the other.
It may be, yes I have used such a design - to create threads that tries to open COM1-COM9, and if open is successful, So try to communicate with a peripheral. If the compass succeeds, the handler thread serves the main thread with a message reference / pointer that can be used to communicate with the port, (and usually give the user feedback, such as check the checkbox And change its background color from red to green).
What if there are many devices connected to the same PC? Should we maintain separate threads for each device?
In the same way I usually handle it, yes. I define a square that represents the port, it's the protocol and the state. Each port-handler thread then makes its own and therefore each port is handled independently.
Comments
Post a Comment