Is it safe to read and write to a serial port at the same time via different threads (one read thread and one write thread)? Would it be necessary to add locking around reading/writing in each thread?
views:
524answers:
2From the documentation of SerialPort:
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Since Read and Write are not static, they would not be threadsafe. It's a very bad idea, in any case, since the SerialPort class maintains internal buffers for you.
You'll need to synchronize your I/O to your serial port.
Reading and writing to the serial port "at the same time" from different threads is a standard way to handle serial port communications: one thread handles reading, and one handles writing. Acceptable.
There are a number of serial-based devices that send data asynchronously to the host machine while still allowing commands to be sent to the device itself: devices such as barcode scanners, tag scanners and cameras.
Problems?
Problems arise when you attempt to synchronize your communication to and from the device.
For instance, you want to write a command and then immediately read back any response. Well, in that case you would suspend the reading thread and manually read off all the serial port data after having written the command. After the command has been processed, the reading thread can start up again.
Summary
In general through, I would suggest only have one extra thread that processes all the reading of port data and fires off events, such as DataReceived
and perform all your writes from your main thread.