嗨,我试图理解一段代码,当我运行它时,这是该代码区域中的错误。我认为它存在根本错误,但这可能只是因为没有调用任何输入
from cstruct import struct
class MateNET(object):
"""
Interface for the MATE RJ45 bus ("MateNET")
This class only handles the low level protocol,
it does not care what is attached to the bus.
"""
TxPacket = struct('>BBHH', ('port', 'ptype', 'addr', 'param')) # Payload is always 4 bytes?
QueryPacket = struct('>HH', ('reg', 'param'))
QueryResponse = struct('>H', ('value'))
struct函数是
def struct(fmt, fields):
fmt = Struct(fmt)
test = fmt.unpack_from(''.join('\0' for i in range(fmt.size)))
nfields = len(test)
if len(fields) != nfields:
raise RuntimeError("Number of fields provided does not match the struct format (Format: %d, Fields: %d)" % (nfields, len(fields)))
错误是:
Traceback (most recent call last):
File "C:\pymate\matenet\FX_inverter_controller.py", line 1, in <module>
from matenet import MateFX
File "C:\pymate\matenet\matenet.py", line 30, in <module>
class MateNET(object):
File "C:\pymate\matenet\matenet.py", line 36, in MateNET
TxPacket = struct('>BBHH', ('port', 'ptype', 'addr', 'param')) # Payload is always 4 bytes?
File "C:\pymate\matenet\cstruct.py", line 14, in struct
test = fmt.unpack_from(''.join('\0' for i in range(fmt.size)))
TypeError: a bytes-like object is required, not 'str'
我在这里很新,也是python的新手,所以请执行我的菜鸟提问技巧
答案 0 :(得分:0)
在python 3.x中,您必须添加bytes(您的字节).encode而不是以前的2.x格式。它变成
test = fmt.unpack_from(bytes(''.join('\0' for i in range(fmt.size)).encode()))