Pyserial - can I write to the serial port from stream a, do a read lock from stream b?

I tried to search for it, could not find the answer, searched here, could not find the answer. Has anyone ever wondered if it is safe to thread write a Serial () (pyserial) object from stream a and block reads from stream b?

I know how to use thread synchronization primitives and thread-safe data structures, and in fact my current form of this program has a read / write stream on the serial port, and I use thread-safe data structures to coordinate actions in the application.

My application would be very useful if I could write to the serial port from the main stream (and never read it), and read from the serial port using the lock, read in the second stream (and never write to it). If someone really wants me to go on why this is beneficial to the application, I can add my reasons. In my opinion, there will be only one instance of Serial (), and even if thread B sits in the lock read on the Serial object, thread A will be safe to use write methods in the Serial object.

Does anyone know if this Serial class can be used?

EDIT: It seems to me that the answer may be platform dependent. If you have experience with such a platform, it would be nice to know which platform you worked on.

EDIT: There was only one answer, but if someone else tried this, please leave an answer with your experience.

+6
source share
4 answers

I did this using pyserial. Reading from one stream and writing from another should not cause problems at all, since there really is no problem of arbitration of resources. Serial ports have full duplex, so reading and writing can occur completely independently and at the same time.

+11
source

I used pyserial this way on Linux (and Windows), no problem!

+3
source

I would recommend changing Thread B from "blocking read" to "non blocking read / write". Thread B will become your Daemon serial port.

Thread A can run at full speed for a friendly user interface or perform any operation in real time.

Thread A will write a message to Thread B instead of trying to write directly to the serial port. If the size / frequency of the messages is low, a simple common buffer will work for the message itself and a flag to indicate the presence of a new message. If you need better performance, you should use the stack. This is actually implemented simply using an array large enough to accumulate a large number of sent messages and two pointers. The write pointer is updated only by thread A. The read pointer is updated only by thread B.

Thread B will capture the message and send it to the serial port. The serial port must use the timeout function so that the serial port read function releases the processor, allowing you to poll the shared buffer and, if any new message is present, send it to the serial port. I would use sleep at this point to limit the processor time used by Thread B .. Then you can make the Thread B loop available to the serial read port function. If the serial port timeout does not work correctly, for example, if the USB-RS232 cable is disconnected, the sleep function will make the difference between good Python code and not very good.

0
source
0
source

Source: https://habr.com/ru/post/905575/


All Articles