Accessing PLC Tags From C#
So you want to access Allen Bradley (Rockwell) PLC tags for use in your own C# Projects? Read on!
A long time ago I developed a wrapper class to expose the functionality of the OPC Automation Wrapper in a simple manner for inclusion in your own C# projects.
Prerequisites
You will first need the following items to make this work...
regsvr32 OPCDAAuto.dll
Including the wrapper class
Make sure to include my wrapper class in your own program. Find it here.
Using the wrapper class
First instantiate the wrapper class...
public RSLinxConnection RSLinx = new RSLinxConnection();
You can now reference it as RSLinx in your program.
Next, connect your program to RSLinx...
RSLinx.connect();
You can disconnect it when your done with it later...
RSLinx.disconnect();
Next, create a new group to hold the tags you want to read. The name is arbitary so call it what you want! I used "MyNewGroup"...
RSLinx.addGroup("MyNewGroup");
You can either add each tag you need by referencing the group name from above. I used "MyNewGroup"...
RSLinx.addTag("MyNewGroup","[PLC_NAME]One_Sec_ONS");
Or if you have permanent OPC topics in RSLinx use the groupID...
RSLinx.addTag(0, "[PLC_NAME]DateAndTime");
You can now read the values of the group using the readAll class function, storing the values in a tagValues datatype...
RSLinxConnection.TagGroupValues tagValues = RSLinx.readAll("MyNewGroup");
Extract the values using something similar to this which will add the name and value to a list box...
String entry;
listBox1.Items.Clear();
if (tagValues != null)
{
for (int i = 0; i < tagValues.getLength(); i++)
{
entry = tagValues.tagNames.GetValue(i) + " " + tagValues.tagValues.GetValue(i);
listBox1.Items.Add(entry);
}
}
If you just want to read a single tag then use which returns a string representation of the value...
RSLinx.readTag("[PLC_NAME]EM001_STATION1_DipTank_Comp")
Easy peasy! if you need the values in another format other than string use C# to convert them. This class is designed for ease of use to get you running.
Please see my github repository for the wrapper class source code, the dll and a C# example usage https://github.com/mattythorne/RSLinx-OPC-Connection-Wrapper/tree/master
Happy coding!
Controls and Automation Engineer
7 个月Very nice Matt, great info!