//Initialize our PHSensor object and hook the event handlers private void Form1_Load(object sender, EventArgs e) { sensitivityTxt.Enabled = false; ph_sensor = new PHSensor(); //Hook the event handlers to the object ph_sensor.Attach += new AttachEventHandler(ph_sensor_Attach); ph_sensor.Detach += new DetachEventHandler(ph_sensor_Detach); ph_sensor.Error += new ErrorEventHandler(ph_sensor_Error); ph_sensor.PHChange += new PHChangeEventHandler(ph_sensor_PHChange); openCmdLine(ph_sensor); }
//When the application is terminating, close the Phidget. private void Form1_FormClosed(object sender, FormClosedEventArgs e) { ph_sensor.Attach -= new AttachEventHandler(ph_sensor_Attach); ph_sensor.Detach -= new DetachEventHandler(ph_sensor_Detach); ph_sensor.Error -= new ErrorEventHandler(ph_sensor_Error); ph_sensor.PHChange -= new PHChangeEventHandler(ph_sensor_PHChange); //run any events in the message queue - otherwise close will hang if there are any outstanding events Application.DoEvents(); ph_sensor.close(); ph_sensor = null; }
//PHSensor Detach event handler....Clear the fields and disable the modify sensitivity textbox so that //new sensity value can't be sent while there is no device attached, otherwise this would generate a PhidgetException void ph_sensor_Detach(object sender, DetachEventArgs e) { PHSensor detached = (PHSensor)sender; attachedTxt.Text = detached.Attached.ToString(); nameTxt.Clear(); serialTxt.Clear(); versionTxt.Clear(); phTxt.Clear(); potentialTxt.Clear(); sensitivityTxt.Enabled = false; temperatureTxt.Clear(); temperatureTxt.Enabled = false; }
static void Main(string[] args) { try { PHSensor ph_sensor = new PHSensor(); //Declare a PHSensor object //hook the basic event handlers ph_sensor.Attach += new AttachEventHandler(ph_sensor_Attach); ph_sensor.Detach += new DetachEventHandler(ph_sensor_Detach); ph_sensor.Error += new ErrorEventHandler(ph_sensor_Error); //hook the phidget specific event handlers ph_sensor.PHChange += new PHChangeEventHandler(ph_sensor_PHChange); //open the phidget object to allow connections with phidget PH sensor //devices ph_sensor.open(); //Make the program wait for a Phidget PHSensor to be attached before //trying to communicate with one If we try and call the methods of the //ph_sensor object without a PHSensor device attached, a PhidgetException //will be thrown Console.WriteLine("Waiting for PHSensor to be attached...."); ph_sensor.waitForAttachment(); //Get the program to wait for user input to continue so that we can see //some events fire and console output to be displayed Console.WriteLine("Hit any key to end the program...."); Console.Read(); //Since user input was read, we'll terminate the program, so close the //ph_sensor object ph_sensor.close(); //null he object to clear it from memory ph_sensor = null; //If no exceptions were thrown at this point, the program is ok to //terminate Console.WriteLine("ok"); } catch (PhidgetException ex) { Console.WriteLine(ex.Description); } }
//PHSensor Attach event handler...We'll populate the fields in the GUI and enable the modify sensitivity textbox void ph_sensor_Attach(object sender, AttachEventArgs e) { PHSensor attached = (PHSensor)sender; attachedTxt.Text = attached.Attached.ToString(); nameTxt.Text = attached.Name; serialTxt.Text = attached.SerialNumber.ToString(); versionTxt.Text = attached.Version.ToString(); try { phTxt.Text = attached.sensor.PH.ToString(); } catch (PhidgetException) { phTxt.Text = "Unknown"; } try { potentialTxt.Text = attached.sensor.Potential.ToString(); } catch (PhidgetException) { potentialTxt.Text = "Unknown"; } try { sensitivityTxt.Text = attached.sensor.Sensitivity.ToString(); } catch (PhidgetException) { sensitivityTxt.Text = "Unknown"; } potentialRange.Text = "(" + ph_sensor.sensor.PotentialMin.ToString() + "mV - " + ph_sensor.sensor.PotentialMax.ToString() + "mV)"; phRange.Text = "(" + ph_sensor.sensor.PHMin.ToString() + " - " + ph_sensor.sensor.PHMax.ToString() + ")"; sensitivityTxt.Enabled = true; temperatureTxt.Enabled = true; }