我试图通过USB将一些数据从Windows 10 PC上的Python 3.5程序发送到TM4C微控制器,并且我正在使用PyUSB。
问题是每当字节值超过127(0x7f)时,PyUSB会在该字节之前添加一个额外字节,有时也会更改原始值。 以下是我用于发送数据的代码部分
def send_data(data): # data is list of integers
message = ''.join(chr(i) for i in data)
TivaC.epOut.write(message) #TiVaC is USB object
部分数据包如下:
sending ..0x66 0x12 0x0 0x0 0x0 0x6
Receiving 0x66 0x12 0x0 0x0 0x0 0x6
New Message
sending ..0x66 0x12 0x0 0x7f 0x0 0x6
Receiving 0x66 0x12 0x0 0x7f 0x0 0x6
New Message
sending ..0x66 0x12 0x0 0x80 0x0 0x6
Receiving 0x66 0x12 0x0 0xc2 0x80 0x0 0x6
Corrupt Packet
sending ..0x66 0x12 0x0 0xbf 0x0 0x6
Receiving 0x66 0x12 0x0 0xc2 0xbf 0x0 0x6
Corrupt Packet
sending ..0x66 0x12 0x0 0xc0 0x0 0x6
Receiving 0x66 0x12 0x0 0xc3 0x80 0x0 0x6
Corrupt Packet
sending ..0x66 0x12 0x1 0xf4 0x0 0x6
Receiving 0x66 0x12 0x1 0xc3 0xb4 0x0 0x6
Corrupt Packet
sending ..0x66 0x12 0x3 0xe8 0x0 0x6
Receiving 0x66 0x12 0x3 0xc3 0xa8 0x0 0x6
Corrupt Packet
问题仅在于从PC发送到微控制器。微控制器始终正确发送所有字节。我检查了一下。