[Python] How to make RTU Instrument.write_register function write with socket but not pymodbus...

Discussão em 'Python' iniciado por Stack, Julho 9, 2025.

  1. Stack

    Stack Membro Participativo

    Some base first on what i am doing how i am doing : I have an IOT device which is connected to a meter, this iot device send data to my server and i coded it in python and doing import socket module for client-server connection and communication. (I don't have to use pyModbus)

    I have to write some value at the register address in meter. (Reading register at address works fine).

    How i am receiving and sending data at the moment ?

    Receiving :

    connection, client_address = socket.accept() //socket coming from 'import socket'
    connection.recv(20) // receives in a while loop


    After receiving data i convert it in the form that i need by get_command_response function from Instrument class. (The instrument here is instance of Instrument class by Jonas Berg from https://github.com/pyhys/minimalmodbus using 2019 , old version, not latest 2023 but doesn't matter)

    convertedDataResponse = self.instrument.get_command_response(
    data_from_socket,
    self.current_func_code


    Sending : Sending works fine when i have to read the register at mentioned address of register in meter, This works fine. A snippet of how i do this is below:

    dataToSend, funcCode = self.instrument.read_registers(
    registerAddress, //register address to be read
    numberOfRegisters, //total registers
    functioncode=function_code // 3 or 4 mainly
    )
    self.connection.sendall(dataToSend) //I send data request to read the address of register to server, it works fine because i get correct expected response of this sent data in connection.recv(..).


    Where is the problem now ?

    Till now i am reading the register using instrument.read_registers(..) which had a return type (dataToSend) and i sent it to server by doing connection.sendall(dataToSend) and that worked. Now i have to write to some register and according to Jonas berg socket minimal modbus interface the function to write register (instrument.write_register()) do not have any return type, so i don't know how and what to send to server (connection.sendall(WhatToSendHere)) so that server know the intentions of writing register and make write to that address with the value i request to write.

    WhatToSendHere=self.instrument.write_register(17004,200.0,2,16,False)
    self.connection.sendall(WhatToSendHere)


    Most of the example on google uses PyModbus and ModbusClient class provides direct interface to write register, but i have to use socket module only, and send the command/data to server using connection.sendall(**) or some similar function. (As i explain it works for reading register because of return type of read_registers function that i can use to send to server, but not able to make it for write_register function using this interface of minimalModbus by jonas berg)

    Or is there any other function i can use from MinimalModbus to write to register using socket module to send intention of writing the register to server ?

    Continue reading...

Compartilhe esta Página