2

Recently I have been tasked with a project involving reading Modbus data, specifically with the pymodbus package. before diving into this project I wanted to simulate some reading and writing of Modbus data (without having to use a machine) with python. Using the sample data they have on their homepage, I try to write using the client but encounter a Modbus error

Ive tried looking into the matter to see what I could dig up wonder if I need a server to write or read. However I am unsure as with my experiences with socket and serial I just needed an established connection with the correct port to simply write (however I understand that Modbus is different).

Here is the code

client = ModbusTcpClient('localhost')
client.write_coil(1, True)
result = client.read_coils(1,1)
print(result.bits[0])
client.close()

and here is my error "pymodbus.exceptions.ConnectionException: Modbus Error: [Connection] Failed to connect[ModbusTcpClient(localhost:502)]"

I expect the output to simply write Modbus without needing anything no necessarily listen on the other end, but instead I keep getting an error when trying to connect/write. (sorry if this is hard to comprehend, my brain is all over the place and I am extremely new to Modbus in general).

1 Answer 1

3

If you want to send Modbus queries and you don't have any Modbus hardware yet you need to run a dummy Modbus server on your computer.

You can take a look at the examples.

You might also need to add a rule to your firewall for port 502. If you are on Linux you can just switch ports to a higher number like 5020 on both ends to avoid this problem.

Sign up to request clarification or add additional context in comments.

1 Comment

Great thanks! it worked perfectly after editing my firewall rules.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.