Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. import serial
  2.  
  3.  
  4. def checksum(array):
  5. checksum = (sum(DataArray) ^ 255) + 1
  6. checksum = hex(checksum)
  7. return checksum[-2:]
  8.  
  9. def processbyte(byte):
  10. if byte.hex() != checksum(DataArray):
  11. DataArray.append(int.from_bytes(byte, byteorder='little'))
  12. else:
  13. print(f'MID: {str(DataArray[0]):4}', end=" ")
  14. print(f'Data: 0x{DataArray[1:].hex().upper():30}', end=" ")
  15. print(f'CS: 0x{checksum(DataArray).upper():3}')
  16. DataArray.clear()
  17.  
  18.  
  19. com = serial.Serial()
  20. com.port = "COM28"
  21. com.baudrate = 9600
  22. com.timeout= (1/9600)*22
  23. com.STOPBITS = 1
  24. com.PARITIES = 0
  25. com.set_buffer_size = 21
  26. com.interCharTimeout = 0.01
  27.  
  28. DataArray = bytearray()
  29.  
  30. if ~com.is_open:
  31. com.open()
  32.  
  33. while(com.is_open):
  34. if(com.in_waiting):
  35. byte = com.read()
  36. processbyte(byte)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement