fcntl, serial ports and serial signals on RS232.

Anssi Saari as at sci.fi
Thu Apr 8 11:54:10 EDT 2010


Max Kotasek <mawrman at gmail.com> writes:

> 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'm not an expert in this by any means. However, I don't think that
fcntl call actually returns the port status after the bit setting. But
why not check it explicitly with termios.TIOCMGET? At least then I
seem to be able to toggle the RTS bit (bit 2) in the register. Here
are the trivial functions I used:

def set_rts(fd):
    print "Setting RTS."
    p = struct.pack('I', termios.TIOCM_RTS) 
    fcntl.ioctl(fd, termios.TIOCMBIS, p)

def clear_rts(fd):
    print "Clearing RTS."
    p = struct.pack('I', termios.TIOCM_RTS) 
    fcntl.ioctl(fd, termios.TIOCMBIC, p)

def get_status(fd):
    print "Querying RTS state."
    stat = struct.pack('I', 0)
    rc = fcntl.ioctl(fd, termios.TIOCMGET, stat)
    if struct.unpack('I', rc)[0] & termios.TIOCM_RTS:
        print "RTS bit is on."
    else:
        print "RTS bit is off."


It seems to me also that RTS is always on after the port has been
opened. I didn't dig out my voltmeter or anything to check this,
though.



More information about the Python-list mailing list