| 作者 | 主题 |
|---|---|
|
HUO1921 侠圣 经验值:2390 发帖数:152 精华帖:6 |
楼主
主题:【分享】C#中实现OPC数据访问(学习笔记二)C#编程
测试环境:SIMATIC_NET_8.2 Visual studio 2010 系统:Windows Win7旗舰版 测试结果:同步读写数据成功 异步读写数据成功 订阅式读取并分解数据成功 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 OPCAutomation; namespace _4_Automation_RW { public partial class Form1 : Form { public Form1() { InitializeComponent(); } OPCServer MyOpcServer; //定义OPCSe OPCGroup MyOpcGroup; //定义组 const int ItemCount = 6; OPCItem[] MyOpcItem = new OPCItem[ItemCount+1]; //Item int[] ServerHandle = new int[ItemCount];//服务器端注册句柄 //连接OPCServer,建立Group,添加Item private void btn_Conn_Click(object sender, EventArgs e) { try { MyOpcServer = new OPCServer(); MyOpcServer.Connect("OPC.SimaticNet", txt_IP.Text.Trim());//OPCServer MyOpcGroup = MyOpcServer.OPCGroups.Add("MyGroup1"); MyOpcGroup.IsActive = true; //OPCGroup的IsActive属性:用以控制 OPC 组的活动状态。只有活动状态的 OPC 组才进行定期的数据更新。 MyOpcGroup.IsSubscribed = false;//是否异步, 在采用异步读写, 订阅等方式下都需要 MyOpcGroup.DeadBand = 0; //新添加的 OPC 组的不敏感带的默认值(%) 。初期值是 0%。 MyOpcGroup.UpdateRate = 1;//OPCGroup的UpdateRate属性:数据更新周期(毫秒) 。 MyOpcItem[0] = MyOpcGroup.OPCItems.AddItem("S7:[S7 connection_1]DB10,INT0", 1); MyOpcItem[1] = MyOpcGroup.OPCItems.AddItem("S7:[S7 connection_1]DB10,INT2", 2); MyOpcItem[2] = MyOpcGroup.OPCItems.AddItem("S7:[S7 connection_1]DB10,W4", 3); MyOpcItem[3] = MyOpcGroup.OPCItems.AddItem("S7:[S7 connection_1]DB10,B6", 4); MyOpcItem[4] = MyOpcGroup.OPCItems.AddItem("S7:[S7 connection_1]DB10,REAL8", 5); MyOpcItem[5] = MyOpcGroup.OPCItems.AddItem("S7:[S7 connection_1]DB10,REAL12", 6); MyOpcItem[6] = MyOpcGroup.OPCItems.AddItem("S7:[S7 connection_1]MX100.0", 7); ServerHandle[0] = MyOpcItem[0].ServerHandle; ServerHandle[1] = MyOpcItem[1].ServerHandle; ServerHandle[2] = MyOpcItem[2].ServerHandle; ServerHandle[3] = MyOpcItem[3].ServerHandle; ServerHandle[4] = MyOpcItem[4].ServerHandle; ServerHandle[5] = MyOpcItem[5].ServerHandle; MyOpcGroup.AsyncWriteComplete += new DIOPCGroupEvent_AsyncWriteCompleteEventHandler(MyOpcGroup_AsyncWriteComplete); MyOpcGroup.AsyncReadComplete += new DIOPCGroupEvent_AsyncReadCompleteEventHandler(MyOpcGroup_AsyncReadComplete); MyOpcGroup.AsyncCancelComplete += new DIOPCGroupEvent_AsyncCancelCompleteEventHandler(MyOpcGroup_AsyncCancelComplete); MyOpcGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(MyOpcGroup_DataChange); } catch (System.Exception error) { MessageBox.Show(error.Message, "Result - connect server", MessageBoxButtons.OK, MessageBoxIcon.Error); } } //同步读数据 private void btn_Read_Sync_Click(object sender, EventArgs e) { if (MyOpcServer == null) { MessageBox.Show("请连接OPCServer"); return; } object ItemValues; object Qualities; object TimeStamps; try { MyOpcItem[0].Read(1, out ItemValues, out Qualities, out TimeStamps); txt_R1_Value.Text = String.Format("{0}", ItemValues); txt_R1_Quality.Text = String.Format("{0}", Qualities); // Quality txt_R1_TimeStamp.Text = String.Format("{0}", TimeStamps); // Timestamp MyOpcItem[1].Read(1, out ItemValues, out Qualities, out TimeStamps); txt_R2_Value.Text = String.Format("{0}", ItemValues); txt_R2_Quality.Text = String.Format("{0}", Qualities); // Quality txt_R2_TimeStamp.Text = String.Format("{0}", TimeStamps); // Timestamp
MyOpcItem[2].Read(1, out ItemValues, out Qualities, out TimeStamps); txt_R3_Value.Text = String.Format("{0}", ItemValues); txt_R3_Quality.Text = String.Format("{0}", Qualities); // Quality txt_R3_TimeStamp.Text = String.Format("{0}", TimeStamps); // Timestamp
MyOpcItem[3].Read(1, out ItemValues, out Qualities, out TimeStamps); txt_R4_Value.Text = String.Format("{0}", ItemValues); int temp = Convert.ToInt32(txt_R4_Value.Text); bool[] temp2 = ByteToBool(temp); btn_LED1.BackColor = temp2[0] ? Color.Green : Color.White; btn_LED2.BackColor = temp2[1] ? Color.Green : Color.White; btn_LED3.BackColor = temp2[2] ? Color.Green : Color.White; btn_LED4.BackColor = temp2[3] ? Color.Green : Color.White; btn_LED5.BackColor = temp2[4] ? Color.Green : Color.White; btn_LED6.BackColor = temp2[5] ? Color.Green : Color.White; btn_LED7.BackColor = temp2[6] ? Color.Green : Color.White; btn_LED8.BackColor = temp2[7] ? Color.Green : Color.White; txt_R4_Quality.Text = String.Format("{0}", Qualities); // Quality txt_R4_TimeStamp.Text = String.Format("{0}", TimeStamps); // Timestamp
MyOpcItem[4].Read(1, out ItemValues, out Qualities, out TimeStamps); txt_R5_Value.Text = String.Format("{0}", ItemValues); txt_R5_Quality.Text = String.Format("{0}", Qualities); // Quality txt_R5_TimeStamp.Text = String.Format("{0}", TimeStamps); // Timestamp MyOpcItem[5].Read(1, out ItemValues, out Qualities, out TimeStamps); txt_R6_Value.Text = String.Format("{0}", ItemValues); txt_R6_Quality.Text = String.Format("{0}", Qualities); // Quality txt_R6_TimeStamp.Text = String.Format("{0}", TimeStamps); // Timestamp } catch (System.Exception error) { MessageBox.Show(error.Message, "Result - 同步读", MessageBoxButtons.OK, MessageBoxIcon.Error); } } //同步写数据 private void btn_Write_Sync_Click(object sender, EventArgs e) { if (MyOpcServer == null) { MessageBox.Show("请连接OPCServer"); return; } try { MyOpcItem[0].Write(txt_W1.Text); MyOpcItem[1].Write(txt_W2.Text); MyOpcItem[2].Write(txt_W3.Text); MyOpcItem[3].Write(txt_W4.Text); MyOpcItem[4].Write(txt_W5.Text); MyOpcItem[5].Write(txt_W6.Text); } catch (System.Exception error) { MessageBox.Show(error.Message, "Result - 同步写", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
hongxi-002@qq.com
|
|
HUO1921 侠圣 经验值:2390 发帖数:152 精华帖:6 |
1楼
主题:回复:【分享】C#中实现OPC数据访问(学习笔记二)C#编程//异步读数据 private void btn_Read_Async_Click(object sender, EventArgs e) { if (MyOpcServer == null) { MessageBox.Show("请连接OPCServer"); return; } if (MyOpcGroup.IsSubscribed == false) { MessageBox.Show("请选择订阅"); return; } int[] handle = new int[ItemCount+1] { 0, ServerHandle[0], ServerHandle[1], ServerHandle[2], ServerHandle[3], ServerHandle[4], ServerHandle[5] };//注意写的方式 Array MyServerHandles = (Array)handle; Array errors; int cancelID; try { MyOpcGroup.AsyncRead(ItemCount, ref MyServerHandles, out errors, 1, out cancelID); } catch (System.Exception error) { MessageBox.Show(error.Message, "Result - 异步读", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
//异步写数据 private void btn_Write_Async_Click(object sender, EventArgs e) { if (MyOpcServer == null) { MessageBox.Show("请连接OPCServer"); return; } if (MyOpcGroup.IsSubscribed == false) { MessageBox.Show("请选择订阅"); return; } int[] handle = new int[ItemCount+1] { 0, ServerHandle[0], ServerHandle[1], ServerHandle[2], ServerHandle[3], ServerHandle[4], ServerHandle[5] }; Array MyServerHandles = (Array)handle; object[] values = new object[ItemCount+1] { "", txt_W1.Text, txt_W2.Text, txt_W3.Text, txt_W4.Text, txt_W5.Text, txt_W6.Text }; Array Myvalues = (Array)values; Array errors; int cancelID; try { MyOpcGroup.AsyncWrite(ItemCount, ref MyServerHandles, ref Myvalues, out errors, 2, out cancelID); } catch (System.Exception error) { MessageBox.Show(error.Message, "Result - 异步写", MessageBoxButtons.OK, MessageBoxIcon.Error); } GC.Collect(); } //选择是否为订阅方式 private void chk_Subscribe_CheckedChanged(object sender, EventArgs e) { if (MyOpcServer == null) { MessageBox.Show("请连接OPCServer"); return; } if (chk_Subscribe.Checked) { MyOpcGroup.IsSubscribed = true; } else { MyOpcGroup.IsSubscribed = false; } } //断开OPCServer连接 private void btn_disConn_Click(object sender, EventArgs e) { try { for (int i = 0; i < ItemCount; i++) { if (MyOpcItem[i] != null) MyOpcItem[i] = null; } MyOpcServer.Disconnect(); } catch (System.Exception error) { MessageBox.Show(error.Message, "Result - 异步写", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void btn_Change_Click(object sender, EventArgs e) { if (MyOpcServer == null) { MessageBox.Show("请连接OPCServer"); return; } object ItemValues; object Qualities; object TimeStamps; try { MyOpcItem[6].Read(1, out ItemValues, out Qualities, out TimeStamps); if (String.Format("{0}", ItemValues).ToLower() == "false") { MyOpcItem[6].Write("true"); btn_Change.Text = "CHANGE"; } else { MyOpcItem[6].Write("false"); btn_Change.Text = "UNCHANGE"; } } catch (System.Exception error) { MessageBox.Show(error.Message, "Result - 同步写", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private bool[] ByteToBool(int temp) { bool[] temp2 = new bool[8]; for (int k = 0; k < 8; k++) { if (temp % 2 == 1) temp2[k] = true; else temp2[k] = false; temp = temp / 2; } return temp2; } private void SetValue(int NumItems, System.Array ClientHandles, System.Array ItemValues, System.Array Qualities, System.Array TimeStamps) { for (int i = 0; i < NumItems; i++) { if (ItemValues.GetValue(i + 1) != null) { switch (Convert.ToInt32(ClientHandles.GetValue(i + 1))) { case 1: txt_R1_Value.Text = ItemValues.GetValue(i + 1).ToString(); txt_R1_Quality.Text = Qualities.GetValue(i + 1).ToString(); txt_R1_TimeStamp.Text = TimeStamps.GetValue(i + 1).ToString(); break; case 2: txt_R2_Value.Text = ItemValues.GetValue(i + 1).ToString(); txt_R2_Quality.Text = Qualities.GetValue(i + 1).ToString(); txt_R2_TimeStamp.Text = TimeStamps.GetValue(i + 1).ToString(); break; case 3: txt_R3_Value.Text = ItemValues.GetValue(i + 1).ToString(); txt_R3_Quality.Text = Qualities.GetValue(i + 1).ToString(); txt_R3_TimeStamp.Text = TimeStamps.GetValue(i + 1).ToString(); break; case 4: txt_R4_Value.Text = ItemValues.GetValue(i + 1).ToString(); int temp = Convert.ToInt32(txt_R4_Value.Text); bool[] temp2 = ByteToBool(temp); btn_LED1.BackColor= temp2[0]?Color.Green:Color.White; btn_LED2.BackColor= temp2[1]?Color.Green:Color.White; btn_LED3.BackColor= temp2[2]?Color.Green:Color.White; btn_LED4.BackColor= temp2[3]?Color.Green:Color.White; btn_LED5.BackColor= temp2[4]?Color.Green:Color.White; btn_LED6.BackColor= temp2[5]?Color.Green:Color.White; btn_LED7.BackColor= temp2[6]?Color.Green:Color.White; btn_LED8.BackColor= temp2[7]?Color.Green:Color.White; txt_R4_Quality.Text = Qualities.GetValue(i + 1).ToString(); txt_R4_TimeStamp.Text = TimeStamps.GetValue(i + 1).ToString(); break; case 5: txt_R5_Value.Text = ItemValues.GetValue(i + 1).ToString(); txt_R5_Quality.Text = Qualities.GetValue(i + 1).ToString(); txt_R5_TimeStamp.Text = TimeStamps.GetValue(i + 1).ToString(); break; case 6: txt_R6_Value.Text = ItemValues.GetValue(i + 1).ToString(); txt_R6_Quality.Text = Qualities.GetValue(i + 1).ToString(); txt_R6_TimeStamp.Text = TimeStamps.GetValue(i + 1).ToString(); break; } } } }
hongxi-002@qq.com
|
|
HUO1921 侠圣 经验值:2390 发帖数:152 精华帖:6 |
2楼
主题:回复:【分享】C#中实现OPC数据访问(学习笔记二)C#编程//异步读完成事件 void MyOpcGroup_AsyncReadComplete(int TransactionID, int NumItems, ref System.Array ClientHandles, ref System.Array ItemValues, ref System.Array Qualities, ref System.Array TimeStamps, ref System.Array Errors) { try { SetValue(NumItems, ClientHandles, ItemValues, Qualities, TimeStamps); } catch (System.Exception error) { MessageBox.Show(error.Message, "Result - 异步读", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
//异步写完成事件 void MyOpcGroup_AsyncWriteComplete(int TransactionID, int NumItems, ref Array ClientHandles, ref Array Errors) { txt_WriteStatus.Text = Errors.GetValue(1).ToString(); } void MyOpcGroup_AsyncCancelComplete(int CancelID) { //增加相应代码 }
//订阅方式数据发生改变 void MyOpcGroup_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps) { try { SetValue(NumItems, ClientHandles, ItemValues, Qualities, TimeStamps); } catch (System.Exception error) { MessageBox.Show(error.Message, "Result - 订阅", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }
hongxi-002@qq.com
|