fcntl, serial ports and serial signals on RS232.

Max Kotasek mawrman at gmail.com
Wed Apr 7 16:33:40 EDT 2010


Hello to all out there,

I'm trying to figure out how to parse the responses from fcntl.ioctl()
calls that modify the serial lines in a way that asserts that the line
is now changed.  For example I may want to drop RTS explicitly, and
assert that the line has been dropped before returning.

Here is a brief snippet of code that I've been using to do that, but
not sure what to do with the returned response:

def set_RTS(self, state=True):
  if self.fd is None:
    return 0

  p = struct.pack('I', termios.TIOCM_RTS)
  if state:
    return fcntl.ioctl(self.fd, termios.TIOCMBIS, p)
  else:
    return fcntl.ioctl(self.fd, termios.TIOCMBIC, p)

The problem is I get responses like '\x01\x00\x00\x00', or
'\x02\x00\x00\x00'  and I'm not sure what they mean.  I tried doing
illogical things like settings CTS using the TIOCM_CTS flag and I end
up just getting back a slightly different binary packed 32 bit integer
(in that case '\x20\x00\x00\x00').  The above example has self.fd
being defined as os.open('/dev/ttyS0', os.O_RDWR | os.O_NONBLOCK).

Is someone familiar with manipulating serial signals like this in
python?  Am I even taking the right approach by using the fcntl.ioctl
call?  The environment is a ubuntu 8.04 distribution.  Unfortunately
due to other limitations, I can't use/extend pyserial, though I would
like to.

I appreciate any advice on this matter,
Max



More information about the Python-list mailing list