If possible, you should copy and paste the text of the error message
into your post, not a screen shot.
You should also tell us whether it is a runtime exception, and copy and
paste the full traceback, or just a compile-time linter warning from
your IDE.
It looks like your IDE is showing a compile-time type error from this
line:
chunks = newdata.split(',')
newdata
is read from a serial port, so it contains bytes, not a
string. You need to split on bytes b','
, not a Unicode string.
# Use this instead.
chunks = newdata.split(b',')
In this line you try to find a Unicode string in an array of bytes.
where = newdata.find('$GPRMC')
# Use this instead.
where = newdata.find(b'$GPRMC')
There is a logic bug in the next line, as well as a type error:
gprmcpart = "" + newdata[where:]
The logic bug is this:
- What happens if b’$GPRMC’ is not found, and
where
is -1?
You probably need to check that where != -1
first. I don’t know what
you should do if it is not found.
The type error is that you are trying to concatenate the Unicode string
“” with the bytestring newdata[where:]
. You need b"" instead.
But that’s another logic error. Concatenating an empty string to another
string is a waste of time. Use this instead:
gprmcpart = newdata[where:]
Then you have one more type error, where again you try to split a
bytestring using a Unicode string:
morechunks = gprmcpart.split(',')
Again, the solution is to insert the b prefix so Python uses bytes, not
a Unicode string:
morechunks = gprmcpart.split(b',')