то был первый вариант
питон принимает строго нужное количество байт, crc совпадает
![]()
CRC вычисляется в именно в строгом количестве байт, смотрите даташит на датчик. В вашем коде на питоне проверки CRC нет.
я же написал, то был первый вариант….
import MySQLdb
import string
import time
import serial
from datetime import datetime
ser = serial.Serial('/dev/ttyUSB0',
baudrate=9600,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
timeout=1.0)
result=ser.write("\xff\x01\x86\x00\x00\x00\x00\x00\x79")
s=ser.read(9)
str = ""
for ch in s:
str += hex(ord(ch))+ " "
print str
crc_get = ord(s[8])
crc_calc = (255 - (ord(s[1]) + ord(s[2]) + ord(s[3]) + ord(s[4]) + ord(s[5]) + ord(s[6]) + ord(s[7]))) % 256 + 1
print "crc calc=" + hex(crc_calc) + " crc get=" + hex(crc_calc)
if s[0] == "\xff" and s[1] == "\x86" and crc_calc == crc_get:
co2 = ord(s[2])*256 + ord(s[3])
print "crc ok"
else:
co2 = 0
print "crc error"
print "co2=", co2
ts = time.time()
ts1 = int(ts * 1000)
print "ts=", ts1
db = MySQLdb.connect(host="localhost", user="xxxxxxx", passwd="xxxxxxx", db="iobroker", charset="utf8")
cursor = db.cursor()
sql = """insert iobroker.ts_number(id, ts, val, ack, _from, q) values (12,%(ts)s,%(val)s,0,2,0) """ %{"ts":ts1, "val":co2}
print "sql=", sql
cursor.execute(sql)
db.commit()
db.close()
вывод исполнения
0xff 0x86 0x1 0xca 0x57 0x40 0x28 0x58 0x98
crc calc=0x98 crc get=0x98
crc ok
co2= 458
ts= 1514450008318
sql= insert iobroker.ts_number(id, ts, val, ack, _from, q) values (12,1514450008318,458,0,2,0)