/// <summary> /// Here we create our connection with the BAOS device /// First we create the StreamConnection and connect to the remote device /// Connect takes the name or the IP address of the BAOS device /// (which performs an enumeration to find the associated IP address and protocol). /// Then we create the Connector from the Stream Connection and start the keep-alive thread. /// Finally we attach to the datapoint events. This calls our function /// onDatapointEvent when a datapoint indication or response is received /// </summary> private void buttonConnect_Click(object sender, EventArgs e) { if (editConnection.Text == "") { MessageBox.Show("Please insert a valid IP-Address or Name"); return; } try { // create the connection baosConnection = new BaosConnection(); if (radioByIP.Checked) { // connect to the device by IPv4 address baosConnection.connectIp(editConnection.Text, true); } else { // connect to the device by device friendly name baosConnection.connectIpByName(editConnection.Text, true); } connector = new Connector(baosConnection); // connect the events baosEvent = new BaosEvent(connector); baosEvent.DatapointEvent += new BaosEvent.DatapointEventHandler(onDatapointEventInThreadContext); baosEvent.enable(); // read the status of all datapoints readDatapoints(); // enable and disable controls buttonConnect.Enabled = false; buttonDisconnect.Enabled = true; groupBoxVisualisation.Enabled = true; radioByName.Enabled = false; radioByIP.Enabled = false; editConnection.Enabled = false; } catch (kdrive.KdriveException exception) { MessageBox.Show(exception.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }
/// <summary> /// Event handler for click buttonRead /// First we create a connection to the device /// with the Name or IP in the textbox. Is that device not available, we see /// an error message in the messagebox. Otherwise we create the connection and read /// out the datapoints properties /// </summary> private void buttonRead_Click(object sender, EventArgs e) { // clear old data from the data grid dataGridView.Rows.Clear(); if (editConnection.Text == "") { MessageBox.Show("Please insert a valid IP-Address or Name"); return; } try { BaosConnection baosConnection = new BaosConnection(); using (baosConnection) { if (radioByIP.Checked) { // create a TPC/IP connection with the remote BAOS device by IPv4 address baosConnection.connectIp(editConnection.Text, true); } else { //Connecting with the device which has this Name baosConnection.connectIpByName(editConnection.Text, true); } Connector connector = new Connector(baosConnection); //Reading the datapoints readDatapointDescriptions(connector); baosConnection.disconnect(); } } catch (kdrive.KdriveException exception) { MessageBox.Show(exception.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }