-1
import serial

portwrite = "/dev/ttyUSB2"
port = "/dev/ttyUSB1"

def parseGPS(data):
    print "raw:", data #prints raw data
    if data[0:6] == "$GPRMC":
        sdata = data.split(",")
        if sdata[2] == 'V':
             print "no satellite data available"
             return
        print "-----Parsing GPRMC-----"
        time = sdata[1][0:2] + ":" + sdata[1][2:4] + ":" + sdata[1][4:6]
        lat = decode(sdata[3]) #latitude
        dirLat = sdata[4]      #latitude direction N/S
        lon = decode(sdata[5]) #longitute
        dirLon = sdata[6]      #longitude direction E/W
        speed = sdata[7]       #Speed in knots
        trCourse = sdata[8]    #True course
        date = sdata[9][0:2] + "/" + sdata[9][2:4] + "/" + sdata[9][4:6]
            #date
        variation = sdata[10]  #variation
        degreeChecksum = sdata[12]
        dc = degreeChecksum.split("*")
        degree = dc[0]        #degree
        checksum = dc[1]      #checksum
        print "time : %s, latitude : %s(%s), longitude : %s(%s), speed : %s, True Course : %s, Date : %s, Magnetic Variation : %s(%s),Checksum : %s "%    (time,lat,dirLat,lon,dirLon,speed,trCourse,date,variation,degree,checks$
    else:
        print "Printed data is ",data[0:6]
def decode(coord):
    #Converts DDDMM.MMMMM -> DD deg MM.MMMMM min
    x = coord.split(".")
    head = x[0]
    tail = x[1]
    deg = head[0:-2]
    min = head[-2:]
    return deg + " deg " + min + "." + tail + " min"

print "Connecting port"
serw = serial.Serial(portwrite, baudrate = 115200, timeout = 1)
serw.write('AT+QGPS=1\r')
serw.close()
sleep(0.5)

print "Receiving GPS data"
ser = serial.Serial(port, baudrate = 115200, timeout = 0.5)
while True:
   data = ser.readline()
   parseGPS(data)

result from code

Connecting port 
Traceback (most recent call last): 
    File "gpstest.py", line 41, in <module> 
      serw = serial.Serial(portwrite, baudrate = 115200, timeout = 1) 
    File "/usr/lib/python2.7/dist-packages/serial/serialutil.py", line 236, in __init__ 
      self.open() 
    File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 286, in open 
      self._update_dtr_state() 
    File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 612, in _update_dtr_state 
      fcntl.ioctl(self.fd, TIOCMBIS, TIOCM_DTR_str) 
IOError: [Errno 32] Broken pipe
  • Are you sure about the ports you use ? /dev/ttyUSB1 and /dev/ttyUSB2 ? Do they appear when you type ls -la /dev/tty* – Félicien Feb 27 '18 at 16:05
  • I have this result. crw-rw---- 1 root dialout 188, 0 Feb 28 00:25 /dev/ttyUSB0 crw-rw---- 1 root dialout 188, 1 Feb 28 00:25 /dev/ttyUSB1 crw-rw---- 1 root dialout 188, 2 Feb 28 00:25 /dev/ttyUSB2 crw-rw---- 1 root dialout 188, 3 Feb 28 00:25 /dev/ttyUSB3 – Ze120 Feb 27 '18 at 17:27
  • 2
    could not open port /dev/ttyUSB2: [Errno 2] No such file or directory: '/dev/ttyUSB2' Thats the error! And with the few details you have posted, it's a guessing game! – MatsK Feb 27 '18 at 17:30
0

I've had a similar problem, and setting both rtscts and dsrdtr to True solved it for me, try:

print "Connecting port"
serw = serial.Serial(portwrite, baudrate=115200, timeout=1, dsrdtr=True, rtscts=True)
serw.write('AT+QGPS=1\r')

and:

print "Receiving GPS data"
ser = serial.Serial(port, baudrate=115200, timeout=0.5, dsrdtr=True, rtscts=True)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.