一、 必備軟件編程
Rslinx classic 2.57ui
.net framework 2.0url
VS2013.net
OS: win7 enterprise x64server
二、 軟件安裝ip
2.一、安裝RSlinx,安裝時選擇xp3兼容模式。ci
2.二、安裝後啓動RsLinx,對PLC進行配置。首先,創建驅動,驅動類型選擇「EnterNet device」,將PLC設備的IP地址輸入,以下圖:資源
2.三、正確配置後,結果以下圖,string
在圖中「192.168.1.254」的節點上,鼠標右鍵,能夠看到datamonitor選項,點擊這個選項,能夠看到PLC中的各個數據。看到了,表示RsLinx和PLC已經鏈接成功,若是看不到表示鏈接失敗。it
三、 編寫代碼
3.一、首先創建C#工程;
3.二、導入三個DLL: OPCNetApi.com.dll, OpcNetApi.dll, OpcNetApi.Xml.dll,這三個dll在rslinx安裝目錄下;
3.三、編程思路:
首先,創建程序(clinet)和OPC server的鏈接。注意OPC Server是安裝有RsLinx的機器(127.0.0.1)而不是PLC的IP地址。
其次,鏈接創建成功以後,創建訂閱組狀態,創建訂閱組;將要讀取的item填入到訂閱組中。
而後,就能夠讀取須要的數據了。
最後,釋放資源。
3.四、具體代碼以下:
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); } }