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.
martes, 27 de noviembre de 2018
pymodbus TCP cliente - ejemplo
import os from pymodbus.client.sync import ModbusTcpClient cliente = ModbusTcpClient('127.0.0.1','5020') #---------------------------------------------------------------------------# import logging logging.basicConfig() log = logging.getLogger() log.setLevel(logging.DEBUG) #---------------------------------------------------------------------------# os.system('clear') print "Read Coils" request = cliente.read_coils(0x0,0x8,unit=1) 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] print "Read Discrete Input" request = cliente.read_discrete_inputs(0x0,0x8,unit=1) 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] print "Read Holding Registers" request = cliente.read_holding_registers(0x0,0x7,unit=1) print request.registers print "Read Input Registers" request = cliente.read_input_registers(0x0,0x7,unit=1) print request.registers cliente.close()
Generated by GNU Enscript 1.6.5.90.
pymodbus TCP server - ejemplo
#!/usr/bin/env python #---------------------------------------------------------------------------# import os from pymodbus.server.async import StartTcpServer from pymodbus.device import ModbusDeviceIdentification from pymodbus.datastore import ModbusSequentialDataBlock from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext from pymodbus.transaction import ModbusRtuFramer, ModbusAsciiFramer #---------------------------------------------------------------------------# import logging logging.basicConfig() log = logging.getLogger() log.setLevel(logging.DEBUG) os.system('clear') #---------------------------------------------------------------------------# # di - entradas discretas # co - coils # hr - holding registros # ir - input registros hr01 = 100 hr02 = 110 hr03 = 120 hr04 = 130 hr05 = 140 hr06 = 150 hr07 = 160 hr08 = 170 ir01 = 100 ir02 = 110 ir03 = 120 ir04 = 130 ir05 = 140 ir06 = 150 ir07 = 160 ir08 = 170 Reg_DI = [1,0,1,1,1,1,1,0,1] Reg_CO = [1,0,1,0,1,0,1,0,1] Reg_HR = [hr01, hr02, hr03, hr04, hr05, hr06, hr07, hr08] Reg_IR = [ir01, ir02, ir03, ir04, ir05, ir06, ir07, ir08] store = ModbusSlaveContext( di=ModbusSequentialDataBlock(0, Reg_DI), co=ModbusSequentialDataBlock(0, Reg_CO), hr=ModbusSequentialDataBlock(0, Reg_HR), ir=ModbusSequentialDataBlock(0, Reg_IR)) context = ModbusServerContext(slaves=store, single=True) #---------------------------------------------------------------------------# identity = ModbusDeviceIdentification() identity.VendorName = 'Pymodbus TCP' identity.VendorUrl = 'http://www.chispolinux.blogspot.com' identity.ProductName = 'Simulador de dispositivos by JMQ' identity.MajorMinorRevision = '1.0' #---------------------------------------------------------------------------# StartTcpServer(context, identity=identity, address=("localhost", 5020))
Generated by GNU Enscript 1.6.5.90.
domingo, 18 de noviembre de 2018
Pymodbus rtu cliente serie
import os from pymodbus.client.sync import ModbusSerialClient as ModbusClient #---------------------------------------------------------------------------# import logging logging.basicConfig() log = logging.getLogger() log.setLevel(logging.DEBUG) #---------------------------------------------------------------------------# cliente = ModbusClient(method='rtu', port='/dev/ttyUSB0', timeout=1, stopbits = 1, bytesize = 8, parity='N', baudrate= 9600) cliente.connect() os.system('clear') print "Read Coils" request = cliente.read_coils(0x0,0x8,unit=1) print request.bits[0] print request.bits[1] print request.bits[2] print "Read Discrete Input" request = cliente.read_discrete_inputs(0x0,0x8,unit=1) print request print "Read Holding Registers" request = cliente.read_holding_registers(0x0,0x7,unit=1) print request.registers print "Read Input Registers" request = cliente.read_input_registers(0x0,0x7,unit=1) print request.registers
Generated by GNU Enscript 1.6.5.90.
Pymodbus rtu maestro serie
#!/usr/bin/env python #---------------------------------------------------------------------------# from pymodbus.server.async import StartSerialServer from pymodbus.device import ModbusDeviceIdentification from pymodbus.datastore import ModbusSequentialDataBlock from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext from pymodbus.transaction import ModbusRtuFramer, ModbusAsciiFramer #---------------------------------------------------------------------------# import logging logging.basicConfig() log = logging.getLogger() log.setLevel(logging.DEBUG) #---------------------------------------------------------------------------# # di - entradas discretas # co - coils # hr - holding registros # ir - input registros hr01 = 100 hr02 = 110 hr03 = 120 hr04 = 130 hr05 = 140 hr06 = 150 hr07 = 160 hr08 = 170 ir01 = 100 ir02 = 110 ir03 = 120 ir04 = 130 ir05 = 140 ir06 = 150 ir07 = 160 ir08 = 170 Reg_DI = [1,0,1,1,1,1,1,0,1] Reg_CO = [1,0,1,1,1,1,1,0,1] Reg_HR = [hr01, hr02, hr03, hr04, hr05, hr06, hr07, hr08] Reg_IR = [ir01, ir02, ir03, ir04, ir05, ir06, ir07, ir08] store = ModbusSlaveContext( di=ModbusSequentialDataBlock(0, Reg_DI), co=ModbusSequentialDataBlock(0, Reg_CO), hr=ModbusSequentialDataBlock(0, Reg_HR), ir=ModbusSequentialDataBlock(0, Reg_IR)) esclavos = { 0x01: store, 0x02: store, 0x03: store, 0x04: store, 0x05: store, 0x06: store, 0x07: store, 0x08: store, } context = ModbusServerContext(slaves=esclavos, single=False) #---------------------------------------------------------------------------# identity = ModbusDeviceIdentification() identity.VendorName = 'Pymodbus SERIE' identity.VendorUrl = 'http://www.chispolinux.blogspot.com' identity.ProductName = 'Simulador de dispositivos by JMQ' identity.MajorMinorRevision = '1.0' #---------------------------------------------------------------------------# StartSerialServer(context, identity=identity, port='/dev/ttyUSB1', baudrate='9600', framer=ModbusRtuFramer)
Generated by GNU Enscript 1.6.5.90.
martes, 13 de noviembre de 2018
CVM 96 Python MODBUS
import os from pymodbus.client.sync import ModbusSerialClient as ModbusClient #---------------------------------------------------------------------------# import logging logging.basicConfig() log = logging.getLogger() log.setLevel(logging.DEBUG) #---------------------------------------------------------------------------# client = ModbusClient(method='rtu', port='/dev/ttyUSB0', timeout=1, stopbits = 1, bytesize = 8, parity='N', baudrate= 9600) client.connect() os.system('clear') request = client.read_input_registers(0x0,0x1E,unit=1) print request.registers print "V1 :", request.registers[0], request.registers[1] print "A1 :", request.registers[2], request.registers[3] print "KW1 :", request.registers[4], request.registers[5] print "Kvar1 :", request.registers[6], request.registers[7] print "PF1 :", request.registers[8], request.registers[9] print "" print "V2 :", request.registers[10], request.registers[11] print "A2 :", request.registers[12], request.registers[13] print "KW2 :", request.registers[14], request.registers[15] print "Kvar2 :", request.registers[16], request.registers[17] print "PF2 :", request.registers[18], request.registers[19] print "" print "V3 :", request.registers[20], request.registers[21] print "A3 :", request.registers[22], request.registers[23] print "KW3 :", request.registers[24], request.registers[25] print "Kvar3 :", request.registers[26], request.registers[27] print "PF3 :", request.registers[28], request.registers[29]
Generated by GNU Enscript 1.6.5.90.
Suscribirse a:
Entradas (Atom)
IRC
#freenode->#usljujuy