Advanced search FAQ Register Login

It is currently Tue Jan 23, 2018 2:47 pm

 

Postby kandy » Mon Feb 27, 2017 10:14 pm

Hi
I am using the GPS HAT board on my Raspberry Pi 3. With Python 2.7 I can parse the GPS string using the pynmea2 module. When using Python 3.4 the following error shows up:

Traceback (most recent call last):
File "/home/pi/Public/sensorsTest_v4.py", line 36, in <module>
parseGPS(str)
File "/home/pi/Public/sensorsTest_v4.py", line 25, in parseGPS
if str.find('GGA') > 0:
TypeError: Type str doesn't support the buffer API

Is there a way to I have to change my code for Python 3.4?

Here is the code snippet:

# Initialze the serial port at 9600 baud for GPS signal
serialPort = serial.Serial("/dev/ttyAMA0", 9600, timeout=0.0)

def parseGPS(str):
global msg
if str.find('GGA') > 0:
msg = pynmea2.parse(str)

# print ("Timestamp: %s -- Lat: %s %s -- Lon: %s %s -- Altitude: %s %s" % (msg.timestamp,msg.lat,msg.lat_dir,msg.lon,msg.lon_dir,msg.altitude,msg.altitude_units))

# initial reading

str = serialPort.readline()
parseGPS(str)

kandy
n00b

Posts: 3

Joined: Mon Feb 27, 2017 10:07 pm

Postby Dave » Tue Feb 28, 2017 11:29 am

What's the issue with using Python 2?

Dave
ModMyPi

User avatar

Posts: 2084

Joined: Wed Jul 22, 2015 11:36 am

Forum Administrator & Technical Support

Postby kandy » Tue Feb 28, 2017 11:43 am

Dave

You are right, but this code snippet is part of a greater program which I'd like to keep in Python 3.4. I assume that this error has to do with the new definitions in Python 3, i.e. str, bytes and bytearray.

Hans

kandy
n00b

Posts: 3

Joined: Mon Feb 27, 2017 10:07 pm

Postby Dave » Tue Feb 28, 2017 12:29 pm

Ah yeah fair enough.

Yeah by the looks of it, the find() function only works with strings.

Looks like python 3 takes serial data as byte data and not string data as python 2 does.

You'll need to convert the byte data to a string, something like:

Code: Select all

str = str.decode("utf-8")

!!Untested!!

Dave
ModMyPi

User avatar

Posts: 2084

Joined: Wed Jul 22, 2015 11:36 am

Forum Administrator & Technical Support

Postby kandy » Thu Mar 02, 2017 11:28 pm

Dave
Thanks for the tip. I did not realize that in Python 3 the GPS output is a bytes instance which has to be converted into a string instance.

Hans

kandy
n00b

Posts: 3

Joined: Mon Feb 27, 2017 10:07 pm

Who is online

Users browsing this forum: No registered users and 1 guest

Board index

The teamDelete all board cookies • All times are UTC [ DST ]