问题
I'm trying to communicate with a serial port as defined in a specification.
ser = serial.Serial("/dev/ttyUSB0", baudrate="115200")
frame = bytearray([
0x00, 0x00, #frame control (2 bytes)
0x00, 0x00, #machine id (2 bytes)
0x07, # number of bytes in data field
0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, #data field itself
0x00, 0x0A #checksum
])
ser.write(frame)
ser.close()
The code executes without error, and I am monitoring the same port in a separate script/process. The device is supposed to return a frame when it receives a successful one.
In this example, I have manually calculated the checksum, which is defined as:
Two-byte checksum, MSB first, computed over the whole frame reanging from FSN.msb...DATA[dsize]. The checksum is calculated by a simple 16-bit unsigned addition of bytes
So in this case, adding everything up in the frame, apart from the checksum would equal 10, as shown by doing sum(frame) without it added. With it added, the sum is 20.
The device on the other end possibly has a fault, so this is a difficult environment to work in, but it would great if anyone could vet my approach so far?
Is generating a checksum literally that simple, or does it require something else?
回答1:
Yes it is that simple - you will normally fill in your frame, and append the checksum in another stage - like in:
In [73]: frame = bytearray([
...: 0x00, 0x00, #frame control (2 bytes)
...: 0x00, 0x00, #machine id (2 bytes)
...: 0x07, # number of bytes in data field
...: 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, #data field itself
...: ])
In [75]: checksum = sum(frame)
In [76]: frame.extend((checksum // 256, checksum % 256))
In [80]: print (", ".join("\\x%02X" % v for v in frame))
\x00, \x00, \x00, \x00, \x07, \x01, \x01, \x01, \x00, \x00, \x00, \x00, \x00, \x0A
Now, note a detail: I added the 2 bytes of the checksum in the "natual order" - which is "MSB" (most significant byte) first. As is in your spec. That should work - if not you likely hae some formatting error in one of the other fields.
来源:https://stackoverflow.com/questions/40381900/how-to-build-byte-array-frame-and-calculate-checksum