Opc and

Recently, our Ndolls studio undertook an automation control project in Shandong University, mainly made a phối of industrial control information system, one of which is a function module that sends part of the data parameters of the system management khổng lồ the OPC hệ thống, which is executed after the OPC server receives data Corresponding industrial control operation. The first contact with OPC projects is a bit big, and I nói qua some experience with you, I hope to help everyone.

You watching: Opc and

1. Development and debugging environment

1. System environment: win7 64-bit

2. Development tools: Microsoft Visual Studio 2010 (.Net4.0)

3. OPC component: Interop.OPCAutomation.dll

Download address: http://www.ndolls.net/doc/OPCAutomation.zip

4. OPC Server simulation software: ICONICS Simulator OPC Server 3.12

Download address (tải về after login): http://www.ndolls.net/doc/ICONICSSimulatorOPCServer3.12.zip

2. Matters needing attention

1. Interop.OPCAutomation.dll needs to lớn be registered in the system, you can directly run the following command:

regsvr32 Interop.OPCAutomation.dll storage path

If the registration cannot be successful due to lớn win7 system permissions, please use the following command:

runas /user:administratorregsvr32Interop.OPCAutomation.dll storage path

2. Please mix the project to X86 mode, otherwise Interop.OPCAutomation.dll will fail lớn Gọi.

See more: All About Farm Frenzy Free: Time Management Games Offline, Download Farm Frenzy For Free At Freeride Games!

3. To use the simulation software ICONICS Simulator OPC Server, you need khổng lồ enable the Distributed Transaction Coordinator service.

See more: Securecrt And Securefx 8

4. In the process of calling Interop.OPCAutomation.dll, if an error code is encountered, please refer to lớn the following address for analysis và resolution:


5. The Chinese document of Interop.OPCAutomation.dll can be downloaded from the following address:


