Click here to Skip to main content
15,887,267 members
Articles / Programming Languages / XML
Tip/Trick

Modbus TCP class

Rate me:
Please Sign up or sign in to vote.
4.89/5 (84 votes)
23 May 2014CPOL 1.7M   60.4K   154   171
A Modbus TCP class.

The project has moved to GIT

https://github.com/stephan1827/modbusTCP

Please download and ask questions from there.

License

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


Written By
Systems Engineer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
NewsProject move to GIT Pin
Stephan Stricker3-Aug-18 22:59
Stephan Stricker3-Aug-18 22:59 
Suggestionmodbus Pin
Member 118375762-Jul-18 19:51
Member 118375762-Jul-18 19:51 
QuestionQuestion on Modbus TCP Pin
shwethasp4-Apr-18 5:50
shwethasp4-Apr-18 5:50 
QuestionModBus TCP class for C++ or C Pin
Emrah Duatepe11-Jan-18 2:40
Emrah Duatepe11-Jan-18 2:40 
AnswerRe: ModBus TCP class for C++ or C Pin
Stephan Stricker12-Jan-18 19:16
Stephan Stricker12-Jan-18 19:16 
QuestionQuestion On "No Response" Pin
Member 1270979225-Nov-17 16:56
Member 1270979225-Nov-17 16:56 
AnswerRe: Question On "No Response" Pin
Stephan Stricker8-Jan-18 21:58
Stephan Stricker8-Jan-18 21:58 
GeneralRe: Question On "No Response" Pin
Member 1270979218-Feb-18 14:05
Member 1270979218-Feb-18 14:05 
QuestionNo connection could be made... Pin
Andrw_S16-Nov-17 5:40
Andrw_S16-Nov-17 5:40 
AnswerRe: No connection could be made... Pin
Member 1270979226-Nov-17 14:57
Member 1270979226-Nov-17 14:57 
QuestionAccess SQL Database and send to Modbus class Pin
ganesh199616-Aug-17 4:12
ganesh199616-Aug-17 4:12 
Hi,

I have the following problem.

I have a application which can send input data (strings, ints and bools) to a machine, using OPCdotNETlib. Now I also wanted to add a new machine so that my program can send its values using Modbus TCP. 

This example works perfect.

Then I changed some things in this example:

The radiobutton always set to word.
Unit always 1
Start address always 0
Size always 16
Only using "Read Holding Register" and "Write Multiple Register"
Connect button is now not important anymore, since it will check for connection when you click "Read Holding Register"

It still works perfect.

I tried to add this same code and principle to my program, using the already excisting Modbus class, but instead of sending the values in the TexBoxes, it has to send values from the SQL database in my program. Here is where it goes wrong.

If you look in the sample program to GetData, you can see the way the values are send. In my case, I edited the code to:

public byte[] GetData(int num, int ARecipe)
        {
            bool[] bits = new bool[num];
            byte[] data = new Byte[num];
            int[] word = new int[num];

            //check if there are any params
            if (!FParams.Any())
            {
                Console.WriteLine("");
                return null;
            }
            
            foreach (DParamvalue param in FModel.DParamvalues.AsQueryable().Where(p => p.recipeid == ARecipe))
            {
                int x = Convert.ToInt16(param.value);

                if ((x <= data.GetUpperBound(0)) && (param.value != ""))
                {
                    try { word[x] = Convert.ToInt16(param.value); }
                    catch (SystemException)
                    {
                        word[x] = Convert.ToUInt16(param.value);
                    }; //wegen/false = 0 en tellen/true = 1
                }
                else break;
            }

            data = new Byte[num * 2];
            for (int x = 0; x < num; x++)
            {
                byte[] dat = BitConverter.GetBytes((short)IPAddress.HostToNetworkOrder((short)word[x]));
                data[x * 2] = dat[0];
                data[x * 2 + 1] = dat[1];
            }

            
            return data;

        }

Where ARecipe = frecipeid (in the sql table). So for each ARecipe, it has to send the values of that specific frecipeid (which also can be 2, 3, 4 etc.).

Now the problem: my program does not send 5, 8 and 9 for example, but 0, 0 and 0. So the program is connected correctly and it can send data, but not the data I want it to send from the database.

How can I make sure it sends the data from the database?

I hope I explained it well enough and that someone can help me.

Ganesh

QuestionEnd Modbus connection Pin
ganesh199616-Aug-17 0:15
ganesh199616-Aug-17 0:15 
QuestionWriteSyncData method problem. Pin
mustafa_ozkn1-Aug-17 2:50
mustafa_ozkn1-Aug-17 2:50 
QuestionRegarding Main function Pin
Member 1325668213-Jun-17 2:21
Member 1325668213-Jun-17 2:21 
QuestionDELTA PLC Pin
Member 1268699815-Aug-16 12:54
Member 1268699815-Aug-16 12:54 
Questionwho to assigned the one address? Pin
Member 1264600227-Jul-16 2:42
Member 1264600227-Jul-16 2:42 
Questionmoddbus says errror ; illegal data adress! Pin
Member 125196883-Jun-16 0:54
Member 125196883-Jun-16 0:54 
AnswerRe: moddbus says errror ; illegal data adress! Pin
Garth J Lancaster3-Jun-16 1:26
professionalGarth J Lancaster3-Jun-16 1:26 
GeneralRe: moddbus says errror ; illegal data adress! Pin
Member 125196883-Jun-16 7:07
Member 125196883-Jun-16 7:07 
QuestionModbus Connection Issue, where to look? Pin
bwr53920-Apr-16 9:35
bwr53920-Apr-16 9:35 
AnswerSolved: Re: Modbus Connection Issue, where to look? Pin
bwr53920-Apr-16 13:54
bwr53920-Apr-16 13:54 
Questionobtaining values with a comma Pin
Member 1233685318-Feb-16 8:11
Member 1233685318-Feb-16 8:11 
QuestionConnection lost Problem Pin
megha_p14-Aug-15 0:29
megha_p14-Aug-15 0:29 
AnswerRe: Connection lost Problem Pin
Stephan Stricker17-Sep-15 3:02
Stephan Stricker17-Sep-15 3:02 
AnswerRe: Connection lost Problem Pin
Robert McCullough9-Jan-16 5:06
Robert McCullough9-Jan-16 5:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.