Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0
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?
Thanks
Posted 5-Jun-13 16:15pm

1 solution

Rate this: bad
good
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.
  Permalink  
Comments
sandra_tay at 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 at 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 at 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 at 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 at 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 at 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 at 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 at 5-Jun-13 22:00pm
   
NModbus is the one I use.
 
https://code.google.com/p/nmodbus/
sandra_tay at 5-Jun-13 22:18pm
   
By using this NModbus, is that using same way to builk conneciton?
Ron Beyer at 5-Jun-13 22:21pm
   
Here are examples on how to use the library:
http://web.archive.org/web/20090601225835/http://www.nmodbus.com/Samples.aspx
sandra_tay at 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
0 OriginalGriff 200
1 George Jonsson 175
2 Kornfeld Eliyahu Peter 169
3 PIEBALDconsult 110
4 Richard MacCutchan 85
0 OriginalGriff 6,080
1 DamithSL 4,648
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,624
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 5 Jun 2013
Copyright © CodeProject, 1999-2014
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