sábado, 8 de diciembre de 2018
viernes, 7 de diciembre de 2018
pymodbus TCP cliente mysql tabla - ejemplo
Crear primero la BD llamada MODBUS_SCADA CREATE TABLE ReadCoils ( idReadCoils INT NOT NULL AUTO_INCREMENT, CO1 CHARACTER, CO2 CHARACTER, CO3 CHARACTER, CO4 CHARACTER, CO5 CHARACTER, CO6 CHARACTER, CO7 CHARACTER, CO8 CHARACTER, PRIMARY KEY (idReadCoils) ) ENGINE=InnoDB; CREATE TABLE ReadDiscreteInput ( idReadDiscreteInput INT NOT NULL AUTO_INCREMENT, DI1 CHARACTER, DI2 CHARACTER, DI3 CHARACTER, DI4 CHARACTER, DI5 CHARACTER, DI6 CHARACTER, DI7 CHARACTER, DI8 CHARACTER, PRIMARY KEY (idReadDiscreteInput) ) ENGINE=InnoDB; CREATE TABLE ReadHoldingRegisters ( idReadHoldingRegisters INT NOT NULL AUTO_INCREMENT PRIMARY KEY, HR1 INT, HR2 INT, HR3 INT, HR4 INT, HR5 INT, HR6 INT, HR7 INT, HR8 INT ) ENGINE=InnoDB; CREATE TABLE ReadInputRegisters ( idReadInputRegisters INT NOT NULL AUTO_INCREMENT PRIMARY KEY, IR1 INT, IR2 INT, IR3 INT, IR4 INT, IR5 INT, IR6 INT, IR7 INT, IR8 INT ) ENGINE=InnoDB;
Generated by GNU Enscript 1.6.5.90.
pymodbus TCP cliente mysql - ejemplo
import os import MySQLdb from pymodbus.client.sync import ModbusTcpClient cliente = ModbusTcpClient('127.0.0.1','5020') #---------------------------------------------------------------------------# import logging logging.basicConfig() log = logging.getLogger() log.setLevel(logging.DEBUG) Dispositivo = 1 #---------------------------------------------------------------------------# def CONSULTA_SQL(COMANDO, DISPOSITIVO, a, b, c, d, e, f, g, h): coneccion = MySQLdb.connect("localhost","root","holanena69","MODBUS_SCADA") BD = coneccion.cursor() if COMANDO == 1: BD.execute("""INSERT INTO ReadCoils (CO1, CO2, CO3, CO4, CO5, CO6, CO7, CO8) VALUES (%c, %c, %c, %c, %c, %c, %c, %c)""",(a, b, c, d, e, f, g, h)) elif COMANDO == 2: BD.execute("""INSERT INTO ReadDiscreteInput (DI1, DI2, DI3, DI4, DI5, DI6, DI7, DI8) VALUES (%c, %c, %c, %c, %c, %c, %c, %c)""",(a, b, c, d, e, f, g, h)) elif COMANDO == 3: BD.execute("""INSERT INTO ReadHoldingRegisters (HR1, HR2, HR3, HR4, HR5, HR6, HR7, HR8) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)""",(a, b, c, d, e, f, g, h)) elif COMANDO == 4: BD.execute("""INSERT INTO ReadInputRegisters (IR1, IR2, IR3, IR4, IR5, IR6, IR7, IR8) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)""",(a, b, c, d, e, f, g, h)) coneccion.commit() BD.close() coneccion.close() #---------------------------------------------------------------------------# os.system('clear') print "Read Coils" request = cliente.read_coils(0x0,0x8,unit=Dispositivo) print request.bits[0] print request.bits[1] print request.bits[2] print request.bits[3] print request.bits[4] print request.bits[5] print request.bits[6] print request.bits[7] CONSULTA_SQL(1,0,request.bits[0], request.bits[1], request.bits[2], request.bits[3], request.bits[4], request.bits[5], request.bits[6], request.bits[7]) print "Read Discrete Input" request = cliente.read_discrete_inputs(0x0,0x8,unit=Dispositivo) print request.bits[0] print request.bits[1] print request.bits[2] print request.bits[3] print request.bits[4] print request.bits[5] print request.bits[6] print request.bits[7] CONSULTA_SQL(2,0,request.bits[0], request.bits[1], request.bits[2], request.bits[3], request.bits[4], request.bits[5], request.bits[6], request.bits[7]) print "Read Holding Registers" request = cliente.read_holding_registers(0x0,0x7,unit=Dispositivo) print request.registers print type(request.registers[0]) CONSULTA_SQL(3,0,request.registers[0], request.registers[1], request.registers[2], request.registers[3], request.registers[4], request.registers[5], request.registers[6], 9) print "Read Input Registers" request = cliente.read_input_registers(0x0,0x7,unit=Dispositivo) print request.registers CONSULTA_SQL(4,0,request.registers[0], request.registers[1], request.registers[2], request.registers[3], request.registers[4], request.registers[5], request.registers[6], 9) cliente.close()
Generated by GNU Enscript 1.6.5.90.
Suscribirse a:
Entradas (Atom)
IRC
#freenode->#usljujuy