PySerial rereads its own data

Cliff Wells clifford.wells at
Wed Jun 4 09:27:02 CEST 2003

On Tue, 2003-06-03 at 19:42, Greg Ewing (using wrote:
> Cliff Wells wrote:
> >>>My question is this:  why do I get back the data I just wrote?  More to
> >>>the point, how do I stop it?
> I missed your original post, so this is just a guess, but
> are you by any chance trying to use the same file object
> to both read and write the serial port?
> I made that mistake once in C, and I got exactly that
> symptom. It seems that stdio gets its buffering in a
> knot if you try to do that.

Yeah, I've had that problem before (with both C and Python).  In this
case I was reading and writing to ttyS0 via PySerial and reading ttyS1
via 'cat /dev/ttyS1'.  ttyS1 and ttyS0 are connected via a null-modem
cable (this is a test setup).

When I wrote to ttyS0 (from PySerial) the data was correctly displayed
via the cat process.  However, if I then read from ttyS0 via PySerial I
would get back the same data even though cat was clearly not writing

Anyway, I've discovered since that it only happens when using cat
(having another PySerial process doesn't have this problem).  Even
though 'stty -F /dev/ttySn' didn't show any odd flags, I'm guessing that
somehow some echoing was enabled.   PySerial explicitly disables echoing
on ttyS0, so I'm guessing some default setting on ttyS1 was causing the
data to get reflected back to ttyS0.  Not really an educated guess but
my concern about it disappeared when I discovered I wouldn't be facing
that problem anyway (unless I use cat in my production code <wink>).


More information about the Python-list mailing list