系统:windows 7旗舰版
软件:visual studio2010
simatic_net_8.2
opc库文件源代码:
using system;
using system.collections.generic;
using system.linq;
using system.text;
using opcautomation;
using system.net;
namespace opchelper
{
class opchelper:idisposable
{
#region version 3
#region 变量
private string strhostip;
private string strhostname;
private opcserver opcserver;
private opcgroups opcgroups;
private opcgroup opcgroup;
private list<int> itemhandleclient = new list<int>();
private list<int> itemhandleserver = new list<int>();
private list<string> itemnames = new list<string>();
private opcitems opcitems;
private opcitem opcitem;
private string itemnamepreappend;
private dictionary<string, string> itemvalues = new dictionary<string, string>();
public bool connected = false;
#endregion
#region 构造函数
public opchelper(string strhostip, string strhostname, int updaterate)
{
this.strhostip = strhostip;
this.strhostname = strhostname;
if (!creatserver())
return;
if (!connectserver(strhostip, strhostname))
return;
connected = true;
opcgroups = opcserver.opcgroups;
opcgroup = opcgroups.add(opcdotnetgroup);
setgroupproperty(opcgroup, updaterate);
opcgroup.datachange += new diopcgroupevent_datachangeeventhandler(opcgroup_datachange);
opcgroup.asyncwritecomplete += new diopcgroupevent_asyncwritecompleteeventhandler(opcgroup_asyncwritecomplete);
opcitems = opcgroup.opcitems;
}
#endregion
#region 私有方法
private bool creatserver()
{
try
{
opcserver = new opcserver();
}
catch
{
return false;
}
return true;
}
private bool connectserver(string strhostip, string strhostname)
{
try
{
opcserver.connect(strhostname, strhostip);
}
catch
{
return false;
}
return true;
}
private void setgroupproperty(opcgroup opcgroup, int updaterate)
{
opcgroup.isactive = convert.toboolean(true);
opcgroup.deadband = 0;
opcgroup.updaterate = updaterate;
opcgroup.issubscribed = convert.toboolean(true);
}
#endregion
#region 公有方法
public bool contains(string itemnamecontains)
{
foreach (string key in itemvalues.keys)
{
if (key == itemnamepreappend + itemnamecontains)
return true;
}
return false;
}
public void additems(string itemnamepreappend, string[] itemnamesadded)
{
this.itemnamepreappend = itemnamepreappend;
for (int i = 0; i < itemnamesadded.length; i++)
{
this.itemnames.add(this.itemnamepreappend + itemnamesadded[i]);
itemvalues.add(this.itemnamepreappend + itemnamesadded[i], );
}
for (int i = 0; i < itemnamesadded.length; i++)
{
itemhandleclient.add(itemhandleclient.count != 0 ? itemhandleclient[itemhandleclient.count - 1] + 1 : 1);
opcitem = opcitems.additem(this.itemnamepreappend + itemnamesadded[i], itemhandleclient[itemhandleclient.count - 1]);
itemhandleserver.add(opcitem.serverhandle);
}
}
public string[] getitemvalues(string[] getvaluesitemnames)
{
string[] getedvalues = new string[getvaluesitemnames.length];
for (int i = 0; i < getvaluesitemnames.length; i++)
{
if (contains(getvaluesitemnames[i]))
itemvalues.trygetvalue(itemnamepreappend + getvaluesitemnames[i], out getedvalues[i]);
}
return getedvalues;
}
public void asyncwrite(string[] writeitemnames, string[] writeitemvalues)
{
opcitem[] bitem = new opcitem[writeitemnames.length];
for (int i = 0; i < writeitemnames.length; i++)
{
for (int j = 0; j < itemnames.count; j++)
{
if (itemnames[j] == itemnamepreappend + writeitemnames[i])
{
bitem[i] = opcitems.getopcitem(itemhandleserver[j]);
break;
}
}
}
int[] temp = new int[writeitemnames.length + 1];
temp[0] = 0;
for (int i = 1; i < writeitemnames.length + 1; i++)
{
temp[i] = bitem[i - 1].serverhandle;
}
array serverhandles = (array)temp;
object[] valuetemp = new object[writeitemnames.length + 1];
valuetemp[0] = ;
for (int i = 1; i < writeitemnames.length + 1; i++)
{
valuetemp[i] = writeitemvalues[i - 1];
}
array values = (array)valuetemp;
array errors;
int cancelid;
opcgroup.asyncwrite(writeitemnames.length, ref serverhandles, ref values, out errors, 2009, out cancelid);
gc.collect();
}
public void syncwrite(string[] writeitemnames, string[] writeitemvalues)
{
opcitem[] bitem = new opcitem[writeitemnames.length];
for (int i = 0; i < writeitemnames.length; i++)
{
for (int j = 0; j < itemnames.count; j++)
{
if (itemnames[j] == itemnamepreappend + writeitemnames[i])
{
bitem[i] = opcitems.getopcitem(itemhandleserver[j]);
break;
}
}
}
int[] temp = new int[writeitemnames.length + 1];
temp[0] = 0;
for (int i = 1; i < writeitemnames.length + 1; i++)
{
temp[i] = bitem[i - 1].serverhandle;
}
array serverhandles = (array)temp;
object[] valuetemp = new object[writeitemnames.length + 1];
valuetemp[0] = ;
for (int i = 1; i < writeitemnames.length + 1; i++)
{
valuetemp[i] = writeitemvalues[i - 1];
}
array values = (array)valuetemp;
array errors;
opcgroup.syncwrite(writeitemnames.length, ref serverhandles, ref values, out errors);
gc.collect();
}
public void dispose()
{
if (opcgroup != null)
{
opcgroup.datachange -= new diopcgroupevent_datachangeeventhandler(opcgroup_datachange);
opcgroup.asyncwritecomplete -= new diopcgroupevent_asyncwritecompleteeventhandler(opcgroup_asyncwritecomplete);
}
if (opcserver != null)
{
opcserver.disconnect();
opcserver = null;
}
connected = false;
}
#endregion
#region 事件
void opcgroup_asyncwritecomplete(int transactionid, int numitems, ref array clienthandles, ref array errors)
{
//
}
void opcgroup_datachange(int transactionid, int numitems, ref array clienthandles, ref array itemvalues, ref array qualities, ref array timestamps)
{
for (int i = 1; i <= numitems; i++)
{
itemvalues[itemnames[convert.toint32(clienthandles.getvalue(i)) - 1]] = itemvalues.getvalue(i).tostring();
}
}
#endregion
#endregion
}
}
测试图片:
测试代码:
using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.linq;
using system.text;
using system.windows.forms;
using system.net;
namespace opchelper
{
public partial class form1 : form
{
public form1()
{
initializecomponent();
}
opchelper opchelper;
private void form1_load(object sender, eventargs e)
{
iphostentry iphost = dns.resolve(environment.machinename);
string strip;
strip = iphost.addresslist[0].tostring();
opchelper = new opchelper(strip, opc.simaticnet,10);
opchelper.additems(s7:[s7 connection_1], new string[] {start, stop, start2, pump1, pump2});
opchelper.additems(s7:[s7 connection_1], new string[] { num1, num2, num3, num4 });
opchelper.additems(s7:[s7 connection_1], new string[] { db1,int266, db1,int268, db1,int270, db1,int272 });
}
private void timer1_tick(object sender, eventargs e)
{
if (opchelper.connected)
{
string str1, str2;
str1 = opchelper.getitemvalues(new string[] { pump1 })[0];
str2 = opchelper.getitemvalues(new string[] { pump2 })[0];
btn_pump1.backcolor = str1!=null&&str1.tolower() == true ? color.green: color.gray;
btn_pump2.backcolor = str2 != null && str2.tolower() == true ? color.green : color.gray;
string[] str3 = opchelper.getitemvalues(new string[] { num1, num2, num3, num4, db1,int266, db1,int268, db1,int270, db1,int272 });
txt_rnum1.text=str3[0];
txt_rnum2.text=str3[1];
txt_rnum3.text=str3[2];
txt_rnum4.text=str3[3];
txt_rnum5.text = str3[4];
txt_rnum6.text = str3[5];
txt_rnum7.text = str3[6];
txt_rnum8.text = str3[7];
}
}
private void btn_start_mousedown(object sender, mouseeventargs e)
{
opchelper.syncwrite(new string[] { start }, new string[] { 1 });
}
private void btn_start_mouseup(object sender, mouseeventargs e)
{
opchelper.syncwrite(new string[] { start }, new string[] { 0 });
}
private void btn_stop_mousedown(object sender, mouseeventargs e)
{
opchelper.syncwrite(new string[] { stop }, new string[] { 1 });
}
private void btn_stop_mouseup(object sender, mouseeventargs e)
{
opchelper.syncwrite(new string[] { stop }, new string[] { 0 });
}
private void btn_start2_click(object sender, eventargs e)
{
if (opchelper.contains(start2))
{
string str;
str = opchelper.getitemvalues(new string[] { start2 })[0];
if (convert.toboolean(str))
opchelper.syncwrite(new string[] { start2 }, new string[] { 0 });
else
opchelper.syncwrite(new string[] { start2 }, new string[] { 1 });
}
}
private void btn_write_click(object sender, eventargs e)
{
opchelper.syncwrite(new string[] { num1, num2, num3, num4 }, new string[] { txt_wnum1.text, txt_wnum2.text, txt_wnum3.text, txt_wnum4.text });
}
private void form1_formclosing(object sender, formclosingeventargs e)
{
opchelper.dispose();
}
}
}