private void ASyncReadButton_Click(System.Object sender, System.EventArgs e) { try { _mRequest = null; _mHandle = 0; var subscription = OpcController.Subscription; if (subscription != null) { // Begin the asynchronous read request. if ((subscription.Items != null)) { StatusBar.Text = "OPC Group Async Read in progress ..."; ReadCompleteEventHandler rceh = OnReadComplete; subscription.Read(subscription.Items, +_mHandle, rceh, out _mRequest); } } // Update controls if request successful. if ((_mRequest != null)) { ASyncReadButton.Enabled = false; } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> /// The event handler to recieve the returned results from OPC Server's read acton. /// </summary> /// <param name="clientHandle"></param> /// <param name="results"></param> private void OnReadComplete(object clientHandle, Opc.Da.ItemValueResult[] results) { try { if ((object)_mHandle != clientHandle) { return; } // Save results. _mRequest = null; // Check if there is nothing to do. if (results == null || results.Length == 0) { return; } WriteControlValues(results); ASyncReadButton.Enabled = true; StatusBar.Text = "OPC Group Async Read operation is complete."; } catch (Exception e) { MessageBox.Show(e.Message); } }
private void ASyncWriteButton_Click(System.Object sender, System.EventArgs e) { try { var subscription = OpcController.Subscription; var itemsvalue = PrepareControlValueItemList(); // Convert to array of item objects. _mRequest = null; _mHandle = 0; if (subscription != null) { // Begin the asynchronous read request. if (itemsvalue != null) { StatusBar.Text = "OPC Group Async Write operation in progress ..."; subscription.Write(itemsvalue, +_mHandle, OnWriteComplete, out _mRequest); } } // Update controls if request successful. if ((_mRequest != null)) { ASyncWriteButton.Enabled = false; } } catch (Exception em) { MessageBox.Show(em.Message); } }
/// <summary> /// Called when an asynchronous read request completes. /// </summary> private void OnReadComplete(object clientHandle, OpcDa::ItemValueResult[] results) { try { if (InvokeRequired) { BeginInvoke(new OpcDa::ReadCompleteEventHandler(OnReadComplete), new object[] { clientHandle, results }); return; } if (!m_handle.Equals(clientHandle)) { return; } ResultsCTRL.Initialize(m_subscription.Server, null, results); m_request = null; m_results = results; OkBTN.Enabled = true; CancelBTN.Enabled = true; GoBTN.Visible = true; StopBTN.Visible = false; } catch (Exception e) { MessageBox.Show(e.Message); } }
/// <summary> /// Begins the asynchronous request. /// </summary> private void BeginRequest() { try { m_request = null; // begin the asynchronous read request. if (m_items != null) { m_subscription.Read(m_items, ++m_handle, new OpcDa::ReadCompleteEventHandler(OnReadComplete), out m_request); } // begin the asynchronous write request. else if (m_values != null) { m_subscription.Write(m_values, ++m_handle, new OpcDa::WriteCompleteEventHandler(OnWriteComplete), out m_request); } // update controls if request successful. if (m_request != null) { OkBTN.Enabled = false; CancelBTN.Enabled = false; GoBTN.Visible = false; StopBTN.Visible = true; } } catch (Exception e) { MessageBox.Show(e.Message); } }
/// <summary> /// The event handler to recieve the returned results from the Write action. /// </summary> /// <param name="clientHandle"></param> /// <param name="results"></param> private void OnWriteComplete(object clientHandle, Opc.IdentifiedResult[] results) { if (!_mHandle.Equals(clientHandle)) { return; } _mRequest = null; ASyncWriteButton.Enabled = true; StatusBar.Text = "OPC Group Async Write operation is complete."; }
/// <summary> /// Cancels the asynchronous request. /// </summary> private void CancelRequest() { try { if (m_request != null) { m_subscription.Cancel(m_request, new OpcDa::CancelCompleteEventHandler(OnCancelComplete)); } } catch (Exception e) { m_request = null; OkBTN.Enabled = true; CancelBTN.Enabled = true; GoBTN.Visible = true; StopBTN.Visible = false; MessageBox.Show(e.Message); } }
/// <summary> /// Called when an asynchronous write request completes. /// </summary> private void OnWriteComplete(object clientHandle, Opc.IdentifiedResult[] results) { if (InvokeRequired) { BeginInvoke(new OpcDa::WriteCompleteEventHandler(OnWriteComplete), new object[] { clientHandle, results }); return; } if (!m_handle.Equals(clientHandle)) { return; } ResultsCTRL.Initialize(m_subscription.Server, null, results); m_request = null; m_results = results; OkBTN.Enabled = true; CancelBTN.Enabled = true; GoBTN.Visible = true; StopBTN.Visible = false; }