self.thread.start() def cancel(self): self.thread.cancel() #Asignamos valores para conexión TCP/IP serverIp = "192.168.1.107" #La IP de esta PC serverPort = 5020 #El puerto para la escucha clientIp = "192.168.1.100" #La IP de la otra PC clientPort = 5021 #El puerto para la lectura/escritura #CONFIGURAMOS EL CLIENTE MODBUS TCP client = ModbusTcpClient(clientIp, clientPort) print(client.connect()) readReg = readHoldRegisters(client, 4014, 2) #CONFIGURAMOS EL SERVIDOR MODBUS TCP myBlock = dataServer( 4015, [1000, 8000]) #Creamos el bloque de datos del servidor Modbus TCP store = contextSlave( di=None, co=None, hr=myBlock, ir=None) #Asignamos los bloques de datos al servidor Modbus TCP context = contextServer(slaves=store, single=True) StartTcpServer(context, address=(serverIp, serverPort)) #Encendemos el servidor ModbusTCP while True: print("SERVER ACTIVE") sleep(5)
def __init__(self, tcpipdict): tk.Tk.__init__(self) self.tcpipdict = tcpipdict self.threadreg = Timer(0.3, self.refresh) self.regsval = [0, 0] self.myBlock = dataBlock(4015, self.regsval) self.store = contextSlave(di=None, co=None, hr=self.myBlock, ir=None) self.context = contextServer(slaves=self.store, single=True) self.serverthread = refreshRegisters(self.context, self.tcpipdict) self.serverthread.start() self.title("PRUEBAS MODBUS-TCP") self.geometry("600x600") self.resizable(0, 0) self.label1 = tk.Label(self, text="Registro 1", height=2, width=10, font="Times 14 bold") self.label1.grid(row=0, column=0) self.register1Main = tk.Label(self) self.register1Main.grid(row=1, column=0) self.registerTxt1 = tk.Text(self.register1Main, height=2, width=10) self.registerTxt1.grid(row=0, column=0) self.registerLab1 = tk.Label(self.register1Main, bg="PaleGreen1", bd=2, highlightthickness=2, height=2, width=10) self.registerLab1.grid(row=0, column=1) self.changeReg1Button = tk.Button(self.register1Main, text="Setear", command=self.reg1Button) self.changeReg1Button.grid(row=0, column=2) self.label2 = tk.Label(self, text="Registro 2", height=2, width=10, font="Times 14 bold") self.label2.grid(row=2, column=0) self.register2Main = tk.Label(self) self.register2Main.grid(row=3, column=0) self.registerTxt2 = tk.Text(self.register2Main, height=2, width=10) self.registerTxt2.grid(row=0, column=0) self.registerLab2 = tk.Label(self.register2Main, bg="PaleGreen1", bd=2, highlightthickness=2, height=2, width=10) self.registerLab2.grid(row=0, column=1) self.changeReg2Button = tk.Button(self.register2Main, text="Setear", command=self.reg2Button) self.changeReg2Button.grid(row=0, column=2) self.labelDebbug = tk.Label(self, height=4, width=35, bg="orange2", anchor="nw", text="LOGS:") self.labelDebbug.grid(row=4, column=0, padx=10, pady=15) self.labelDebbug.configure(text="LOGS:" + "\n\r" + self.readReg.log)