N
N
ninacarrot2013-08-19 09:50:26
Python
ninacarrot, 2013-08-19 09:50:26

PySerial replaces '\r' with '\n' when reading from a tty if pySerial opens a tty with data. Is that how it should be?

I open a tty with openpty, then I open the same tty with pySerial. If the entry in the tty was before the device was opened via pySerial, then instead of '\r\n' pySerial will read '\n\n'. If after, then everything is fine.
The effect is observed at least on Linux 3.2.0, Python 2.7.3.
Test file:

import os
import pty
import serial

MASTER, SLAVE = pty.openpty()

print '\nSending %r' % 'Hello?\r\n'
os.write(MASTER, 'Hello?\r\n')

dev = serial.Serial(os.ttyname(SLAVE), 9600)
dev.timeout = 1
print '\nGot: %r' % dev.read(100)

print '\nSending %r' % 'Hello?\r\n'
os.write(MASTER, "Hello?\r\n")

print '\nGot: %r' % dev.read(100)

Strange effect. Is that how it should be? If yes, why?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
ivnik, 2013-08-19
@ivnik

The 1st read-write differs from the 2nd by a pack of ioctls. It's probably about them.
write(1, "Sending 'Hello?\\r\\n'\n", 21) = 21
write(3, "Hello?\r\n", 8) = 8
ioctl(4, SNDCTL_TMR_TIMEBASE or TCGETS, { c_iflags=0x500, c_oflags=0x5, c_cflags=0xbf, c_lflags=0x8a3b, c_line=0, c_cc="\x03\x1c\x7f\x15\x04\x00\x01\x00\x11\x13\x1a\x00\x12\ x0f\x17\x16\x00\x00\x00"}) = 0
fstat(4, {st_dev=makedev(0, 10), st_ino=32, st_mode=S_IFCHR|0620, st_nlink=1, st_uid=1000, st_gid= 5, st_blksize=1024, st_blocks=0, st_rdev=makedev(136, 29), st_atime=2013/08/19-15:00:48, st_mtime=2013/08/19-15:00:48, st_ctime=2013 /08/19-15:00:48}) = 0
readlink("/proc/self/fd/4", "/dev/pts/29", 4095) = 11
stat("/dev/pts/29", {st_dev=makedev(0, 10), st_ino=32, st_mode=S_IFCHR|0620, st_nlink=1, st_uid=1000, st_gid=5, st_blksize=1024, st_blocks=0 , st_rdev=makedev(136, 29), st_atime=2013/08/19-15:00:48, st_mtime=2013/08/19-15:00:48, st_ctime=2013/08/19-15:00: 48}) = 0
open("/dev/pts/29", O_RDWR|O_NOCTTY|O_NONBLOCK) = 5
ioctl (5, SNDCTL_TMR_TIMEBASE or TCGETS, {c_iflags=0x500, c_oflags=0x5, c_cflags=0xbf, c_lflags=0x8a3b, c_line =0, c_cc="\x03\x1c\x7f\x15\x04\x00\x01\x00\x11\x13\x1a\x00\x12\x0f\x17\x16\x00\x00\x00"}) = 0
ioctl (5, SNDCTL_TMR_TIMEBASE or TCGETS, {c_iflags=0x500, c_oflags=0x5, c_cflags=0xbf, c_lflags=0x8a3b, c_line=0, c_cc="\x03\x1c\x7f\x15\x04\x00\x01\x00\x11\ x13\x1a\x00\x12\x0f\x17\x16\x00\x00\x00"}) = 0
ioctls(5, SNDCTL_TMR_TIMEBASE or TCGETS, {c_iflags=0x500, c_oflags=0x5, c_cflags=0xbf, c_lflags=0x8a3b, c_line=0, c_cc="\x03\x1c\x7f\x15\x04\x00\x01\x00\x11\ x13\x1a\x00\x12\x0f\x17\x16\x00\x00\x00"}) = 0
ioctl (5, SNDCTL_TMR_START or TCSETS, {c_iflags=0, c_oflags=0x4, c_cflags=0x8bd, c_lflags=0, c_line =0, c_cc[VMIN]=0, c_cc[VTIME]=0, c_cc="\x03\x1c\x7f\x15\x04\x00\x00\x00\x11\x13\x1a\x00\x12\x0f\x17 \x16\x00\x00\x00"}) = 0
ioctl (5, SNDCTL_TMR_TIMEBASE or TCGETS, {c_iflags=0, c_oflags=0x4, c_cflags=0x8bd, c_lflags=0, c_line=0, c_cc[VMIN]=0, c_cc [VTIME]=0, c_cc="\x03\x1c\x7f\x15\x04\x00\x00\x00\x11\x13\x1a\x00\x12\x0f\x17\x16\x00\x00\x00"}) = 0
ioctls(5, SNDCTL_TMR_TIMEBASE or TCGETS, {c_iflags=0, c_oflags=0x4, c_cflags=0x8bd, c_lflags=0, c_line=0, c_cc[VMIN]=0, c_cc[VTIME]=0, c_cc="\x03\x1c\ x7f\x15\x04\x00\x00\x00\x11\x13\x1a\x00\x12\x0f\x17\x16\x00\x00\x00"}) = 0
select(6, [5], [], [ ], {1, 0}) = 1 (in [5], left {0, 999996})
read(5, "Hello?\n\n", 100) = 8
select(6, [5], [] , [], {1, 0}) = 0 (Timeout)
write(1, "\n", 1) = 1
write(1, "Got: 'Hello?\\n\\n'\n", 18 ) = 18
write(1, "\n", 1) = 1
write(1, "Sending 'Hello?\\r\\n'\n", 21) = 21
write(3, "Hello?\r\ n", 8) = 8
select(6, [5], [], [], {1, 0}) = 1 (in [5], left {0, 999996})
read(5, "Hello?\ r\n", 100) = 8
select(6, [5], [], [], {1, 0}) = 0 (Timeout)
write(1, "\n", 1) = 1
write(1, "Got: 'Hello?\\r\\n'\n", 18) = 18

PS log retrieved by strace command

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question