Добрый день. Второй день воюю с кодом. Не могу понять в чем ошибка :-(
import struct
import sys
import time
import datetime
import pymysql.cursors
from pymodbus.client.sync import ModbusSerialClient
from pymodbus.exceptions import ModbusIOException
from datetime import datetime
connection = pymysql.connect(host='127.0.0.1', user='root', password='passwd', db='scales', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
unit = 5
client = ModbusSerialClient(method='rtu', port='/dev/ttyUSB1', parity='N', baudrate=9600, bytesize=8, stopbits=1, timeout=1, strict=False)
#---------------------------Продукт 1-------------------------
while True:
reply = client.write_registers(52, 1, unit=unit)
reply = client.write_registers(53, 1, unit=unit)
reply = client.write_registers(5, 2020, unit=unit) #2020 - Total managment
request = client.read_holding_registers(51, 1, unit=unit)
result1 = request.registers
print(result1)
reply = client.write_registers(52, 2, unit=unit)
reply = client.write_registers(53, 1, unit=unit)
reply = client.write_registers(5, 2020, unit=unit) #2020 - Total managment
request = client.read_holding_registers(51, 1, unit=unit)
result2 = request.registers
print(result2)
reply = client.write_registers(52, 3, unit=unit)
reply = client.write_registers(53, 1, unit=unit)
reply = client.write_registers(5, 2020, unit=unit) #2020 - Total managment
request = client.read_holding_registers(51, 1, unit=unit)
result3= request.registers
print(result3)
reply = client.write_registers(52, 1, unit=unit)
reply = client.write_registers(53, 1, unit=unit)
reply = client.write_registers(5, 2020, unit=unit) #2020 - Total managment
request = client.read_holding_registers(51, 1, unit=unit)
result4 = request.registers
print(result4)
reply = client.write_registers(52, 2, unit=unit)
reply = client.write_registers(53, 1, unit=unit)
reply = client.write_registers(5, 2020, unit=unit) #2020 - Total managment
request = client.read_holding_registers(51, 1, unit=unit)
result5 = request.registers
print(result5)
reply = client.write_registers(52, 3, unit=unit)
reply = client.write_registers(53, 1, unit=unit)
reply = client.write_registers(5, 2020, unit=unit) #2020 - Total managment
request = client.read_holding_registers(51, 1, unit=unit)
result6= request.registers
print(result6)
if (result1 == result4 and result2 == result5 and result3 == result6):
written_as = result1[0] * 0.1, result2[0] * 0.1, result3[0] * 0.1
print(result1, result2, result3)
#---------Write DB--------------------
with connection:
cur = connection.cursor()
cur.execute("SELECT * FROM wl5 ORDER BY id DESC LIMIT 1")
rows = cur.fetchall()
for row in rows:
print("{0}".format(row[2]))
print("{0}".format(row[3]))
print("{0}".format(row[4]))
try:
a1 = float(result1[0] * 0.1)
b1 = float(row[2])
silos14 = b1 - a1 - bool(b1 > a1) * 6553.5
print ("Расход:"f'{silos14:.1f}')
a2 = float(result2[0] * 0.1)
b2 = float(row[3])
silos15 = b2 - a2 - bool(b2 > a2) * 6553.5
print ("Расход:"f'{silos15:.1f}')
a3 = float(result3[0] * 0.1)
b3 = float(row[4])
silos16 = b3 - a3 - bool(b3 > a3) * 6553.5
print ("Расход:"f'{silos16:.1f}')
except:
#---------write db result-----
with connection.cursor() as cursor:
# Create a new record
sql = "INSERT INTO `line5` (`date`, `silos14`, silos15, silos16) VALUES (%s, %s, %s, %s)"
today = datetime.datetime.today()
newHireDate = today.strftime("%Y-%m-%d-%H.%M")
cursor.execute(sql, (newHireDate, silos14, silos15, silos16))
#------Write db wl5-------
sql = "INSERT INTO `wl5` (`date`, `product1`, `product2`, `product3`) VALUES (%s, %s, %s, %s)"
today = datetime.datetime.today()
newHireDate = today.strftime("%Y-%m-%d-%H.%M")
cursor.execute(sql, (newHireDate, result1[0] * 0.1, result2[0] * 0.1,result3[0] * 0.1))
connection.commit()
break