AdvancedClimateSystems / uModbus

Python implementation of the Modbus protocol.
Mozilla Public License 2.0
211 stars 81 forks source link

illegalDataAddressError while writing single or multiple registers TCP #126

Closed I-Tingya closed 1 year ago

I-Tingya commented 1 year ago

Hi Sir,

I have installed umodbus on raspberry pi 3B+

Default simple tcp client and server programs in the example directory are working fine. I extended client code it to add a request to write single register it gave me illegalDataAddressError. After which I tried multiple registers write request which resulted in the same error.

I am trying to write the register on the address 5. Server code is default where it's accepting address range from 0 to 9.

CLIENT SIDE snip: message = tcp.write_multiple_coils(slave_id=1, starting_address=1, values=[1, 0, 1, 1]) read_message = tcp.read_coils(slave_id=1, starting_address=1, quantity=4) write_register=tcp.write_multiple_registers(slave_id=1, starting_address=5, values=[23,45]) with create_connection((host, port)) as sock:

Response depends on Modbus function code. This particular returns the

# amount of coils written, in this case it is.
print('writing to the server...')
response = tcp.send_message(message, sock)
print('reading from the server...')
response = tcp.send_message(read_message, sock)
print(response)
print('writing registers...')
response = tcp.send_message(write_register, sock)
print(response)

SERVER SIDE snip:

@app.route(slave_ids=[1], function_codes=[5, 15], addresses=list(range(0, 5000))) def write_data_store(slave_id, function_code, address, value): """" Set value for address. """ print('writing at address '+str(address)+' with value '+str(value)) data_store[address] = value

if name == 'main': try: app.serve_forever() finally: app.shutdown() app.server_close()

Thank you so much for building such an open source package!

I-Tingya commented 1 year ago

I figured, had to expand on the @app.route in the simple_tcp_server.py to include the function codes to [5,6,15] from [5,15].

Thanks for the library!