Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 393
Taylor Leese Avatar asked Sep 28 '09 18:09

Taylor Leese


1 Answers

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.

like image 164
Reed Copsey Avatar answered Oct 22 '22 17:10

Reed Copsey