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?