将python 2移植到python 3串行文件
我在将一个旧的python2脚本移植到python3时遇到了一个问题,而python3过去工作得很好。以下是Python 2代码,其目的是创建一个命令,通过串行方式发送:将python 2移植到python 3串行文件,python,encode,porting,Python,Encode,Porting,我在将一个旧的python2脚本移植到python3时遇到了一个问题,而python3过去工作得很好。以下是Python 2代码,其目的是创建一个命令,通过串行方式发送: def makeCommand(command, data=''): length = len(command) + len(data) + 1 buf = [chr(length), chr(0xFF), command, data] checksum = 256 - sum[(ord(x) for
def makeCommand(command, data=''):
length = len(command) + len(data) + 1
buf = [chr(length), chr(0xFF), command, data]
checksum = 256 - sum[(ord(x) for x in buf)]%256
buf.append(chr(checksum))
return ''.join(buf)
def main():
makeCommand('A')
main()
Python3,有一些更新:
def makeCommand(command, data=''):
length = len(command) + len(data) + 1
buf = [chr(length), chr(0xFF), command, data]
checksum = 256 - sum[x for x in buf)]%256
buf.append(chr(checksum))
return ''.join(buf).encode()
现在,当我运行它们时,我得到:
- Python 2:\x02\xFFA\CRC
- Python 3:\x02\xc3\xbfA\CRC