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.Xml.dll,這三個dll在rslinx安裝目錄下;
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);
}
}