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.
No hay comentarios:
Publicar un comentario