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 }