首页 > 解决方案 > 如何将python3程序转换成python2.7程序

问题描述

我用jetson nano board测试程序按照这条指令进行,可以在python3解释器上运行,但是在python2.7版本的解释器会提示错误,请问大家,怎么修改呢?

  import time
  import struct
  from binascii import unhexlify
  import binascii
  import crcmod

  import math
  byte0 = b'\x01'
  byte1 = b'\xEA'

  DEC1 = int(100)
  DEC2 = int(20)
  byte2 =(struct.pack("i",DEC1)[-4:-3])#线速度
  byte3 =(struct.pack("i",DEC1)[-3:-2])#线速度
  #        speed_vel = byte2 + byte3
  byte4 = (struct.pack("i",DEC2)[-4:-3])    #角速度两个字节
  byte5 = (struct.pack("i",DEC2)[-3:-2])
  #        speed_ang = byte4+byte5
  byte_speed = byte2 + byte3 + byte4 + byte5
  read = byte0 + byte1 + byte_speed  #解析校验码要输入的前几位
  read=(str(binascii.b2a_hex(read))[2:-1])
  crc16 =crcmod.mkCrcFun(0x18005, rev=True, initCrc=0xFFFF, xorOut=0x0000)
  data = read.replace(" ","")
  readcrcout=hex(crc16(unhexlify(data))).upper()
  print(readcrcout)
  str_list = list(readcrcout)
  if len(str_list) < 6:
    str_list.insert(2, '0'*(6-len(str_list)))  # 位数不足补0
  crc_data = "".join(str_list)
  #print(crc_data)
  read = read.strip()+crc_data[4:]+crc_data[2:4]
  read = bytes.fromhex(read)
  speed_code = read
  print('speed_code', speed_code)

标签: python

解决方案


推荐阅读