Skip to content

Instantly share code, notes, and snippets.

@fishmandev
Created May 1, 2020 12:55
Show Gist options
  • Save fishmandev/c41280da1ffe78a1b2bbefb8c544d321 to your computer and use it in GitHub Desktop.
Save fishmandev/c41280da1ffe78a1b2bbefb8c544d321 to your computer and use it in GitHub Desktop.
MH-Z19B
#!/usr/bin/python
import serial, os, time, sys, struct
def crc8(a):
crc=0x00
count=1
b=bytearray(a)
while count<8:
crc+=b[count]
count=count+1
#Truncate to 8 bit
crc%=256
#Invert number with xor
crc=~crc&0xFF
crc+=1
return crc
ser = serial.Serial()
ser.port = "/dev/ttyUSB0"
ser.baudrate = 9600
ser.open()
ser.write(serial.to_bytes([0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79]))
time.sleep(0.1)
s = ser.read(9)
print hex(int(s.encode('hex'), 16))
print hex(crc8(s))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment