Click here to Skip to main content
15,894,410 members
Articles / Web Development / ASP.NET
Article

ModBus for Delta DVP Series PLC

Rate me:
Please Sign up or sign in to vote.
3.71/5 (6 votes)
31 Mar 2009CPOL2 min read 102.7K   12.7K   24   27
This project is an easy test for anyone wishing to test modbus communications

Introduction

I was required to connect a small touch screen to a delta PLC and used ModBus as the protocol. This caused me to want to know more about protocols. The project is written using ASCII not RTU as I do not know how to format the code to RTU yet, but be assured I will get there soon.

Background

For a more in depth view of ModBus, there are numerous sites and they will give you more in depth description of modbus. I am only going to explain my code and the functions available. I used the Delta DVP Series PLC communication protocol ver 1.0 as a reference. This is downloadable from their website.

Using the Code

The two main classes that we use here are clsComms and clsInputValidation.

Any ModBus transmit word consists of the following:

: + Controller Address + Register Address +
   ModBus Operation + Data Sent + Check Bit + Carriage return + line feed

When data is entered, it needs to be hex. clsInputValidation.validHex() will make sure of this:

C#
//Check that only valid HEX keys are entered into the text boxes
  public static void ValidHex(string register)
          {
              Regex rx = new Regex("[^A-F|^0-9|^ |^\t]");
              if (rx.IsMatch(register))

                  throw new Exception("Enter a valid HEX number");
          }

The checkbit is the 2's compliment of the total of all the words. clsComms.checkSum() will do this for you:

C#
public static string checkSum(string writeUncheck)
          {
              char[] hexArray = new char[writeUncheck.Length];
              hexArray = writeUncheck.ToCharArray();
              int decNum = 0, decNumMSB= 0, decNumLSB = 0;
              int decByte, decByteTotal = 0;

              bool msb = true;

              for( int t = 0; t <= hexArray.GetUpperBound(0); t++)
              {
                  if((hexArray[t]>= 48) && (hexArray[t] <= 57))

                      decNum = (hexArray[t] - 48);

                  else if ( (hexArray[t] >= 65) & (hexArray[t] <= 70))
                      decNum = 10 + (hexArray[t] - 65);

                  if (msb)
                  {
                      decNumMSB = decNum * 16;
                      msb = false;
                  }
                  else
                  {
                      decNumLSB = decNum;
                      msb = true;
                  }
                  if (msb)
                  {
                      decByte = decNumMSB + decNumLSB;
                      decByteTotal += decByte;
                  }
              }

              decByteTotal = (255 - decByteTotal) + 1;
              decByteTotal = decByteTotal & 255;

              int a, b = 0;

              string hexByte = "", hexTotal = "";
              double i;

              for (i = 0; decByteTotal > 0; i++)
              {
                  b = Convert.ToInt32(System.Math.Pow(16.0, i));
                  a = decByteTotal % 16;
                  decByteTotal /= 16;
                  if (a <= 9)
                      hexByte = a.ToString();
                  else
                  {
                      switch (a)
                      {
                          case 10:
                              hexByte = "A";
                              break;
                          case 11:
                              hexByte = "B";
                              break;
                          case 12:
                              hexByte = "C";
                              break;
                          case 13:
                              hexByte = "D";
                              break;
                          case 14:
                              hexByte = "E";
                              break;
                          case 15:
                              hexByte = "F";
                              break;
                      }
                  }
                  hexTotal = String.Concat(hexByte, hexTotal);
              }

              return hexTotal;
          }

The clsComms.Read() will take the transmit string and send it to the PLC returning either a response from the PLC or an error message, such as time out.

Other features in clsComms include code that will convert strings to a double, in this way you can convert the part of code of interest to you into a decimal value, making it easier to read for the end user and code which converts an integer into a valid HEX number, again making user interfacing easier.

Points of Interest

This project was an excellent learning curve for me as I learned C# as well as the protocol in the process. My intention is to later develop a ModBus IO server which I can use to update tags in a mini SCADA system. I hope there are people out there who have similar projects and can share information with me.

History

  • 31st March, 2009: Initial version

License

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


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

Comments and Discussions

 
QuestionPlease help me Pin
Member 128278441-Nov-16 20:14
Member 128278441-Nov-16 20:14 
QuestionHello Shaun : need ladder logic of PLC config Pls mail Pin
Member 1204082020-Sep-16 7:10
Member 1204082020-Sep-16 7:10 
QuestionReading Loop Area Pin
TTNYUM24-Sep-15 1:34
TTNYUM24-Sep-15 1:34 
AnswerRe: Reading Loop Area Pin
ShaunThomas4-Nov-15 0:43
ShaunThomas4-Nov-15 0:43 
GeneralRe: Reading Loop Area Pin
TTNYUM4-Nov-15 3:34
TTNYUM4-Nov-15 3:34 
GeneralRe: Reading Loop Area Pin
ShaunThomas6-Nov-15 1:24
ShaunThomas6-Nov-15 1:24 
GeneralRe: Reading Loop Area Pin
TTNYUM12-Nov-15 23:58
TTNYUM12-Nov-15 23:58 
QuestionThank you Pin
Member 1078511727-May-14 0:44
Member 1078511727-May-14 0:44 
QuestionModbus on RTU Pin
shrirampendse20-Sep-11 15:11
shrirampendse20-Sep-11 15:11 
AnswerRe: Modbus on RTU Pin
ShaunThomas19-Oct-11 9:58
ShaunThomas19-Oct-11 9:58 
GeneralRe: Modbus on RTU Pin
KaelLoh30-May-12 14:40
KaelLoh30-May-12 14:40 
GeneralNeed Your Help Shaun Pin
rezaasgharian31-Dec-10 2:39
rezaasgharian31-Dec-10 2:39 
GeneralMy vote of 5 Pin
rezaasgharian31-Dec-10 2:31
rezaasgharian31-Dec-10 2:31 
NewsLRC Checksum Function Pin
Ozan Müyesseroğlu2-May-10 20:12
Ozan Müyesseroğlu2-May-10 20:12 
GeneralRe: LRC Checksum Function Pin
ShaunThomas2-May-10 23:01
ShaunThomas2-May-10 23:01 
QuestionTks a lot ShaunThomas... Pin
SAMCALEB12-Sep-09 1:26
SAMCALEB12-Sep-09 1:26 
AnswerRe: Tks a lot ShaunThomas... Pin
ShaunThomas12-Sep-09 23:23
ShaunThomas12-Sep-09 23:23 
GeneralTks a lot ShaunThomas... Pin
SAMCALEB13-Sep-09 18:48
SAMCALEB13-Sep-09 18:48 
GeneralRe: Tks a lot ShaunThomas... Pin
Ryno Engelbrecht14-Jun-10 21:05
Ryno Engelbrecht14-Jun-10 21:05 
GeneralBug :) Pin
Penko Mitev31-May-09 2:09
Penko Mitev31-May-09 2:09 
GeneralRe: Bug :) Pin
ShaunThomas31-May-09 3:27
ShaunThomas31-May-09 3:27 
GeneralRe: Bug :) Pin
Penko Mitev31-May-09 3:56
Penko Mitev31-May-09 3:56 
GeneralRe: Bug :) Pin
ShaunThomas31-May-09 4:21
ShaunThomas31-May-09 4:21 
GeneralNmodbus Pin
rallets7-Apr-09 9:39
rallets7-Apr-09 9:39 
GeneralRe: Nmodbus Pin
ShaunThomas8-Apr-09 6:35
ShaunThomas8-Apr-09 6:35 

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.