Shivansh Sharma
Shivansh Sharma

Reputation: 21

Is there a way to use Modbus Function Code 23 from server to write data via DataBank() with pyModbusTCP?

I am using pyModbusTCP library to exchange data with a tool. The tool only supports Modbus Function Code 23 (Read Write Registers) and can only act as a Modbus Client and a TCP Master. I am able to read the data from the tool successfully but I am not able to write/send the data back to the tool. The values to read are only there at register 2048. And values are supposed to be written at register address 0. Following is the code:

import random
from time import sleep
from pyModbusTCP.server import DataBank, ModbusServer
# instance of DataBank
data_bank = DataBank()

def int_to_bit(value):
    bin_str = format(value, '016b')
    return [int(bit) for bit in bin_str]

def bit_to_int(bin_list):
    bin_str = ''.join(str(bit) for bit in bin_list)
    return int(bin_str, 2)

def print_values(values):
    for i in range(len(values)):
        print(f"Register {i+1}: {values[i]}")

def user_input():
    user_input = input("Enter up to 16 bits (e.g. 1010101010101010): ")
    if not all(bit in '01' for bit in user_input) or len(user_input) > 16:
        print("Invalid input. Please enter a valid 16-bit binary number.")
        return user_input()
    bin_list = [int(bit) for bit in user_input.zfill(16)]
    value = bit_to_int(bin_list)
    data_bank.set_holding_registers(0, [value])
    print(f"{value} written to register 0.")
    print(f"{bin_list} written to register 0.")
        

# instance of ModbusServer
server = ModbusServer('192.168.88.253', 502, no_block=True, data_bank=data_bank)

# Initialize the holding registers to 0 for addresses 0 to 7 and 2048 to 2049
data_bank.set_holding_registers(0, [0]*2)
data_bank.set_holding_registers(2048, [0]*2)

try:
    print("Starting Server...")
    server.start()
    print("Server is online")
    
    previous_state = [0]*2  # to keep track of changes in registers 2048 to 2049
    
    while True:
        # Check for changes in the holding registers at address 2048
        current_state = data_bank.get_holding_registers(2048, 2)
        if current_state != previous_state:
            print("Values at registers 2048 to 2055 have changed:")
            print_values(current_state)
            register_0_value = data_bank.get_holding_registers(2048, 1)[0]
            bin_list = int_to_bit(register_0_value)
            print(f"Register 0 value in 16-bits: {bin_list}")
            previous_state = current_state  # update previous state

        user_input()

        sleep(2)
except Exception as e:
    print(f"An error occurred: {e}")

print("Shutdown server...")
server.stop()
print("Server is stopped")

I tried using different methods in user_input() function instead of set_holding_registers(), also tried with different inputs (like a list [0,1,1,1,0...] instead of [value] in the same method. I also tried using server._write_read_multiple_registers() method from server.py part of the library but did not entirely understand how it works. Any help would be great.

Upvotes: 2

Views: 88

Answers (0)

Related Questions