//When the application is terminating, close the Phidgets. private void Form1_FormClosed(object sender, FormClosedEventArgs e) { servo1.Attach -= new AttachEventHandler(servo_Attach); servo1.Detach -= new DetachEventHandler(servo_Detach); servo1.PositionChange -= new PositionChangeEventHandler(servo_PositionChange); servo2.Attach -= new AttachEventHandler(servo_Attach); servo2.Detach -= new DetachEventHandler(servo_Detach); servo2.PositionChange -= new PositionChangeEventHandler(servo_PositionChange); Application.DoEvents(); servo1.close(); servo1 = null; servo2.close(); servo2 = null; }
static void Main(string[] args) { try { //Declare a Servo object Servo servo = new Servo(); //Hook the basic event handlers servo.Attach += new AttachEventHandler(servo_Attach); servo.Detach += new DetachEventHandler(servo_Detach); servo.Error += new ErrorEventHandler(servo_Error); //hook the phidget specific event handlers servo.PositionChange += new PositionChangeEventHandler (servo_PositionChange); //open the Servo object for device connections servo.open(); //Get the program to wait for a Servo to be attached Console.WriteLine("Waiting for Servo to be attached..."); servo.waitForAttachment(); //Set the initial position for the servo. I set it to 15 here just //since I am going to cycle through the positive values to show a basic //full movement if (servo.Attached) { servo.servos[0].Position = 15.00; } double i; //Wait for the motor to finish getting into position and let the user //continue Console.WriteLine("Press any key to continue..."); Console.Read(); //Move the motor from position value 15 to 231m we sleep for //10 milliseconds between each step to give the motor enough time to //move to the set position if (servo.Attached) { for (i = 15.00; i < 232.00; i++) { Thread.Sleep(10); servo.servos[0].Position = i; } } //Wait for the events to fire and display, user input will continue the //program Console.WriteLine("Press any key to end..."); Console.Read(); //user input was read so we can terminate the program now, close the //Servo object servo.close(); //set the object to null to get it out of memory servo = null; //if no exceptions were thrown at this point it is safe to terminate Console.WriteLine("ok"); } catch (PhidgetException ex) { Console.WriteLine(ex.Description); } }