RE:在此特定行上将Python2传输到Python3

问题描述

我正在尝试将此行更改为从python2来源集中的python3接受:

"$PMTK220,10000*2F\r\n"

这是Python2中GPS的一组源,仍然可以使用,但是我看到所有与Python2有关的想法都将不再可用,并且/或者已经完成很多工作了。

所以,我的想法是更新该行和其他行。

python3中,我收到与字节有关的错误,并且当尝试在python3中制作.csv文件时,我目前已经在源中了解到(arg,newline ='')的概念。

在如何将python3合并到此特定行中时,我仍然茫然无措。

如有必要,我可以提供有关该系列产品或其他来源的更多信息。我从toptechboy.com收到此资源。我不认为该研究员曾经将源代码更新为可以在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*2C\r\n" #Measure every 10 seconds
    MEAS_5_sec = "$PMTK300,5000,0*18\r\n"   #Measure every 5 seconds
    MEAS_1_sec = "$PMTK300,1000,0*1C\r\n"   #Measure once a second
    MEAS_200_msec= "$PMTK300,200,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,1,0*29\r\n" #Send only the GPRMC Sentence
    GPRMC_GPGGA = "$PMTK314,0*28\r\n"#Send GPRMC AND GPGGA Sentences
    SEND_ALL = "$PMTK314,0*28\r\n" #Send All Sentences
    SEND_nothing = "$PMTK314,0*28\r\n" #Send nothing

...

这是McWhorter先生为python2中的GPS模块编写的GPS类。我正在尝试将此python2源配置为可行的python3类。

我收到诸如“需要字节”和/或“不能在此处使用字节”之类的错误消息。

无论如何,如果您在使用python3时很方便,并且知道我在此源上犯了错误,将其转移到python3,请告诉我。我已经尝试过多次更改源以接受字节并被读取为utf字符串。

在这里Best way to convert string to bytes in Python 3?

解决方法

当在字符串前面为字节添加 b 时,这一行很简单......就像这样。

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

那应该可以消除 TypeError: unicode strings are not supported,please encode to bytes:

的错误