Parsing GPS sentence

Questions and discussion about The WiPy 1.0 board and CC3200 boards.
Target audience: Users with a WiPy 1.0 or CC3200 board.
Post Reply
jiemde
Posts: 10
Joined: Sun Jun 19, 2016 12:07 pm
Location: Belgium
Contact:

Parsing GPS sentence

Post by jiemde » Fri Aug 05, 2016 4:19 pm

Hi all,

Since 3 days I try to select the right sentence ( $GPRMC ) from an GPS via the uart.
When I use the readline(), no problems all the sentences are read one by one, except that when I print them there is a "b" as the first character of the line.
When I try to identify the good sentence by:

while True:
cad=uart.readline(7) #just read the first 7 characters of the sentence
if (cad == '$GPRMC,'): # compare these 7 characters to $GPRMC,
data=uart.readline() #read the rest of the line for parsing it later

That don't work!
Is there a way for not having the "b" in front of the extracted line ?
See somewhere that tha "b" is displaying by micropython to say that's a binary result.

If someone can help me ?

Best regards

Jiemde

User avatar
dhylands
Posts: 3409
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Parsing GPS sentence

Post by dhylands » Fri Aug 05, 2016 4:55 pm

You need to understand the difference between unicode strings and binary strings. Strings with a b prefix are binary strings, and strings without a b prefix are unicode strings.

Code: Select all

>>> 'abc' == b'abc'
False
Reading from a uart returns a binary string, so you should either compare it with a binary string or convert it to a unicode string in order to compare it:

Code: Select all

>>> 'abc' == b'abc'.decode('utf-8')
True
So you should change your comparison from:

Code: Select all

if (cad == '$GPRMC,'):
to be

Code: Select all

if (cad == b'$GPRMC,'):
instead.

User avatar
pythoncoder
Posts: 4335
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Parsing GPS sentence

Post by pythoncoder » Fri Aug 05, 2016 5:02 pm

I think you need to read up on Python3 and Unicode. The preceding b denotes a bytes object which is a sequence of 8 bit quantities. In Python3 this is not the same as a string, which is a sequence of Unicode characters. To perform a comparison the types must match. To convert from a string to bytes, do the following:

Code: Select all

>>> s = 'Rats. And more rats.'
>>> s
'Rats. And more rats.'
>>> s.encode('UTF-8')
b'Rats. And more rats.'
>>> 
As you might guess, the reverse operation can be performed with a matching encode() function.
Peter Hinch

jiemde
Posts: 10
Joined: Sun Jun 19, 2016 12:07 pm
Location: Belgium
Contact:

Re: Parsing GPS sentence

Post by jiemde » Fri Aug 05, 2016 5:42 pm

Thanks!
When trying the news comparison in the Micropython console of Pymakr, that's work fine, but not yet in the main.py ?
But today I'm tired an try again tomorrow morning !
Regards

Jiemde

User avatar
pythoncoder
Posts: 4335
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Parsing GPS sentence

Post by pythoncoder » Sat Aug 06, 2016 6:35 am

@jiemde The documentation for UART.readline() method http://docs.micropython.org/en/latest/w ... T.readline makes no mention of an argument. Are you sure

Code: Select all

cad=uart.readline(7) #just read the first 7 characters of the sentence
actually works? I'd have written

Code: Select all

cad = uart.readline()
and then used

Code: Select all

cad[0:7]
to extract the first 7 characters.
Peter Hinch

jiemde
Posts: 10
Joined: Sun Jun 19, 2016 12:07 pm
Location: Belgium
Contact:

Re: Parsing GPS sentence

Post by jiemde » Sat Aug 06, 2016 8:03 am

Hi Pythoncoder,

it's work!, you get the same answer, but in one line instead of 2.
And when you use the uart.readline() again, you get the rest of the line.

regards

Jiemde

Post Reply