Home > Instrument & Automation, Programming, Tips & Trick, Tutorial > Creating Dynamic Library for Modbus Serial in C#

Creating Dynamic Library for Modbus Serial in C#

December 9th, 2010 Leave a comment Go to comments

When we create and develop some Software Application, sometime we need to use some function more than once, or we need to use some function in another Application. therefor we need to build Dynamic Link Library (DLL). In C# programming, to develop library is just create a Class with static Function.

Step 1:
Open your Visual C# and then Create New project, File>Create New Project>
and your computer will displaying:

Select Class Library and then give the name in box name and click OK

step2:
After you complete step 1 this window will display like picture below.

you can rename Class1 to any name what you want in my example is ModbusLib.
don’t forget to save all project before, and give a name and folder.

Step-3:
You can create your function in class teritory, like picture or Script below.

This is Full script from picture above:

[code language=”csharp”]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MobusClassLibrary
{
public class ModbusLib
{
//Create Your Function here

//Function to get CRC Value
public static void GetCRC(byte[] message, ref byte[] CRC)
{
//Function expects a modbus message of any length as well as a 2 byte CRC array in which to
//return the CRC values:

ushort CRCFull = 0xFFFF;
byte CRCHigh = 0xFF, CRCLow = 0xFF;
char CRCLSB;

for (int i = 0; i < (message.Length) - 2; i++) { CRCFull = (ushort)(CRCFull ^ message[i]); for (int j = 0; j < 8; j++) { CRCLSB = (char)(CRCFull &amp;amp;amp;amp;amp;amp;amp;amp;amp;amp; 0x0001); CRCFull = (ushort)((CRCFull >> 1) &amp;amp;amp;amp;amp;amp;amp;amp;amp;amp; 0x7FFF);

if (CRCLSB == 1)
CRCFull = (ushort)(CRCFull ^ 0xA001);
}
}
CRC[1] = CRCHigh = (byte)((CRCFull >> 8) &amp;amp;amp;amp;amp;amp;amp;amp;amp;amp; 0xFF);
CRC[0] = CRCLow = (byte)(CRCFull &amp;amp;amp;amp;amp;amp;amp;amp;amp;amp; 0xFF);
}

//Function to convert HEX string Value to Byte Array
static byte[] HexStringToByteArray(string s)
{
s = s.Replace(” “, “”);
byte[] buffer = new byte[s.Length / 2];
for (int i = 0; i < s.Length; i += 2) buffer[i / 2] = (byte)Convert.ToByte(s.Substring(i, 2), 16); return buffer; } //Function to convert Byte Array Value to Hex String static string ByteArrayToHexString(byte[] data) { StringBuilder sb = new StringBuilder(data.Length * 3); foreach (byte b in data) sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' ')); return sb.ToString().ToUpper(); } //Function to create Modbus request message (MASTER to Slave) public static string ModbusFrame(string RTUadd, string fungsi, string regPertama, string panjangReg) { //RTU addrress int valInt1 = Convert.ToInt32(RTUadd); string hexOutput1 = String.Format("{0:X}", valInt1); if (hexOutput1.Length == 1) hexOutput1 = "0" + hexOutput1; //Function int valInt2 = Convert.ToInt32(fungsi); string hexOutput2 = String.Format("{0:X}", valInt2); if (hexOutput2.Length == 1) hexOutput2 = "0" + hexOutput2; //firstRegister high //firstRegister low int valInt3 = Convert.ToInt32(regPertama); valInt3 = Math.Abs(valInt3 - 40001); if (valInt3 > 0) valInt3–;
string hexOutput3 = String.Format(“{0:X}”, valInt3);
if (hexOutput3.Length == 1)
{
hexOutput3 = “000” + hexOutput3;
}
else if (hexOutput3.Length == 2)
{
hexOutput3 = “00” + hexOutput3;
}
else if (hexOutput3.Length == 3)
{
hexOutput3 = “0” + hexOutput3;
} //data dibentuk jadi 4 digit ex: 00 FF <- untuk 255 //Lenght int valInt4 = Convert.ToInt32(panjangReg); string hexOutput4 = String.Format("{0:X}", valInt4); if (hexOutput4.Length == 1) { hexOutput4 = "000" + hexOutput4; } else if (hexOutput4.Length == 2) { hexOutput4 = "00" + hexOutput4; } else if (hexOutput4.Length == 3) { hexOutput4 = "0" + hexOutput4; }//data dibentuk jadi 4 digit ex: 00 FF <- untuk 255 //dibentuk ulang lagi biar gk kacau stringnya hexOutput1 = String.Format("{0:X}", hexOutput1); hexOutput2 = String.Format("{0:X}", hexOutput2); hexOutput3 = String.Format("{0:X}", hexOutput3); hexOutput4 = String.Format("{0:X}", hexOutput4); byte[] CRC = new byte[2]; byte[] messagetos = new byte[6]; byte[] kumpul = new byte[8]; messagetos = HexStringToByteArray(hexOutput1 + hexOutput2 + hexOutput3 + hexOutput4); //dipindah array messagetos[] ke kumpul[] supaya CRC nya valid "byte[8]". //klo messegatos_nya diganti byte[6] jadi byte[8] pun tetep gk valid hasil CRC nya.. gk tau kenapa ^-^ kumpul[0] = messagetos[0]; kumpul[1] = messagetos[1]; kumpul[2] = messagetos[2]; kumpul[3] = messagetos[3]; kumpul[4] = messagetos[4]; kumpul[5] = messagetos[5]; //dapetin nilai CRC GetCRC(kumpul, ref CRC); string hexoutput5 = ByteArrayToHexString(CRC); //bentuk pesan Modbus nya... //modbus prtokol message: RTUaddress+Function+firstReg_HI+firstReg_LO+lenght_HI+lenght_LO+CRC1+CRC2 String datatosend = ModbusLib.ByteArrayToHexString(messagetos) + hexoutput5; //kirim data ke serial return (datatosend); } //Function to create Modbus respon message (SLAVE to Master) public static string bentukMSG(string[,] bufREG, int RTU, int FCode, int ReqReg, int panjang) { RTU = RTU - 1; string[] MODBUSmsg = new string[5 + (2 * panjang)]; byte[] byteMODBUSmsg = new byte[5 + (2 * panjang)]; string sendIT = ""; byteMODBUSmsg[0] = Convert.ToByte(RTU + 1); byteMODBUSmsg[1] = Convert.ToByte(FCode); byteMODBUSmsg[2] = Convert.ToByte(panjang * 2); for (int i = 0; i <= panjang; i++) { if (Convert.ToInt32(bufREG[RTU, i]) <= 255) { byteMODBUSmsg[3 + (i * 2)] = 0; byteMODBUSmsg[4 + (i * 2)] = Convert.ToByte(bufREG[RTU, i]); } else { string ValGridSTR = String.Format("{0:X}", bufREG[RTU, i]); byte[] ValGridByte = HexStringToByteArray(ValGridSTR); byteMODBUSmsg[3 + (i * 2)] = ValGridByte[0]; byteMODBUSmsg[4 + (i * 2)] = ValGridByte[1]; } } byte[] CRC = new byte[2]; ModbusLib.GetCRC(byteMODBUSmsg, ref CRC); byteMODBUSmsg[byteMODBUSmsg.Length - 2] = CRC[0]; byteMODBUSmsg[byteMODBUSmsg.Length - 1] = CRC[1]; sendIT = ByteArrayToHexString(byteMODBUSmsg); return sendIT; } } } [/code] After completing script and function, you can build your *.dll from your toolbar menu. Build>>Build Solution
and then check the folder where you save the project, and then enter the bin>release>your.dll

and now your DLL is ready to use by other Application.

Download Project: http://www.4shared.com/file/0dXc8qJi/MobusClassLibrary.html

Regards,
Rifqi Imanto (System Engineer)