Using OPC to connect PLC for AB SLC-5_04 data acquisition, plcslc-5_04
1. Required Software
Rslinex classic 2.57
. Net framework 2.0
VS2013
OS: win7 enterprise x64
2. Software Installation
2.1 install RSlinx and select xp3 compatibility mode during installation.
2.2 After installation, start RsLinx and configure the PLC. First, create a driver, select "EnterNet device" for the driver type, and enter the IP address of the PLC device, for example:
2.3 After the configuration is correct, the result is as follows,
Right-click the "192.168.1.254" node in the figure and you can see the datamonitor option. Click this option to view the data in the PLC. If the result is displayed, RsLinx and PLC are connected successfully. If the result is not displayed, the connection fails.
3. write code
3.1 first, establish the C # project;
3.2 import three DLL files: OPCNetApi.com. dll, OpcNetApi. dll, and OpcNetApi. Xml. dll. These three dll files are under the rslinx installation directory;
3.3 programming ideas:
First, establish the connection between the program (clinet) and the OPC server. Note that the OPC Server is a machine installed with RsLinx (127.0.0.1) instead of the IP address of the PLC.
Second, after the connection is established successfully, the subscription group status is established and the subscription group is established. The items to be read are filled in the subscription group.
Then, you can read the required data.
Finally, release the resource.
3.4. The Code is as follows:
Public static void GetData (ref int zhValue, ref int nsValue, ref int deviceStopTime, ref int materialStopTime, ref int qualityStopTime)
{
Try
{
Opc. Da. Server lt;server = null;
Opc. URL lt;url = null;
Opc. ConnectData lt;conndata = null;
Opc. Da. SubscriptionState lt;state = null;
Opc. Da. subpartition lt;subcription = null;
String url1 = "opcda: // 127.0.0.1/RSLinx Remote OPC Server/{a05bb6d5-2f8a-11d1-9bb0-080009d01446 }";
Lt420url = new Opc. URL (url1 );
Lt1_server = new Opc. Da. Server (new OpcCom. Factory (), lt1_url );
System. Net. NetworkCredential credential = null;
System. Net. WebProxy proxy = null;
Lt1_connData = new Opc. ConnectData (credential, proxy );
Try
{
Lt;server.connect (lt;conndata );
}
Catch (Exception ex)
{
Return;
}
// Create the status of the subscription Group
Lt1_state = new Opc. Da. SubscriptionState ();
Lt;state.name = "LowTube1 ";
Lt1_state.ServerHandle = null;
Lt1_state.ClientHandle = Guid. NewGuid (). ToString ();
Lt420state.active = false;
Lt420state.updaterate = 1000;
Lt420state.deadband = 0;
Lt420state.locale = null;
// Create a subscription Group
Lt;subcription = (Opc. Da. subscribe) lt;server.createsub.pdf (lt;state );
// Data collection for the interior line I: 2.2
// Chassis line data collection I: 3.2
// Device failure time of the day T4: 161.ACC
// Ask for help on the day. T4: 162.ACC
// Quality help time of the day T4: 163.ACC
String [] item_names_ary = new string [] {"[UNTITLED] I: 2.2", "[UNTITLED] I: 3.2 ",
"[UNTITLED] T4: 161.ACC"," [UNTITLED] T4: 162.ACC", "[UNTITLED] T4: 163.ACC "};
Opc. Da. Item [] readItems = new Opc. Da. Item [item_names_ary.Length];
For (int ii = 0; ii <item_names_ary.Length; ii ++)
{
Opc. Da. Item model_item = null;
Opc. ItemIdentifier model_id = new Opc. ItemIdentifier (null, item_names_ary [ii]);
Model_item = new Opc. Da. Item (model_id );
Model_item.ReqType = null;
Model_item.MaxAge = 0;
Model_item.MaxAgeSpecified = false;
Model_item.Active = false;
Model_item.ActiveSpecified = false;
Model_item.SamplingRate = 0;
Model_item.SamplingRateSpecified = false;
Model_item.EnableBuffering = false;
Model_item.EnableBufferingSpecified = false;
Model_item.ClientHandle = Guid. NewGuid (). ToString ();
ReadItems [ii] = model_item;
}
Opc. Da. ItemResult [] m_r = lt1_subcription.AddItems (readItems );
Opc. Da. ItemValueResult [] results = lt;subcription.read (lt;subcription.items );
NsValue = Convert. ToInt32 (results [0]. Value );
ZhValue = Convert. ToInt32 (results [1]. Value );
DeviceStopTime = Convert. ToInt32 (results [2]. Value );
MaterialStopTime = Convert. ToInt32 (results [3]. Value );
QualityStopTime = Convert. ToInt32 (results [4]. Value );
Lt420server.cancelsubtion (lt420subcription );
Lt;server.disconnect ();
Lt;server.dispose ();
}
Catch (Exception ex)
{
LogMessage. WriteLog (ex );
}
}