Comments (4)
The API to the library accepts strings, and not bytes. This is intentional. It will probably never accept bytes, unless someone has some good arguments or a proprietary protocol requires some low-level binary data.
You not wanting to properly decode your inputs is not a good argument.
It is your responsibility to know what encodings you need for your own devices and own IO, and to perform them. This is not because I'm lazy, it's because the library cannot know where your data comes from or how it was encoded. The common interface for passing string data is strings.
Please do some research about strings vs bytes best practices. Here's the first google result that I found that I will quote from: https://medium.com/better-programming/strings-unicode-and-bytes-in-python-3-everything-you-always-wanted-to-know-27dc02ff2686
There is no string without encoding
Having a string without knowing its encoding makes no sense at all.
You simply can’t interpret and decode a string unless you know its encoding.
The bytes type has no inherent encoding, so you must know the encoding if you want to try and decode it, as we saw a few paragraphs above. Again: you can’t pretend to decode something unless you know its encoding.
This library could be used in many different contexts. Sometimes directly connected to a device over a serial port, where the encoding is probably ascii, but also reading logs from a file, streaming over the internet, etc, and I have no way of knowing what encodings were used. Your proposed fixes with ascii
encoding in the library are pretending to decode it, and while it may work for you most of the time on a serial device, it is incorrect and will cause bugs for other people and other use cases.
The Unicode Sandwich
“Bytes on the outside, unicode on the inside, encode/decode at the edges.”
pynmea2 here is the inside, and deals strictly with strings. The edges, and how you use the library, are up to you.
There's another workaround that doesn't require you to modify the library sources. Instead, convert the serial output to string before passing it to pynmea2:
ser = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=0.5) while True: data = ser.readline().decode("ascii", "ignore") if data.startswith("$GPGGA"): msg = pynmea2.parse(data) print(msg)
This isn't a workaround, but is correct usage and is basically how I would recommend you use the library (but without "ignore"
). However, don't just blindly guess ascii
, without actually know what encoding you're using.
It works ok for GGA, but some other types still fail.
Please provide examples, and I can help debug them, but without any data, I can only speculate that your IO layer is buggy (the "ignore"
in your decoding is a red flag to me that your input data is corrupt, and you are masking device errors. Or you're wrong about the encoding and it's not ascii)
This is not just my opinion, it is the accepted practice in Python 3 of how to do IO. Note the examples in pyserial all encode/decode the bytes directly at the IO edge after reading/writing them to the device, how built in file IO operates on bytes and makes the user specify an encoding or do the encoding themselves, etc.
from pynmea2.
Hello,
I have already complained several times about that bug, but my correction proposal was rejected. See issue #87 that has been closed rejected. But the problem is still here as you experience it.
This is a pure bug so the knio reaction is a mystery for me. The workaround is in the issue #87
from pynmea2.
@sterwen is right, the bytes vs. str handling in the library is broken for Python 3.
There's another workaround that doesn't require you to modify the library sources. Instead, convert the serial output to string before passing it to pynmea2:
ser = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=0.5)
while True:
data = ser.readline().decode("ascii", "ignore")
if data.startswith("$GPGGA"):
msg = pynmea2.parse(data)
print(msg)
It works ok for GGA, but some other types still fail.
from pynmea2.
pyserial reads and sends bytes
clearly no good reason to use a stream reader, with .next() explicitly - More strangeness than I like
from pynmea2.
Related Issues (20)
- Why `identifier()` method result in Query and Talker sentence contains trailing comma? HOT 1
- Wrong QuerySentence HOT 6
- Does not support PUBX (u-blox propietary sentences) serialization HOT 1
- Issues with parshing PASHR sentences HOT 3
- Incomplete GSV sentence can be parsed HOT 1
- parsed.identifier() returns with and without comma. HOT 2
- How to Communicate with PA1010D GNSS Receiver? HOT 1
- GGA sentence latiitude and longitude are parsed incorrectly HOT 2
- Can't Parse: $IIMWV' HOT 1
- PTNL PJK File Update HOT 4
- NMEA Data; Not all fields are present
- SKU:TEL0137 Gps Module Empty Data HOT 4
- support the FLARM protocol by adding $PFLAA and $PFLAU traffic sentences HOT 2
- calculating distance
- Micropython compatability HOT 2
- Add support for Kenwood LMR sentences HOT 1
- Attribute Errors HOT 2
- Version 1.19.0 has not been tagged in Git
- RMC message error in 1.19.0 (but 1.15.0 works fine) HOT 5
- Needs porting to Python 3.12 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pynmea2.