Is it safe to read and write to a serial port at the same time via different threads?

.net, c#, serial-port

Solution

From 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.

Problem

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?

Original source