Question re threading and serial i/o
peter at engcorp.com
Thu Nov 6 15:27:28 CET 2003
Frank Millman wrote:
> This is my Mk 3 version, using select() -
> def scan():
> p = file('/dev/ttyS0')
> while x:
> ans = select.select([p],,,0.1)
> if ans:
> print ord(p.read(1))
> The scanner sends a string consisting of 'code' <tab> 'qty' <cr>. If I
> scan a code of '1' and a quantity of '1', I would expect the program
> to display 49 9 49 13. The Mk 2 version does this correctly.
> The Mk 3 version behaves differently. After the first scan, it
> displays 49. After each subsequent scan, it displays 9 49 13 49.
> If anyone can explain what I am doing wrong, I will be most grateful.
> In the meantime I am sticking with Mk 2, as it is doing the job.
Not entirely sure... but what is "x" in the while statement?
This might be the answer though: when select() returns, it says that
there is "some" data waiting to be read, not just one byte. You
are reading only one byte, however. Depending on what "x" is, this
might mean you are going back to read another single byte only after
some other condition comes true.
Another possibility is that you have not opened the file in non-blocking
mode. I don't know what the effect of that would be on the select()
statement, but if you did use non-blocking, you could change the read()
call to get a whole bunch of data at a time, instead of only one byte.
If you did p.read(1024), for example, on a non-blocking file, you should
get back anywhere from 1 to 1024 bytes after select indicates it is
readable. If nothing else, this will speed up your final result, in
either case. (You can't do that on a block read, of course, since
it would then block until all 1024 bytes were available, which might
More information about the Python-list