[SerialConnection] Help

Grant Edwards invalid at invalid.invalid
Thu Apr 9 18:28:26 CEST 2015


On 2015-04-09, Travis Griggs <travisgriggs at gmail.com> wrote:
>
>> On Apr 7, 2015, at 8:42 AM, Hugo Caldas <hcaldas92 at gmail.com> wrote:
>> 
>>  read and write the port values with multi threading 
>
> Care to elaborate what you mean by this part? In general, serial
> ports and multi threading don’t mix well. IOW, you’ll need to use
> multithreading pieces to make sure you serialize your access to your
> serial port.

One thing that does work well (and is used by lots of apps) is to use
one thread for reading and a different thread for writing.  If that's
what you want to do, then you don't need to add any mutexes or any
sort of application-level serialization.

At least on Unix, the basic read/write calls should be thread-safe,
_but_not_necessarily_atomic_.

If two threads call write() "at the same time", all the data will go
out, and the order of bytes within each written block will be
preserved, but there's no guarantee that the blocks won't get
interleaved.

If you've got two threads blocking on a read, only one thread will
read any particular byte of data, but there's no guarantee which bytes
will go to which thread.

> As other have pointed out, pyserial is the package you want.
> Documentation is pretty good. We use it a lot. 

-- 
Grant Edwards               grant.b.edwards        Yow! Don't hit me!!  I'm in
                                  at               the Twilight Zone!!!
                              gmail.com            



More information about the Python-list mailing list