PyCharm giving warnings

So this is my code, I am trying to fetch NMEA sentence from the NEO6M GPS module. Here’s my code:

def get_gps():
    logger.info("Getting GPS Data")
    port = '/dev/ttyAMAO'
    ser = serial.Serial(port, baudrate=9600, timeout=0.5)
    newdata = ser.readline()
    chunks = newdata.split(',')
    where = newdata.find('$GPRMC')
    gprmcpart = "" + newdata[where:]
    morechunks = gprmcpart.split(',')
    lat = str(morechunks[3]) + morechunks[4]
    lng = str(morechunks[5]) + morechunks[6]
    alt = chunks[9]
    spd = morechunks[8]
    logger.info("Got GPS Data")
    logger.info("-- -- -" + lat + "-- -- -" + lng + "-- -- -" + alt + "-- -- -" + spd)

The error

image and I have all libraries installed

Hello @Paarth-go, and welcome!

Are your running Python 2? If so, this is a simple “issue” with the way Python 2 manipulates strings and bytes (bytes are the default, and you are mixing them with strings). If you are running with Python 3, I think nothing should go wrong here.

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',')
2 Likes

Oh, I just noticed the subject line of your post, you do tell us that
they are warnings from PyCharm. Sorry, I missed that.

I am running Python 3.10

Thanks, I’ll need to check

Now its saying

image

I think you’ll have to provide strings everywhere.

Looking back to your code, the serial.Serial object (Assuming serial belongs to the pyserial package) return bytes. So if you convert the ser.readline() to str(ser.readline()), the issue must be resolved?


Edit: Use ser.readline().encode("your encoding, maybe 'utf-8'") instead of str(), since this is the right way to pass bytes to strings. Thanks @steven.daprano for pointing this out!

1 Like

Now its not giving any warnings

I have a related question, here’s its saying
image

class StreamingHandler(server.BaseHTTPRequestHandler):
    def do_GET(self):
        if self.path == '/':
            self.send_response(301)
            self.send_header('Location', '/index.html')
            self.end_headers()
        elif self.path == '/index.html':
            content = PAGE.encode('utf-8')
            self.send_response(200)
            self.send_header('Content-Type', 'text/html')
            self.send_header('Content-Length', len(content))
            self.end_headers()
            self.wfile.write(content)
        elif self.path == '/stream.mjpg':
            self.send_response(200)
            self.send_header('Age', 0)
            self.send_header('Altitude', func.alt)
            self.send_header('Cache-Control', 'no-cache, private')
            self.send_header('Pragma', 'no-cache')
            self.send_header('Content-Type', 'multipart/x-mixed-replace; boundary=FRAME')
            self.end_headers()
            try:
                while True:
                    with output.condition:
                        output.condition.wait()
                        frame = output.frame
                    self.wfile.write(b'--FRAME\r\n')
                    self.send_header('Content-Type', 'image/jpeg')
                    self.send_header('Content-Length', len(frame))
                    self.end_headers()
                    self.wfile.write(frame)
                    self.wfile.write(b'\r\n')
            except Exception as e:
                logging.warning(
                    'Removed streaming client %s: %s',
                    self.client_address, str(e))
        else:
            self.send_error(404)
            self.end_headers()

here

and

and here

If possible, you should copy and paste the text of the error message
into your post, not a screen shot.

That’s not the right way to convert bytes objects into a Unicode string.

You should try it:

str(b"xy")  # returns "b'xy'" but expected "xy"

If the incoming stream of bytes represents a stream of text, you need to
use the encode method to convert it to Unicode text. If you are reading
a binary stream of bytes, you cannot expect to convert it into Unicode.

1 Like

ok sure i’ll try

Ah yeah, I forgot that :sweat_smile: