[Tutor] how to read over serial port

Brian C. Lane bcl at brianlane.com
Sun Nov 2 16:38:31 CET 2008


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

shawn bright wrote:
> Hey there all,
> 
> I have a gps device that talks to the computer over a serial port.
> i am using the pyserial module and getting values in.

Here are the relevant bits of a serial to tcp/ip app that I use. Most
likely you have the wrong baudrate set. If it is an older GPS device the
baudrate will be 4800, newer devices allow you to set it to higher
speeds, 9600 being standard. Check the device settings to make sure that
it doesn't have any weird parity settings too. 8 bits, No Parity, 1 stop
bit is common.


import sys
import serial

port = "/dev/ttyS0"
baud = 9600

ser = serial.Serial()
ser.port = port
ser.baudrate = baud

try:
    ser.open()
except:
    sys.stderr.write("Error opening serial port %s\n" % (ser.portstr) )
    sys.exit(1)

ser.setRtsCts(0)

while 1:
    # Read from serial port, blocking
    data = ser.read(1)

    # If there is more than 1 byte, read the rest
    n = ser.inWaiting()
    if n:
        data = data + ser.read(n)

    sys.stdout.write(data)



- --
- ---[Office 68.7F]--[Outside 51.0F]--[Server 103.2F]--[Coaster 70.0F]---
- ---[       TACOMA WSF (366772760) @ 47 36.3260 -122 23.1697       ]---
Software, Linux, Microcontrollers             http://www.brianlane.com
AIS Parser SDK                                http://www.aisparser.com

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (Darwin)
Comment: Remember Lexington Green!

iD8DBQFJDcl3Iftj/pcSws0RAs2+AJ91ynHgzdXDfVpbh37iM7XITnDI7wCeNON8
qxyWcuc5opuOpeRCJ6cWr+o=
=fPs4
-----END PGP SIGNATURE-----


More information about the Tutor mailing list