Mailing List Archive

Serial port IO question
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()
Serial port IO question [ In reply to ]
Hi All--

David Stokes wrote:
>

[snip]

> def get_ups_data_p(self):
> upsdata = open( upsdev, 'r+'); # open port to ups <<==Shouldn't this be "rb+"
> upsdata.write("\015") # send ^M to ask for
> status

Take care,
Ivan
----------------------------------------------
Ivan Van Laningham
Callware Technologies, Inc.
ivanlan@callware.com
ivanlan@home.com
http://www.pauahtun.org
See also:
http://www.foretec.com/python/workshops/1998-11/proceedings.html
Army Signal Corps: Cu Chi, Class of '70
----------------------------------------------
Serial port IO question [ In reply to ]
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()