你的位置:首页 > ASP.net教程

[ASP.net教程]使用OPC的方式去连接PLC进行AB SLC


1、  必备软件

Rslinx classic 2.57

.net framework 2.0

VS2013

OS: win7 enterprise x64

2、  软件安装

2.1、安装RSlinx,安装时选择xp3兼容模式。

2.2、安装后启动RsLinx,对PLC进行配置。首先,建立驱动,驱动类型选择“EnterNet device”,将PLC设备的IP地址输入,如下图:

 

2.3、正确配置后,结果如下图,

 

在图中“192.168.1.254”的节点上,鼠标右键,可以看到datamonitor选项,点击这个选项,可以看到PLC中的各个数据。看到了,表示RsLinx和PLC已经连接成功,如果看不到表示连接失败。

3、  编写代码

3.1、首先建立C#工程;

3.2、导入三个DLL: OPCNetApi.com.dll, OpcNetApi.dll, OpcNetApi.

3.3、编程思路:

首先,建立程序(clinet)和OPC server的连接。注意OPC Server是安装有RsLinx的机器(127.0.0.1)而不是PLC的IP地址。

其次,连接建立成功之后,建立订阅组状态,建立订阅组;将要读取的item填入到订阅组中。

然后,就可以读取需要的数据了。

最后,释放资源。

3.4、具体代码如下:

public static void GetData(ref int zhValue, ref int nsValue, ref int deviceStopTime, ref int materialStopTime, ref int qualityStopTime)
{
try
{
Opc.Da.Server lt1_server = null;
Opc.URL lt1_url = null;
Opc.ConnectData lt1_connData = null;
Opc.Da.SubscriptionState lt1_state = null;
Opc.Da.Subscription lt1_subcription = null;

string url1 = "opcda://127.0.0.1/RSLinx Remote OPC Server/{a05bb6d5-2f8a-11d1-9bb0-080009d01446}";
lt1_url = 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
{
lt1_server.Connect(lt1_connData);
}
catch (Exception ex)
{
return;
}

//创建订阅组的状态
lt1_state = new Opc.Da.SubscriptionState();
lt1_state.Name = "LowTube1";
lt1_state.ServerHandle = null;
lt1_state.ClientHandle = Guid.NewGuid().ToString();
lt1_state.Active = false;
lt1_state.UpdateRate = 1000;
lt1_state.Deadband = 0;
lt1_state.Locale = null;

//创建订阅组
lt1_subcription = (Opc.Da.Subscription)lt1_server.CreateSubscription(lt1_state);

//内饰线数据采集I:2.2
//底盘线数据采集I:3.2
//当天设备故障时间T4:161.ACC
//当天物料求助时间T4:162.ACC
//当天品质求助时间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 = lt1_subcription.Read(lt1_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);

lt1_server.CancelSubscription(lt1_subcription);
lt1_server.Disconnect();
lt1_server.Dispose();
}
catch (Exception ex)
{
LogMessage.WriteLog(ex);
}
}