Click here to Skip to main content
13,043,092 members (62,367 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi, I'm writing a code which connect to Modbus. However, when I connect it and try to read coils it will show the error below:-

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond IPAddress

Below is the code that i try to connect and read coils:-
for (startAddress = 0; startAddress < 7; startAddress++)
    MBmaster = new Master(IPAddress, 502);
    MBmaster.ReadCoils(ID, startAddress, Length);

Any idea how to solve this problem?
Posted 5-Jun-13 15:15pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Move the MBmaster = new Master(...) out of the for loop, the device can probably only accept one connection from one host.
sandra_tay 5-Jun-13 21:23pm
I have try to remove it, but when i remove it, it cannot loop all the startAddress in the loop.
Ron Beyer 5-Jun-13 21:26pm
I think you may be confused on how the function works, why are you looping when you can read all 7 registers at one time? The function probably returns an array of bool[] which you can use to get all the values you need. The loop isn't needed at all, which is probably why its not working as you expect.
sandra_tay 5-Jun-13 21:38pm
I'm sorry that I still confuse with this. What you means is that I can just having " MBmaster.ReadCoils(ID, startAddress, Length); " to read all 7 registers?
Then what value should I pass in for startAddress?
Ron Beyer 5-Jun-13 21:42pm
Lets say you want to read coils 10000 through 10007, you pass in 0 for the start address (it automatically translates it to the right coil address), and for Length you pass in 7.
sandra_tay 5-Jun-13 21:49pm
I have try it, but it only return 1 value to me. My code is as below:
MBmaster.ReadCoils(ID, 0, 7);
Ron Beyer 5-Jun-13 21:52pm
What library are you using? this should be returning an array, like:

bool[] vals = MBmaster.ReadCoils(ID, 0, 7);

then vals should have 7 boolean values in it, that you can access like:

vals[0], vals[1], vals[2]..., vals[6]
sandra_tay 5-Jun-13 21:58pm
ModbusTCP, this is a free library get from Internet and the returning value is not array.
Do you have any others library?
Ron Beyer 5-Jun-13 22:00pm
NModbus is the one I use.
sandra_tay 5-Jun-13 22:18pm
By using this NModbus, is that using same way to builk conneciton?
Ron Beyer 5-Jun-13 22:21pm
Here are examples on how to use the library:
sandra_tay 5-Jun-13 22:43pm
Thanks for your help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 5 Jun 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100