3. Demo interface & code analysis (tải về address: http://www.ndolls.net/doc/OPCManage.zip)

Demo mainly realizes the directed sending of data to the packets & tags specified by the OPC VPS.

The following picture shows the winform interface of the demo


The following figure is the simulation software interface


Paste the code below, please note that you first need lớn add a reference using OPCAutomation

#region variable // OPCServer"s IPhường private String strHostIP; // Host name of OPCServer private String strHostName; // Whether to establish a connection with OPCServer private Boolean opc_connected; // The OPCServer object created private OPCServer myServer; // OPCServer node browser private OPCBrowser myOPCBrowser; // Grouped collection private OPCGroups myGroups; // Grouping examples private OPCGroup myGroup; // Grouped TAG nodes private OPCItems myItems; // Server handle int itmHandleServer = 0; // The leaf node lớn be written private OPCItem<> myItemArray; #endregion #region Trigger event /// /// Event to be executed whenever the cửa nhà data changes /// /// Process ID /// number of items /// tác phẩm client handle /// TAG value /// Quality /// timestamp void myGroup_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps) { for (int i = 1; i /// Event executed when writing TAG value /// /// Process ID /// number of items /// tòa tháp client handle /// error information returned by the VPS void myGroup_AsyncWriteComplete(int TransactionID, int NumItems, ref Array ClientHandles, ref Array Errors) { // To facilitate testing, display to lớn the status bar output for (int i = 1; i /// connect lớn the VPS /// /// /// private void btnConIP_Click(object sender, EventArgs e) // Acquire the IPhường address of the OPC server according lớn the input đầu vào strHostIP = txtIP..Text; // Get the OPC VPS host name by IP IPHostEntry ipHostEntry = Dns.GetHostEntry(strHostIP); strHostName = ipHostEntry.HostName.ToString(); try // Instantiate OPC service myServer = new OPCServer(); // Get the OPCServer menu object serverList = myServer.GetOPCServers(strHostName); // Show OPCServer lớn ComboBox foreach (string turn in (Array)serverList) cmbServer.Items.Add(turn); cmbServer.SelectedIndex = 0; // Turn on the OPC connection button btnOPC.Enabled = true; catch (Exception err) MessageBox.Show ("Error enumerating OPC service:" + err.Message, "Prompt message", MessageBoxButtons.OK, MessageBoxIbé.Warning); /// /// Connect to lớn OPC VPS /// /// /// private void btnOPC_Click(object sender, EventArgs e) try // Connect according khổng lồ the selected OPCServer myServer.Connect(cmbServer.Text, strHostIP); // đầu ra status information according to lớn whether the connection is successful if (myServer.ServerState == (int)OPCServerState.OPCRunning) lblState.Text = "Connected to:" + myServer.ServerName; // Display server information lblState.Text + = "---- Start time:" + myServer.StartTime.ToString (); lblState.Text + = "---- Version:" + myServer.MajorVersion.ToString () + "." + myServer.MinorVersion.ToString () + "." + myServer.BuildNumber.ToString (); else lblState.Text = "State:" + myServer.ServerState.ToString (); // Connected tag opc_connected = true; // Turn on the Get Tag button btnGetGrps.Enabled = true; MessageBox.Show ("Successfully linked OPC"); catch (Exception err) MessageBox.Show ("Error initializing:" + err.Message, "Prompt message", MessageBoxButtons.OK, MessageBoxIcon.Warning); /// /// Get all groups and labels of OPC server /// /// /// private void btnGetGrps_Click(object sender, EventArgs e) // Instantiate Tag browser myOPCBrowser = myServer.CreateBrowser(); // Expvà grouping myOPCBrowser.ShowBranches(); // Expvà tags myOPCBrowser.ShowLeafs(true); // Display all branches và leaf nodes to ListBox lstItems.Items.Clear(); foreach (object turn in myOPCBrowser) lstItems.Items.Add(turn.ToString()); // mở cửa the positioning label button btnSetItem.Enabled = true; /// /// According to lớn the current label selected in the các mục, locate the label that needs khổng lồ sover data /// /// /// private void btnSetItem_Click(object sender, EventArgs e) try /** * It should be noted that the label format of different OPC servers is also different * During the kiểm tra, ICONICS Simulator OPC Server was used, and the label format was Textual.Memory * In the production environment, SimaticNet_V13Sp1 is used, và the label format is as follows: S7: MReal120 * **/ // According to the label selected by the ListBox, the group name is processed string groupName = lstItems.Text; // Instantiate group myGroups = myServer.OPCGroups; myGroup = myGroups.Add(groupName); // Set the mặc định group attribute myServer.OPCGroups.DefaultGroupIsActive sầu = true; myServer.OPCGroups.DefaultGroupDeadband = 0; myGroup.UpdateRate = 250; myGroup.IsActive = true; myGroup.IsSubscribed = true; // Locate the target công trình that needs khổng lồ sover data myItems = myGroup.OPCItems; // Instantiate the label in the group myItemArray = new OPCItem<1>; // populate the project group myItemArray<0> = myItems.AddItem(lstItems.Text, 1); // Get the VPS handle itmHandleServer = myItemArray<0>.ServerHandle; // Monitor data changes in the group myGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(myGroup_DataChange); myGroup.AsyncWriteComplete += new DIOPCGroupEvent_AsyncWriteCompleteEventHandler(myGroup_AsyncWriteComplete); // Turn on the skết thúc parameter button btnWrite.Enabled = true; catch (Exception err) MessageBox.Show ("An error occurred while creating a group:" + err.Message, "prompt message", MessageBoxButtons.OK, MessageBoxIbé.Warning); /// /// write value /// /// /// private void btnWrite_Click(object sender, EventArgs e) // Get the previously positioned label OPCItem bItem = myItems.GetOPCItem(itmHandleServer); // Generate data objects based on user interface đầu vào int<> temp = new int<2> 0, bItem.ServerHandle ; Array serverHandles = (Array)temp; object<> valueTemp = new object<2> "", txtMyValue.Text ; Array values = (Array)valueTemp; Array Errors; int cancelID; // Asynchronous write to OPC VPS myGroup.AsyncWrite(1, ref serverHandles, ref values, out Errors, 2009, out cancelID); // Recycle resources GC.Collect(); /// /// Cthua kém the link when closing the khung /// /// /// private void OPCManage_FormClosing(object sender, FormClosingEventArgs e) if (!opc_connected) return; if (myGroup != null) myGroup.DataChange -= new DIOPCGroupEvent_DataChangeEventHandler(myGroup_DataChange); if (myServer != null) myServer.Disconnect(); opc_connected = false; #endregion