Page 1 of 1

Parsing GPS sentence

Posted: Fri Aug 05, 2016 4:19 pm
by jiemde
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

Re: Parsing GPS sentence

Posted: Fri Aug 05, 2016 4:55 pm
by dhylands
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.

Re: Parsing GPS sentence

Posted: Fri Aug 05, 2016 5:02 pm
by pythoncoder
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.

Re: Parsing GPS sentence

Posted: Fri Aug 05, 2016 5:42 pm
by jiemde
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

Re: Parsing GPS sentence

Posted: Sat Aug 06, 2016 6:35 am
by pythoncoder
@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.

Re: Parsing GPS sentence

Posted: Sat Aug 06, 2016 8:03 am
by jiemde
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