-
-
Save fishmandev/c41280da1ffe78a1b2bbefb8c544d321 to your computer and use it in GitHub Desktop.
MH-Z19B
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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