python 3.10 save plc easymodbus data in postgres Windows 10 64bits

Viewed 15

I need to save some data that I am seeing from a plc to a postgres database for which I have this code.

#!/usr/bin/env python
'''
Created on 12.09.2016
@author: Stefan Rossmann
'''
# @UnresolvedImport
from easymodbus.modbusClient import *
# @UnresolvedImport
import psycopg2
conn = psycopg2.connect(host="127.0.0.1",
                        port="5432",
                        user="americo",
                        password="123456",
                        database="rapidscada")
modbusClient = ModbusClient('192.168.5.1', 502)
#modbusClient = ModbusClient('COM4')
modbusClient.connect()
#discreteInputs = modbusClient.read_discreteinputs(3, 1)
holding_registers = modbusClient.read_holdingregisters(3, 1)
print (holding_registers)
cursor = conn.cursor()
cursor.execute("INSERT INTO mitablita (datito, detalle) VALUES(%s, %s)", (holding_registers, 'tg1010_2'))
conn.commit() # <- We MUST commit to reflect the inserted data
cursor.close()
conn.close()
modbusClient.close()

However it is not working correctly as it gives me data type error, this is strange because the data to save and the table field are both integers.

Thank you for your help.

0 Answers
Related