c#调用OPC自动化接口同步访问西门子PLC

 1 class OpcManager
 2     {
 3         OPCServer Server = new OPCServer();
 4         OPCGroup Group;
 5         public OPCItem Item1;
 6 
 7         public void Start()
 8         {
 9             try
10             {
11                 Server.Connect("OPC.SimaticNet");
12                 Group = Server.OPCGroups.Add("Group1");
13                 Group.IsActive = true;
14                 Item1 = Group.OPCItems.AddItem("S7:[S7_Connection_1]QX0.1", 1);
15             }
16             catch (System.Exception ex)
17             {
18                 MessageBox.Show(ex.Message, "Error - OpcManager", MessageBoxButtons.OK, MessageBoxIcon.Error);
19             }
20         }
21 
22         public void Write(OPCItem Item, object Value)
23         {
24             try
25             {
26                 Item.Write(Value);
27             }
28             catch (System.Exception ex)
29             {
30                 MessageBox.Show(ex.Message, "Error - OpcManager", MessageBoxButtons.OK, MessageBoxIcon.Error);
31             }
32         }
33 
34         public void Read(OPCItem Item, out object Value, out object Quality, out object TimeStamp)
35         {
36             Value = null;
37             Quality = null;
38             TimeStamp = null;
39             try
40             {
41                 Item.Read(1, out Value, out Quality, out TimeStamp);
42             }
43             catch (System.Exception ex)
44             {                
45                 MessageBox.Show(ex.Message, "Error - OpcManager", MessageBoxButtons.OK, MessageBoxIcon.Error);
46             }
47         }
48 
49         public void Disconnect()
50         {
51             try
52             {
53                 if (Item1 != null) Item1 = null;
54                 if (Group != null) Group = null;
55                 Server.Disconnect();
56             }
57             catch (System.Exception error)
58             {
59                 MessageBox.Show(error.Message, "Error - OpcManager", MessageBoxButtons.OK, MessageBoxIcon.Error);
60             }
61         }
62     }

 

转载于:https://www.cnblogs.com/ljsoftware/p/8371556.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值