Search code examples
python-3.xlinuxpython-2.7gpsbeagleboneblack

RE: Transferring Python2 to Python3 on This Specific Line


I am attempting to change this line to become acceptable by python3 from a python2 set of source:

Here is the error:

TypeError: unicode strings are not supported, please encode to bytes: 
'$PMTK251,9600*17\r\n'

Can anyone tell my why this is this way or how I can change it to suit Python3 methods?

It is a GPS set of source in Python2 that still works but I see that all ideas relating to Python2 will be gone from availability and/or is already pretty much done and gone.

So, my ideas were to update that line and others.

In python3, I receive errors relating to bytes and I have currently read about the idea of (arg, newline='') in source when attempting to make .csv files in Python3.

I am still at a loss w/ how to incorporate Python3 in this specific line.

I can offer more about the line or the rest of the source if necessary. I received this source from toptechboy.com. I do not think that fellow ever updated the source to work w/ Python3.

class GPS:
def __init__(self):
    #This sets up variables for useful commands.
    #This set is used to set the rate the GPS reports
    UPDATE_10_sec = "$PMTK220,10000*2F\r\n" #Update Every 10 Seconds
    UPDATE_5_sec = "$PMTK220,5000*1B\r\n"   #Update Every 5 Seconds  
    UPDATE_1_sec = "$PMTK220,1000*1F\r\n"   #Update Every One Second
    UPDATE_200_msec = "$PMTK220,200*2C\r\n" #Update Every 200 Milliseconds
    #This set is used to set the rate the GPS takes measurements
    MEAS_10_sec = "$PMTK300,10000,0,0,0,0*2C\r\n" #Measure every 10 seconds
    MEAS_5_sec = "$PMTK300,5000,0,0,0,0*18\r\n"   #Measure every 5 seconds
    MEAS_1_sec = "$PMTK300,1000,0,0,0,0*1C\r\n"   #Measure once a second
    MEAS_200_msec= "$PMTK300,200,0,0,0,0*2F\r\n"  #Meaure 5 times a second
    #Set the Baud Rate of GPS
    BAUD_57600 = "$PMTK251,57600*2C\r\n"          #Set Baud Rate at 57600
    BAUD_9600 ="$PMTK251,9600*17\r\n"             #Set 9600 Baud Rate
    #Commands for which NMEA Sentences are sent
    ser.write(BAUD_57600)
    sleep(1)
    ser.baudrate = 57600
    GPRMC_ONLY = "$PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29\r\n" #Send only the GPRMC Sentence
    GPRMC_GPGGA = "$PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28\r\n"#Send GPRMC AND GPGGA Sentences
    SEND_ALL = "$PMTK314,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0*28\r\n" #Send All Sentences
    SEND_NOTHING = "$PMTK314,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28\r\n" #Send Nothing

...

That is the GPS Class Mr. McWhorter wrote for a GPS Module in python2. I am trying to configure this python2 source into a workable python3 class.

I am receiving errors like "needs to be bytes" and/or "cannot use bytes here".

Anyway, if you are handy w/ Python3 and know where I am making mistakes on this source to transfer it over to Python3, please let me know. I have tried changing the source many times to accept bytes and to be read as a utf-string.

Here: Best way to convert string to bytes in Python 3? <<< This seems like the most popular topic on this subject but it does not answer my question so far (I think).


Solution

  • This line simply works when adding a b for bytes in front of the string...like so.

    (b'$PMTK251,9600*17\r\n')
    

    That should rid you of that error of TypeError: unicode strings are not supported, please encode to bytes: