0

I have this code:

def checksum_calc(data):
        checksum = 0
        for ch in data:
                checksum += ord(ch)
        return hex(checksum % 256)

checksum=checksum_calc('\xAA\x41\xA1\x0E\x63\x02\x15\x0A\x3F\x00\x00\x00\x00')
test=[0xAA,0x41,0xA1,0x0E,0x63,0x02,0x15,0x0A,0x3F,0x00,0x00,0x00,0x00,checksum]
 ser.write(serial.to_bytes(test))

the data that I need to send in serial is variable and can be changed based on other parts of my code, so I need to calculate the checksum of data and then send it by using serial.to_bytes

when I run the code it returns: string must be of size 1

however checksum_calc function works well but I can not put the result of that in test variable and send it to serial.

how can I put checksum result in the test variable?

Ali
  • 602
  • 6
  • 18

1 Answers1

0

I found the answer here: Sending string to serial.to_bytes not working

I must do like this

test= ['0xAA','0x41','0xA1','0x0E','0x63','0x02','0x15','0x0A','0x3F','0x00','0x00','0x00','0x00',checksum]
   int_values = [int(h, base=16) for h in test]

but I appericate for second approche

Ali
  • 602
  • 6
  • 18