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
Parsing GPS sentence
Re: Parsing GPS sentence
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.
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:
So you should change your comparison from: to be instead.
Code: Select all
>>> 'abc' == b'abc'
False
Code: Select all
>>> 'abc' == b'abc'.decode('utf-8')
True
Code: Select all
if (cad == '$GPRMC,'):
Code: Select all
if (cad == b'$GPRMC,'):
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Parsing GPS sentence
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:
As you might guess, the reverse operation can be performed with a matching encode() function.
Code: Select all
>>> s = 'Rats. And more rats.'
>>> s
'Rats. And more rats.'
>>> s.encode('UTF-8')
b'Rats. And more rats.'
>>>
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Parsing GPS sentence
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
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
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Parsing GPS sentence
@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
actually works? I'd have written
and then used
to extract the first 7 characters.
Code: Select all
cad=uart.readline(7) #just read the first 7 characters of the sentence
Code: Select all
cad = uart.readline()
Code: Select all
cad[0:7]
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Parsing GPS sentence
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
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