//We'll create our encoder object and initialize the event handlers and open the Phidget Encoder //A small note: since we are doing this in the form load method, we don't have to worry about //waiting for attach on the phidget as this would prevent the form from loading until you have an //encoder attached. I have programmd so that the form can fully load without consequence and //everything will work once an Encoder is attached. private void Form1_Load(object sender, EventArgs e) { inputArray = new ArrayList(); inputArray.Add(input0Chk); inputArray.Add(input1Chk); inputArray.Add(input2Chk); inputArray.Add(input3Chk); for (int i = 0; i < 4; i++) { ((CheckBox)inputArray[i]).Visible = false; ((CheckBox)inputArray[i]).AutoCheck = false; ((CheckBox)inputArray[i]).Enabled = true; } encoder = new Phidgets.Encoder(); encoder.Attach += new AttachEventHandler(encoder_Attach); encoder.Detach += new DetachEventHandler(encoder_Detach); encoder.Error += new ErrorEventHandler(encoder_Error); encoder.InputChange += new InputChangeEventHandler(encoder_InputChange); encoder.PositionChange += new EncoderPositionChangeEventHandler(encoder_PositionChange); openCmdLine(encoder); }
static void Main(string[] args) { try { //Initialize the Encoder object encoder = new Phidgets.Encoder(); //Hook the basic event handlers encoder.Attach += new AttachEventHandler(encoder_Attach); encoder.Detach += new DetachEventHandler(encoder_Detach); encoder.Error += new ErrorEventHandler(encoder_Error); //Hook the phidget specific event handlers encoder.InputChange += new InputChangeEventHandler(encoder_InputChange); encoder.PositionChange += new EncoderPositionChangeEventHandler (encoder_PositionChange); //open the object for connections encoder.open(); //Wait for an encoder device to be attached before continuing Console.WriteLine("Waiting for Encoder to be attached...."); encoder.waitForAttachment(); //Wait for user input before continuing so that we can see the events //being sent when using the encoder Console.WriteLine("Press any key to end..."); Console.Read(); //Since user input is read we can terminate the program, so we'll close //the encoder object encoder.close(); //set the object to null to get it out of memory encoder = null; //if no expcetions where thrown at this point it is safe to terminate Console.WriteLine("ok"); } catch (PhidgetException ex) { Console.WriteLine(ex.Description); } }
//Attach event handler...populate our encoder info fields and enable our editable fields void encoder_Attach(object sender, AttachEventArgs e) { Phidgets.Encoder attached = (Phidgets.Encoder)sender; attachedTxt.Text = attached.Attached.ToString(); nameTxt.Text = attached.Name; serialTxt.Text = attached.SerialNumber.ToString(); versionTxt.Text = attached.Version.ToString(); numEncodersTxt.Text = attached.encoders.Count.ToString(); numInputsTxt.Text = attached.inputs.Count.ToString(); for (int i = 0; i < attached.inputs.Count; i++) { ((CheckBox)inputArray[i]).Visible = true; } for (int i = 0; i < attached.encoders.Count; i++) { encoderCmb.Items.Add(i); } encoderCmb.SelectedIndex = 0; encoderCmb.Enabled = true; switch (attached.ID) { case Phidget.PhidgetID.ENCODER_HS_4ENCODER: enabledChk.Enabled = true; timeChangeLabel.Text = "Time since last change (μs):"; break; case Phidget.PhidgetID.ENCODER_HS_1ENCODER: case Phidget.PhidgetID.ENCODER_1ENCODER_1INPUT: enabledChk.Enabled = false; timeChangeLabel.Text = "Time since last change (ms):"; indexPositionTxt.Text = "Unsupported"; break; default: break; } }
//Detach event code...We'll clear our display fields and disable our editable fields while device is not attached //as trying to communicate with the device while not attached will generate a PhidgetException. In this example, //I have coded so that this should not occur, but best practice would be to catch it and handle it accordingly void encoder_Detach(object sender, DetachEventArgs e) { Phidgets.Encoder detached = (Phidgets.Encoder)sender; attachedTxt.Text = detached.Attached.ToString(); nameTxt.Text = ""; serialTxt.Text = ""; versionTxt.Text = ""; numEncodersTxt.Text = ""; numInputsTxt.Text = ""; positionTxt.Text = ""; encoderPositionTxt.Text = ""; timeTxt.Text = ""; velocityTextBox.Text = ""; for (int i = 0; i < 4; i++) { ((CheckBox)inputArray[i]).Visible = false; } enabledChk.Enabled = false; enabledChk.Checked = false; indexPositionTxt.Clear(); encoderCmb.Enabled = false; encoderCmb.Items.Clear(); }