Send Modbus custom request with TCP Socket

Hello, I have a solar inverter that have a custom protocol (VABUS of Bonfiglioli) and I need to send a custom request for read de power of the inverter with bytes in TCP. I don't want testing with a real inverter so i'm using ModRSIM that is a Modbus TCP Server simulator and i want to read a holding register with a script for test how send bytes to a TCP Server.

In python i have a script that works fine:

*import socket*
*import time*

*def trama_modbus():*

*    asdu = bytearray()*
*#2 bytes para el transaction id*
*    asdu.append(0x00)*
*    asdu.append(0x01)*
*#protocolo id, como es modbus esto es 00*
*    asdu.append(0x00)*
*    asdu.append(0x00)*
*#va tener una longitud de 6 registros*
*    asdu.append(0x00)*
*    asdu.append(0x06)*
*#el slave id es el 1*
*    asdu.append(0x01)*
*#como vamos a leer holding es la fc 3*
*    asdu.append(0x03)*
*#el primer registro a leer es el 3*
*    asdu.append(0x00)*
*    asdu.append(0x03)*
*#el numero de registros que se pide en bloque es 1*
*    asdu.append(0x00)*
*    asdu.append(0x01)*
*    return asdu*

*# Press the green button in the gutter to run the script.*
*if __name__ == '__main__':*

*    HOST = '127.0.0.1'  # IP del servidor*
*    PORT = 507  # Puerto de escucha del servidor*

*    data = trama_modbus()*
*    print(data)*
*    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)*
*    s.connect((HOST, PORT))*
*    s.send(data)*
*    time.sleep(1)*

*    resultado = s.recv(1024)*
*    print(resultado.hex())*
*    s.close()*

But I want do this in Ignition and I don't know how do it:

I think that the problem is the creation of the message but i don't know how create the message. I'm using Ignition perspective but im testing this in Script Console of designer.

What doesn't work with your Ignition script?

Sorry, I thought it wasn't working but I tried to write a value with FC = 6 and it works correctly, the problem is that I can't print the result correctly, which comes in ASCII characters.

I know that is ascii characters but i dont know how to print the result, in python i can print with print(resultado.hex()) but here in ignition i don't know

Please see Wiki - how to post code on this forum.

Sorry,I put it in an image so that the result of the script could be seen.

this is the code:

import socket
import time

def trama_modbus():

    asdu = bytearray()
#2 bytes para el transaction id
    asdu.append(0x00)
    asdu.append(0x01)
#protocolo id, como es modbus esto es 00
    asdu.append(0x00)
    asdu.append(0x00)
#va tener una longitud de 6 registros
    asdu.append(0x00)
    asdu.append(0x06)
#el slave id es el 1
    asdu.append(0x01)
#como vamos a leer holding es la fc 3
    asdu.append(0x03)
#el primer registro a leer es el 3
    asdu.append(0x00)
    asdu.append(0x03)
#el numero de registros que se pide en bloque es 1
    asdu.append(0x00)
    asdu.append(0x01)
    return asdu

# Press the green button in the gutter to run the script.
if __name__ == '__main__':

    HOST = '127.0.0.1'  # IP del servidor
    PORT = 507  # Puerto de escucha del servidor

    data = trama_modbus()

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HOST, PORT))
    s.send(data)
    time.sleep(1)

    resultado = s.recv(1024)
    for i in resultado:
    	print(i)
    s.close()

Problem solved, I use ord(i) in the loop and works.

Thanks!