0

I can not make a simple script working fine in Python2.7 work in Python3. The script:

enter code here{
#!/usr/bin/env python
# simple test program to test serial I/O data from gps 
# 
# Dec 2019 HRK

import Adafruit_BBIO.UART as UART
import serial

UART.setup("UART2")

ser2 = serial.Serial('/dev/ttyO2', timeout=2)

def parseGPS(gps_data):
    if gps_data[0:6] == "$GNRMC":
        sdata=gps_data.split(",")
        if sdata[2] == 'V':
            print("Sat data not valid")
            return
        print("Parsing GNRMC data")
        time=sdata[1][0:2]+":"+sdata[1][2:4]+":"+sdata[1][4:6]
        lat=decode(sdata[3])
        dirlat=sdata[4]
        lon=decode(sdata[5])
        dirlon=sdata[6]
        print('GMT: {} lat: {}{} long: {}{}'.format(time, lat, dirlat, lon, dirlon))

def decode(coord):
    # converts dddmm.mmmmm > DD deg MM.MMMMM min
    x = coord.split(".")
    head = x[0]
    tail = x[1]
    tail = tail[0:2]
    deg = head[0:-2]
    min = head[-2:]
    return deg + ":" + min + "." + tail

print("receiving GPS data")
while True:
    data = ser2.readline()
    parseGPS(data)    
    }

Python2 gives the expected respons. Python3 just stops after printing("receiving GPS data"). I am aware of differences in serial in the two Python version but Googling did not bring a solution for me. The problem is likely the ser2.readline() statement. Please advise.
Thanks in advance, Harke

Harke Smits
  • 13
  • 1
  • 7

1 Answers1

0

When you initialize 'ser2' your letting a lot of stuff remain default.
Try setting more parameters to ensure they are correct.
I will try to recreate the error and help get some working code for you when i get home
Example take from this post
Python Serial: How to use the read or readline function to read more than 1 character at a time

serial.Serial( port='COM5',\
               baudrate=9600,\ 
               parity=serial.PARITY_NONE,\
               stopbits=serial.STOPBITS_ONE,\
               bytesize=serial.EIGHTBITS,\
               timeout=0)

Also see this post

pySerial differences with python 2.7 and 3.4

ron
  • 186
  • 7
  • Thank you Ron. Declaring all serial pars has no effect. I also tried: str(ser2.readline()), does not help either. Looks like ser2.readline() is waiting for something. The NMEA protocol out of uBlox specifies CR/LF as line terminator. My best, – Harke Smits Jan 02 '20 at 12:26
  • For the record: I think I found the culprit: not the readline() statement but the first if statement in ParseGPS. It should read: if "GNRMC" in gps_data:. I found this by manual simulation at the Pyhton3 prompt. Not that I understand the mechanism.... – Harke Smits Jan 03 '20 at 20:40
  • Unfortunately this is not the end. The same script on another Beaglebone with the same version of Debian does not run. Just nothing happens at the readline statement. When I type in the relevant statements manually at the Python3 prompt I receive gps data, albeit somewhat older than expected. I cleared the buffer and that helps but then again older data only. Rebooting etc does not help. What can be the cause? Any idea @ron? – Harke Smits Jan 30 '20 at 10:39
  • It has been solved. Different GPS receivers provide different datasets. I was looking for $GNRMC while $GPRMC is more common. This way you can wait for ages...... Found out by configuring the UART pin from the Debian/Linux command prompt and dumping the result to screen. – Harke Smits Feb 06 '20 at 20:13