Todavía falta codear mas funciones de MODBUS, versión 0.01
viernes, 25 de agosto de 2023
Tesys T LTMR + Gambas3 + Modbus + V0.01
' Gambas class file
Public DatoRegistro As String
Public DatoINT[256] As Integer
Public Sub Socket1_Closed()
PictureBox8.Picture = Picture.Load("led_verde_off.png")
End
Public Sub Socket1_Ready()
PictureBox8.Picture = Picture.Load("led_verde_on.png")
End
Public Function BIN_DEC(Binario As String, n As Integer) As Integer
Dim L, i, e, Entero As Integer
e = 0
For i = n To 0 Step -1
If Binario[i] = "1" Then
Entero = Entero + 2 ^ e
Endif
e = e + 1
Next
Return Entero
End
Public Function DEC_BIN(Decimal As Integer) As String
Dim i As Integer
Dim CadenaBit As String
For i = 0 To 15
If (Decimal % 2 == 1) Then
CadenaBit = CadenaBit & "1"
Else
CadenaBit = CadenaBit & "0"
End If
Decimal = Decimal / 2
Next
Return (Byte[].FromString(CadenaBit)).Reverse().ToString()
End
Function Leer_MDBUS_TCP(Registro As Integer) As String
Dim i, l As Integer
Dim Byte_LO As String
Dim Byte_HI As String
Escribir_MODBUS_TCP(0, 3, Registro, 1)
l = Len(DatoRegistro)
For i = 0 To l
DatoINT[i] = Asc(DatoRegistro[i])
Next
'For i = 0 To l
' Print i, DatoINT[i], DEC_BIN(DatoINT[i])
'Next
Byte_LO = Right$(DEC_BIN(DatoINT[10]), 8)
Byte_HI = Right$(DEC_BIN(DatoINT[9]), 8)
Return Byte_HI & Byte_LO
End
Public Sub Escribir_MODBUS_TCP(Direccion As Integer, CodigoFuncion As Integer, DireccionRegistro As Integer, Dato As Integer)
Dim TRAMA As New Integer[256]
Dim DATO_TX As String
Dim BIN, HI, LO As String
'Identificador de la transaccion
TRAMA[0] = 0
TRAMA[1] = 0
'Identificador del protocolo 0 para modbus
TRAMA[2] = 0
TRAMA[3] = 0
'Numero de bytes en esta trama
TRAMA[4] = 0
TRAMA[5] = 6
'Direccion del dispositivo
TRAMA[6] = Direccion
'Codigo de funcion
TRAMA[7] = CodigoFuncion
'Direccion registro
BIN = DEC_BIN(DireccionRegistro)
HI = Mid$(BIN, 1, 8)
LO = Mid$(BIN, 9, 16)
TRAMA[8] = BIN_DEC(HI, 7)
TRAMA[9] = BIN_DEC(LO, 7)
'Datos del registro
BIN = DEC_BIN(Dato)
HI = Mid$(BIN, 1, 8)
LO = Mid$(BIN, 9, 16)
TRAMA[10] = BIN_DEC(HI, 7)
TRAMA[11] = BIN_DEC(LO, 7)
DATO_TX = Chr$(TRAMA[0]) & Chr$(TRAMA[1]) & Chr$(TRAMA[2]) & Chr$(TRAMA[3]) & Chr$(TRAMA[4]) & Chr$(TRAMA[5]) & Chr$(TRAMA[6]) & Chr$(TRAMA[7]) & Chr$(TRAMA[8]) & Chr$(TRAMA[9]) & Chr$(TRAMA[10]) & Chr$(TRAMA[11])
If (Socket1.Status = Net.Connected) Then
Write #Socket1, DATO_TX, Len(DATO_TX)
Endif
End
Public Sub Cerrar_Click()
Escribir_MODBUS_TCP(0, 6, 704, 1)
End
Public Sub Socket1_Error()
Select Case Socket1.Status
Case Net.CannotCreateSocket
Message.Error("El sistema no permite crear un socket")
Case Net.HostNotFound
Message.Error("Host no encontrado")
Case Net.ConnectionRefused
Message.Error("No es posible conectar. La solicitud fue rechazada")
Case Net.CannotRead
Message.Error("Error leyendo datos")
Case Net.CannotWrite
Message.Error("Error escribiendo datos")
End Select
End
Public Sub Abrir_Click()
Escribir_MODBUS_TCP(0, 6, 704, 0)
End
Public Sub Conectar_Click()
Socket1.Host = TextBox1.Text
Socket1.Port = Val(TextBox2.Text)
Socket1.Connect()
End
Public Sub Timer1_Timer()
Dim BITs As String
Dim Entero As Integer
If (Socket1.Status = Net.Connected) Then
BITs = Leer_MDBUS_TCP(459)
Print BITs
If BITs[3] = "1" Then
PictureBox3.Picture = Picture.Load("contactor_close.png")
Else If BITs[3] = "0" Then
PictureBox3.Picture = Picture.Load("contactor_open.png")
Endif
If BITs[2] = "1" Or BITs[14] = "1" Then
PictureBox2.Picture = Picture.Load("Interruptor_close.png")
Else If BITs[2] = "0" Or BITs[14] = "0" Then
PictureBox2.Picture = Picture.Load("Interruptor_open.svg.png")
Endif
Endif
End
Public Sub O2_Click()
Escribir_MODBUS_TCP(0, 6, 704, 2)
End
Public Sub Reset_Click()
Escribir_MODBUS_TCP(0, 6, 704, 8)
End
Public Sub Socket1_Read()
Read #Socket1, DatoRegistro, Lof(Socket1)
End
Public Sub Form_Open()
Timer1.Enabled = True
End
martes, 8 de agosto de 2023
Tesys T LTMR + pymodbus + Tkinter
Observen que al darle al boton ON 0 cambiamos de estado la salida O1 del Tesys T, de esta manera podemos probar nuestro LTMR en forma remota. Recuerden el I6 en estado alto para remoto.
Tesys T LTMR + pymodbus + Tkinter
from Tkinter import * import os import MySQLdb from pymodbus.client.sync import ModbusTcpClient #------------------------------------------------------- #------------------------------------------------------- def decbin(numero): cadenabin = "" for i in range(1,17): resultado = numero / 2 resto = numero % 2 if resto == 0: cadenabin = cadenabin + "0" else: cadenabin = cadenabin + "1" numero=resultado return cadenabin def FUNC_OFF_a(): cliente.write_register(704, 0b0000000000000000) def FUNC_ON_a(): cliente.write_register(704, 0b0000000000000001) def FUNC_WRITE(): dato = entrada.get() print dato dato = int(dato) cliente.write_register(652,dato) def FUNC_ON_b(): cliente.write_register(704, 0b0000000000000010) def FUNC_Reset(): cliente.write_register(704, 0b0000000000001000) def FUNC_Kill(): print ("Cuidado !") cliente.write_register(705, 0b0000000000011111) def FUNC_Estado(): estado = cliente.read_holding_registers(455,1) datos = estado.registers[0] print("-------------------------------------") print ("Registro 455") #print datos print decbin(datos) if (datos & 1): print ("Sistema listo") if (datos & 2): print ("Sistema activado") if (datos & 4): print ("Sistema fallo") if (datos & 8): print ("Sistema advertencia") if (datos & 16): print ("Sistema disparo") if (datos & 32): print ("Reinicio de fallo autorizado") if (datos & 64): print ("Controlador potencia\n") print("-------------------------------------") estado = cliente.read_holding_registers(457,1) datos = estado.registers[0] print ("Registro 457") #print datos print decbin(datos) if (datos & 1): print ("E1 Start") if (datos & 2): print ("E2 Close O2") if (datos & 4): print ("E3 Free") if (datos & 8): print ("E4 Stop O1 O2") if (datos & 16): print ("E5 Reset") if (datos & 32): print ("E6 L/R") # VENT_Estados = Tk() # VENT_Estados.title("LTMR Tesys T - Estados -") # VENT_Estados.minsize(300,200) # texto = Label(VENT_Estados, text="Estado LTMR") # texto.pack() #------------------------------------------------------- cliente = ModbusTcpClient('192.168.4.169','502') cliente.connect() #------------------------------------------------------- # MAIN LOOP VENTANA = Tk() VENTANA.title("LTMR Tesys T") VENTANA.minsize(250,200) texto = Label(VENTANA, text="Comandos LTMR") texto.pack() ON_Img = PhotoImage(file="ON.png") imagen = ON_Img.subsample(10,10) ON_a = Button(VENTANA, text="ON 0", width=8, height=1, command=FUNC_ON_a, bg="green") ON_a.pack() OFF_a = Button(VENTANA, text="OFF 0", width=8, height=1, command=FUNC_OFF_a) OFF_a.pack() ON_b = Button(VENTANA, text="ON 1", width=8, height=1, command=FUNC_ON_b, bg="green") ON_b.pack() #--------------------------------------------------------------------------- entrada = StringVar() corriente = Entry(VENTANA, width=10, textvariable=entrada) corriente.pack() OFF_b = Button(VENTANA, text="WRITE", width=8, height=1, command=FUNC_WRITE) OFF_b.pack() #--------------------------------------------------------------------------- RESET = Button(VENTANA, text="Reset", width=8, height=1, command=FUNC_Reset) RESET.pack() ESTADO = Button(VENTANA, text="Estado", width=8, height=1, command=FUNC_Estado) ESTADO.pack() KILL = Button(VENTANA, text="Kill", width=8, height=1, bg="red", command=FUNC_Kill) KILL.pack() VENTANA.mainloop() cliente.close()