C#OPC数据采集(FORM)
using System;
using System.Windows.Forms;
using Opc;
using Opc.Da;
using OpcCom;
using opc_Library;
del;
using System.Collections.Generic;
namespace opc_collection
{
public partial class Form1 : Form
{
private Opc.Da.Server m_rver =null;//定义数据存取服务器
private Opc.Da.Subscription subscription = null;//定义组对象(订阅者)
private Opc.Da.SubscriptionState state = null;//定义组(订阅者)状态,相当于OPC规范中组的参数
private Opc.IDiscovery m_discovery = new ServerEnumerator();//定义枚举基于COM服务器的接⼝,⽤来搜索所有的此类服务器。 //时钟
private Timer _timer = new Timer();
//写⽇志记录
write_log write_log = new write_log();
public Form1()
{
InitializeComponent();
// _timer.Interval = 2000;
_timer.Enabled = fal;
_timer.Tick = MainProcess;
textBox1.Text = "192.168.1.100";
// button2.Enabled = fal;
// button3.Enabled = fal;
}
private void button1_Click(object nder, EventArgs e)
{
comboBox1.Items.Clear();
/
/查询服务器
try
{
string str= Specification_DA_20.ToString();
Opc.Server[] rvers = m_discovery.GetAvailableServers(Specification_DA_20, textBox1.Text.ToString().Trim(), null); if(rvers != null)
{
foreach(Opc.Da.Server rver in rvers)
{
comboBox1.Items.Add(rver.Name);
}
}
// comboBox1.SelectedIndex = 0;
ls.Items.Add("查询服务器成功.请选择OPC进⾏连接");
write_log.write_log_txt("查询服务器成功.请选择OPC进⾏连接");
// button1.Enabled = fal;
button2.Enabled = true;
}
catch(Exception ex)
快速上网
{
ls.Items.Add(ex.Message);
write_log.write_log_txt(ex.ToString());
}
}
private void button2_Click(object nder, EventArgs e)付东流
水果电池{
//连接
if(button2.Text=="释放")
{
//结束:释放各资源
button2.Text = "连接";
ls.Items.Add("释放成功.不能进⾏读取数据,请重新连接");
write_log.write_log_txt("释放成功.不能进⾏读取数据,请重新连接");
btn_conllection.Enabled = fal;
try
{
subscription.Dispo();//强制资源回收站回收该subscription的所有资源。
m_rver.Disconnect();//断开服务器连接
}
catch(Exception ex)
{
ls.Items.Add(ex.Message);
write_log.write_log_txt(ex.ToString());
}
}
el
{
草原八月末
try
{
Opc.Server[] rvers = m_discovery.GetAvailableServers(Specification_DA_20, textBox1.Text, null); if(rvers != null)
{
foreach(Opc.Da.Server rver in rvers)
{
if(String.Compare(rver.Name, comboBox1.Text, true) == 0)//为true忽略⼤⼩写
{班主任成长故事
m_rver = rver;//建⽴连接。
break;
}
}
}
}
catch(Exception ex)
{
ls.Items.Add(ex.Message);
write_log.write_log_txt(ex.ToString());
return;
}
if(m_rver != null)
{
try
{
m_rver.Connect();
ls.Items.Add("OPC服务器连接成功,请填写变量名称进⾏读取数据");
write_log.write_log_txt("OPC服务器连接成功.");
button2.Text = "释放";
btn_conllection.Enabled = true;
}
catch(Exception ex)
{
ls.Items.Add(ex.Message);
}
}
el
{
ls.Items.Add("连接失败,请检查IP以及服务器对象");
write_log.write_log_txt("连接失败,请检查IP以及服务器对象");
}
}
}
快速止咳
private void button3_Click(object nder, EventArgs e)
{
_timer.Interval =Int32.Par(txt_time.Text.ToString().Trim());
_timer.Enabled = true;
// l_m.Clear();
// xm.xml_to_model("l");
// l_m = xm.get_monitor_Xml_m_all();
// Item[] items = new Item[100];//定义数据项,即item
int i = 0;
/* string daname = string.Empty;
foreach (var e_1 in l_m) //遍历集合
{
// items[i].ClientHandle = Guid.NewGuid().ToString();//客户端给该数据项分配的句柄。
// items[i].ItemPath = null; //该数据项在服务器中的路径。
// items[i].ItemName = e_1.Opc_da;//itemName[0]; //该数据项在服务器中的名字。
// i ;
daname = e_1.Opc_da;
}
try
{
state = new Opc.Da.SubscriptionState();//组(订阅者)状态,相当于OPC规范中组的参数
state.Name = "newGroup";//组名
state.ServerHandle = null;//服务器给该组分配的句柄。
state.ClientHandle = Guid.NewGuid().ToString();//客户端给该组分配的句柄。
state.Active = true;//激活该组。
state.UpdateRate = 100;//刷新频率为1秒。
state.Deadband = 0;// 死区值,设为0时,服务器端该组内任何数据变化都通知组。
state.Locale = null;//不设置地区值。
subscription = (Opc.Da.Subscription)m_rver.CreateSubscription(state);//创建组
梦见河里游泳ls.Items.Add("1");
string[] itemName = new string[] {""};
ls.Items.Add("2");
itemName[0] = daname;
ls.Items.Add("2.1");
Item[] items = new Item[1];//定义数据项,即item
items[0] = new Item();
items[0].ClientHandle = Guid.NewGuid().ToString();//客户端给该数据项分配的句柄。
items[0].ItemPath = null; //该数据项在服务器中的路径。
items[0].ItemName = itemName[0]; //该数据项在服务器中的名字。
subscription.AddItems(items);
ls.Items.Add("3");
ItemValueResult[] values = subscription.Read(subscription.Items);
ls.Items.Add("4");
ls.Items.Add(items[0].Key.ToString());
/
/ ls.Items.Add(values[0].Value.ToString() );
// ls.Items.Add(values[1].Value.ToString());
try
{
foreach (ItemValueResult value in values)
{
ls.Items.Add("5");
ls.Items.Add("成功读取变量为<" daname ">的数据.值为<" value.Value.ToString() ">");
}
/* if (values[0].Quality.Equals(Opc.Da.Quality.Good))
{
/
/ textBox3.Text = values[0].Value.ToString();
ls.Items.Add("成功读取变量为<" daname ">的数据.值为<" values[0].Value.ToString() ">"); }
* */
/* }
catch (Exception e2)
{
ls.Items.Add("e2:" e2.ToString());
}
subscription.RemoveItems(subscription.Items);
m_rver.CancelSubscription(subscription);//m_rver前⽂已说明,通知服务器要求删除组。
}
catch (Exception ex)
{
ls.Items.Add(ex.Message);
}
*/
}
//Form界⾯控制部分
#region Form_Monitor
private void alert(string msg)
{
MessageBox.Show(msg, "系统提⽰", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void Form1_FormClosing(object nder, FormClosingEventArgs e)
{
if(e.CloReason == CloReason.UrClosing)//当⽤户点击窗体右上⾓X按钮或(Alt F4)时发⽣
{
e.Cancel = true;
this.ShowInTaskbar = fal;
this.Hide();
}
}
private void notifyIcon1_MouClick(object nder, MouEventArgs e)
{
if(e.Button == MouButtons.Right)
{
mymenu.Show(System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y); mymenu.Show();
}
if(e.Button == MouButtons.Left)
{
this.Visible = true;
mymenu.Hide();
this.Icon = Resource1._20151104082741597_easyicon_net_256;
this.WindowState = FormWindowState.Normal;
}
}
private void mymenu_Click(object nder, EventArgs e)
{
}
private void mymenu_Opening(object nder, System.ComponentModel.CancelEventArgs e)
{
}
private void mymenu_child_Click(object nder, EventArgs e)
{
Application.Exit();
}
private void Form1_MinimumSizeChanged(object nder, EventArgs e)
{
if(this.WindowState == FormWindowState.Normal && this.Visible == true)
{
this.WindowState = FormWindowState.Minimized;
this.Visible = fal;
this.ShowInTaskbar = fal;//使Form不在任务栏上显⽰
}
}
#endregion
private void btn_stop_Click(object nder, EventArgs e)
{
_timer.Enabled = fal;
ls.Items.Clear();
}
//重新加载OPC点位
List<monitor> l_m = new List<monitor>();
xml_to_model xm = new xml_to_model("l");
string daname = string.Empty;
public void MainProcess(object nder, EventArgs e)
{
l_m.Clear();
// xm.xml_to_model("l");
l__monitor_Xml_m_all();
// Item[] items = new Item[100];//定义数据项,即item
// int i = 0;
// foreach (var e_1 in l_m) //遍历集合
// {
foreach(var e_1 in l_m) //遍历集合
{
maskman
daname = e_1.Opc_da;
try
{
state = new Opc.Da.SubscriptionState();//组(订阅者)状态,相当于OPC规范中组的参数 state.Name = "newGroup";//组名
state.ServerHandle = null;//服务器给该组分配的句柄。
state.ClientHandle = Guid.NewGuid().ToString();//客户端给该组分配的句柄。
state.Active = true;//激活该组。
state.UpdateRate = 100;//刷新频率为1秒。
state.Deadband = 0;// 死区值,设为0时,服务器端该组内任何数据变化都通知组。
state.Locale = null;//不设置地区值。
subscription = (Opc.Da.Subscription)m_rver.CreateSubscription(state);//创建组
ls.Items.Add("1");
string[] itemName = new string[] { ""};
ls.Items.Add("2");
itemName[0] = daname;
ls.Items.Add("2.1");
Item[] items = new Item[1];//定义数据项,即item
items[0] = new Item();
items[0].ClientHandle = Guid.NewGuid().ToString();//客户端给该数据项分配的句柄。
items[0].ItemPath = null; //该数据项在服务器中的路径。
items[0].ItemName = itemName[0]; //该数据项在服务器中的名字。
subscription.AddItems(items);
ls.Items.Add("3");
ItemValueResult[] values = subscription.Read(subscription.Items);
ls.Items.Add("4");
ls.Items.Add(items[0].Key.ToString());
// ls.Items.Add(values[0].Value.ToString() );
// ls.Items.Add(values[1].Value.ToString());
try
{
// foreach (ItemValueResult value in values)
// {
ls.Items.Add("5");
#region
//插⼊DB OPCMAP
Db_Access dba = new Db_Access();
//判断MAP表⾥是否有该DA
ReadXml readxml = new ReadXml();
string[] db_name_inrt = Xml_node("2", "db_name", "l");
string db_name = db_name_inrt[0];
string map_ixists = @"lect count(opc_id) from";
map_ixists = map_ixists " ""[".Trim() db_name.Trim() "]".Trim() ".".Trim() "[dbo]".Trim() ".".Trim() "[tb_opc_map]";
write_log.write_log_txt(map_ixists);
_sql_int("2", map_ixists) == 0)
{
string map_inrt = @"INSERT INTO ";
map_inrt = map_inrt " ""[".Trim() db_name.Trim() "]".Trim() ".".Trim() "[dbo]".Trim() ".".Trim() "[tb_opc_map]([opc_id]"
" ,[opc_da],[opc_name],[opc_type],[c_flag])"" ""lect ""'""1""',""'"e_1.Opc_da.Trim() "',""'"e_1.Opc_name.Trim() "'"",""'" e_1.Opc_type.Trim() "',""'"e_1.C_flag.Trim() "'";
write_log.write_log_txt(map_inrt);
_sql_opreate("2", map_inrt) == "1")
{
//获取插⼊的OPC ID
string opc_id_sql = @"lect top 1 opc_id from";
opc_id_sql = opc_id_sql " ""[".Trim() db_name.Trim() "]".Trim() ".".Trim() "[dbo]".Trim() ".".Trim() "
[tb_opc_map] where opc_name='" e_1.Opc_name.Trim() "'";
write_log.write_log_txt(opc_id_sql );
string opc_id = _sql_opreate("2", opc_id_sql);
string opc_inrt = @"INSERT INTO ";
opc_inrt = opc_inrt " ""[".Trim() db_name.Trim() "]".Trim() ".".Trim() "[dbo]".Trim() ".".Trim() "[tb_opc]([opc_id]"
",opc_value,collection_time)""lect ""'"opc_id.Trim() "',""'""0.01""',""'"DateTime.Now.ToString() "'";
// opc_inrt = opc_inrt " " "[".Trim() db_name.Trim() "]".Trim() ".".Trim() "[dbo]".Trim() ".".Trim() "[tb_opc]([opc_id]"
// ",opc_value,collection_time)" "lect " "'" opc_id.Trim() "'," "'" values[0].Value.ToString().Trim() "'," "'"
DateTime.Now.ToString() "'";
write_log.write_log_txt(opc_inrt );
_sql_opreate("2", opc_inrt) == "0")
{
write_log.write_log_txt("插⼊OPC数据表失败,请检查程序。");
}
}
el
{
write_log.write_log_txt("插⼊OPC配置表失败,请检查程序。");
}
}
_sql_int("2", map_ixists) > 0)
{
//获取插⼊的OPC ID
string opc_id_sql = @"lect count(opc_id) from";
opc_id_sql = opc_id_sql " ""[".Trim() db_name.Trim() "]".Trim() ".".Trim() "[dbo]".Trim() ".".Trim() "[tb_opc_map] where opc_name='" e_1.Opc_name.Trim() "'";
write_log.write_log_txt(">0:"opc_id_sql);
// string opc_id = _sql_opreate("2", opc_id_sql);
int opc_id_flag = _sql_int("2", opc_id_sql);
string opc_id = string.Empty;
if(opc_id_flag > 0)
{
string opc_id_sql_1 = @"lect top 1 opc_id from";