Serial port IO question

pehr anderson pehr at alum.mit.edu
Fri Aug 6 00:03:40 EDT 1999


Dear David,
Don't forget, you have to set all the serial port parameters correctly.
The easiest way to do this is with stty. For example, under linux add the
line:
    os.system("stty 19200 raw < " + upsdev)
You need to find the appropriate speed of course...
For more information see "man stty"... it's not very informative but at
least it works.
    -pehr


David Stokes wrote:

> I am attempting to send a ^M (Control-M) down a serial port and then
> read back from that same port.  The application is an old UPS that will
> send back system status after a ^M.  The code below opens the port but
> the readline seems to be returning what I wrote.  My guess is that I
> haven't really grabbed I/O stream for the read operation.  Any
> suggestions?
>
> Dave Stokes
> -------
>
> #!/usr/bin/env python
> import sys, string
>
> upsdev = "/dev/ttyS1"   # Serial port attached to ups
>
> class   get_ups:
>     def __init__(self):
>         print "Getting ups status"
>
>     def get_ups_data_p(self):
>         upsdata = open( upsdev, 'r+');      # open port to ups
>         upsdata.write("\015")                   # send ^M to ask for
> status
>         upsdata.flush()
>         upsbuffer = upsdata.readline()       # get status back
>         upsdata.close()
>
> if __name__ == '__main__':
>     ups_status = get_ups()
>     ups_status.get_ups_data_p()





More information about the Python-list mailing list