How to build byte array frame and calculate checksum
<p>I'm trying to communicate with a serial port as defined in a specification.</p>

<pre><code>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()
</code></pre>

<p>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.</p>

<p>In this example, I have manually calculated the checksum, which is defined as:</p>

<p><strong>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</strong></p>

<p>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.</p>

<p>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?</p>

<p>Is generating a checksum literally that simple, or does it require something else?</p>