0% found this document useful (0 votes)
90 views3 pages

Modbus Code

This document describes a PLC controller program that communicates with a PLC using Modbus RTU protocol. It defines variables to represent PLC I/O bits and registers, creates objects for network communication, controller speed/time, and Modbus RTU communication. The program continuously scans to read and write PLC registers using the Modbus protocol, handling tasks like reading/writing words and coils in a loop.

Uploaded by

Urvish Shah
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
90 views3 pages

Modbus Code

This document describes a PLC controller program that communicates with a PLC using Modbus RTU protocol. It defines variables to represent PLC I/O bits and registers, creates objects for network communication, controller speed/time, and Modbus RTU communication. The program continuously scans to read and write PLC registers using the Modbus protocol, handling tasks like reading/writing words and coils in a loop.

Uploaded by

Urvish Shah
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 3

/*

PLC with Modbus RTU protocol


Function Code
(0x01) Read Coils
(0x02) Read Discrete Inputs
(0x03) Read Holding Registers
(0x04) Read Input Registers
(0x05) Write Single Coil
(0x06) Write Single Register
(0x0F) Write Multiple Coils
(0x10) Write Multiple registers
(0x14) Read File Record
(0x16) Mask Write Register
(0x17) Read/Write Multiple registers
(0x2B) Read Device Identification
Modbus RTU Framing: [ID:1][Fun:1][Data:N][CRC:2]
*/
using System;
using System.Collections;
using System.Text;
using VM;
//================================================================== For main pr
ogram, see main.cs
class Controller{
//-------------------------------------------------------------> Define Vari
ables
static string[] Bits1={"di0","di1","di2","di3" ,"di4","di5","di6","di7","di
8","di9","di10","di11","di12" ;,"di13","di14","di15"};
static string[] Bits2={"do0","do1","do2","do3" ,"do4","do5","do6","do7","do
8","do9","do10","do11","do12" ;,"do13","do14","do15"};
static string[] VarName={"WordR","WordW","COMM","Sp eed","Second"};
static VmModbusRTU PLC;
static double timeout=1.0;
//-------------------------------------------------------------> Create Obje
cts
public static void create(ArrayList L, VmDataArray D){
D.Add(Bits1);D.Add(Bits2);D.Add(VarName);
L.Add(new VmNetServer (D,8000,null)); //-------------------> Net: Port 8
000
L.Add(new VmSpeed (D,"Speed")); //-------------------> Conterler S
peed
L.Add(new VmSecond (D,"Second")); //-------------------> Controller
Time
L.Add(new VmWordToBits(D,"WordR",Bits1)); //-------------> Word->Bits
L.Add(new VmBitsToWord(D,Bits2,"WordW")); //-------------> Bits->Word
L.Add(new VmCommPort(D, "COMM", //-------------> 232
1, // 1,2....
9600, // 110,300,600,1200,2400,4800,9600,14400,19200,38400,56000,5
7600,115200,1 28000,256000
8, // Number of bits/byte, 4-8
0, // 0-4=no,odd,even,mark,space
0 // 0,1,2 = 1, 1.5, 2 Stop bits
));
L.Add(PLC=new VmModbusRTU(D, "COMM",timeout));
L.Add(new VmTimerCallback(D,0.05, new VmTimerCallback_Method(scan))); //
Scan
}
//-------------------------------------------------------------> Initialize
public static void first(VmDataArray D){}
//-------------------------------------------------------------> Loop
public static void loop(VmDataArray D){}
//-------------------------------------------------------------> Clean
public static void last(VmDataArray D){}
//-------------------------------------------------------------> Scan
static void scan(VmDataArray D){ //Timer function
Communication(D);
Console.Write("Speed: {0} Time: {1} {2} {3} \r",
D["Speed"],
D["Second"].ToString("F2"),
((int)D["WordR"]).ToString("X4"),
((int)D["WordW"]).ToString("X4")
);
}
//----------------------------
//Modbus RTU Framing: [ID][Fun][Data]{CRC}
static byte PLCID=1; //PLC ID
static byte[] B=new byte[100]; //Buffer for sending and receiving
static int Task=1; //current communication task
static int Step=1; //current step
//----------------------------
static void Communication(VmDataArray D){
bool go_next=true;
switch(Task){
case 1: //Read a word
switch(Step){
case 1:
B[0]=0; //Starting Address Hi
B[1]=0; //Starting Address Lo
B[2]=0; //No. of Points Hi
B[3]=16; //No. of Points Lo
PLC.Send(PLCID,1,B,4); //Address, Function Code, Data,
Data Length
break;
case 2:
if(PLC.Receive(B,3)!=0){
D["WordR"]=B[2]*256+B[1]; //Convert to a word
}
else go_next=false;
break;
}
break;
case 2: //Write a word
switch(Step){
case 1:
B[0]=0; //Coil Addre
ss Hi
B[1]=0; //Coil Addre
ss Lo
B[2]=0; //Quantity o
f Coils Hi
B[3]=16; //Quantity o
f Coils Lo
B[4]=2; //Byte Count
B[5]=(byte)(((int)D["WordW"])&0xFF); //Data
B[6]=(byte)(((int)(D["WordW"]/256))&0xFF); //Data
PLC.Send(PLCID,15,B,7); //Address, F
unction Code, Data, Data Length
break;
case 2:
if(PLC.Receive(B,4)==0) go_next=false;
break;
}
break;
}
if(go_next){
Step++;
if(Step>2){ //Total steps in one communication task
Step=1;
Task++;
if(Task>2){ //Total number of communication tasks
Task=1;
}
}
}
if(PLC.timeout()){ PLC.Reset(); Step=1; Console.WriteLine("\nTimeout. No
responsr from COMM port");}
}
//--------------------------------------------------------> End
}
//================================================================= ============
More C# and VB source code for Automation in the following link
http://members.optushome.com.au/vmautomation/Software/vm/frame/contr oller2_n.ht
m
http://members.optusho

You might also like