﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>RoboticsConnection User Forum / RoboticsConnection Software Support / Serializer .NET Library  / When to use Serializer.Run() and Serializer.PumpEvents() / Latest Posts</title><generator>InstantForum.NET v4.1.4</generator><description>RoboticsConnection User Forum</description><link>http://www.roboticsconnection.com/userForums/</link><webMaster>info@roboticsconnection.com</webMaster><lastBuildDate>Thu, 04 Dec 2008 13:47:12 GMT</lastBuildDate><ttl>20</ttl><item><title>When to use Serializer.Run() and Serializer.PumpEvents()</title><link>http://www.roboticsconnection.com/userForums/Topic4-6-1.aspx</link><description>&lt;SPAN class=Forum_Normal id=spBody&gt;For those of you who have used the Serializer .NET library, you have probably seen that there are two ways to allow the library to send out events for the various objects as your application is running.   &lt;P&gt;You can either use:&lt;/P&gt;&lt;P&gt;1.) Serializer.Run(), or&lt;/P&gt;&lt;P&gt;2.) In an infinite loop, call Serializer.PumpEvents()&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Serializer.Run()&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;The Serializer.Run() method is useful if don't need to perform any processing in between events (such has handle button clicks, read joysticks, invoke other methods or interfaces).  Thus, if you have a simple application where all you want to do is print the output of various sensors values to the screen, and you have nothing else to do, then Serializer.Run() is your command.&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;using RoboticsConnection.Serializer;&lt;BR&gt;using RoboticsConnection.Serializer.Ids;&lt;BR&gt;using RoboticsConnection.Serializer.Sensors;&lt;BR&gt;using RoboticsConnection.Serializer.Components;&lt;BR&gt;using RoboticsConnection.Serializer.Controllers;&lt;BR&gt;using System;&lt;BR&gt;&lt;BR&gt;namespace test&lt;BR&gt;{&lt;BR&gt;   class Program&lt;BR&gt;   {&lt;BR&gt;      static Serializer serializer;&lt;BR&gt;      static GP2D12 irSensor;&lt;BR&gt;&lt;BR&gt;      static void Main(string[] args)&lt;BR&gt;      {&lt;BR&gt;          serializer = new Serializer();&lt;BR&gt;&lt;BR&gt;          // Sign up for the CommunicationStarted event, so we'll know&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;          // that the library has established communications w/ the board&lt;BR&gt;          serializer.CommunicationStarted += new SerializerEventHandler(serializer_CommunicationStarted);&lt;BR&gt;&lt;BR&gt;          // Create some object:&lt;BR&gt;&lt;FONT size=3&gt;&lt;FONT size=2&gt;          irSensor = new GP2D12(serializer);&lt;BR&gt;          irSensor.Pin = AnalogPinId.Pin2; &lt;BR&gt;          irSensor.UpdateFrequency = 50;&lt;BR&gt;          irSensor.DistanceChangedThreshold = 0.5;  &lt;BR&gt;&lt;BR&gt;          // Sign up to receive events from the GP2D12 object:    &lt;BR&gt;          irSensor.DistanceChanged += new SerializerComponentEventHandler(irSensor_DistanceChanged);&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&lt;FONT size=3&gt;&lt;FONT size=2&gt;          serializer.StartCommunication();&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;BR&gt;          // Enter the run loop, which constantly invokes Serializer.PumpEvents()&lt;BR&gt;          // Communication with the serializer board is started and this thread &lt;BR&gt;          // is blocked until ShutDown() is called (presumably in an event handler).&lt;BR&gt;          // As PumpEvents() gets called internally, event for various objects, e.g. GP2D12&lt;BR&gt;          // will be signalled, and our handlers below will be invoked.&lt;BR&gt;          serializer.Run();&lt;BR&gt;      }&lt;BR&gt;&lt;BR&gt;      // This gets invoked once the Serializer object has established communication&lt;BR&gt;      // with the Serializer board:&lt;BR&gt;      static void serializer_CommunicationStarted(Serializer sender)&lt;BR&gt;      {&lt;BR&gt;          Console.WriteLine("Communication Started.");&lt;BR&gt;      }&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;      // This gets invoked once the GP2D12 distance has changed&lt;BR&gt;      // more than the threshold amount specified above.&lt;BR&gt;      static void irSensor_DistanceChanged(SerializerComponent sender)&lt;BR&gt;      {&lt;BR&gt;          Console.WriteLine("New gp2d12 distance: {0}", irSensor.Distance);&lt;BR&gt;      }&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;BR&gt;&lt;FONT size=2&gt;   }&lt;BR&gt;}&lt;/FONT&gt;&lt;/FONT&gt;&lt;BR&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Serializer.PumpEvents()&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;If you want to perform other processing in between events sent out from the Serializer .NET library, then you'll want to set up your own internal loop, whether it be a while(true) { } loop, or a Timer.Tick() handler (via a Windows Form), and invoke Serializer.PumpEvents() within that loop.  Building on the example above you would then have:&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;using RoboticsConnection.Serializer;&lt;BR&gt;using RoboticsConnection.Serializer.Ids;&lt;BR&gt;using RoboticsConnection.Serializer.Sensors;&lt;BR&gt;using RoboticsConnection.Serializer.Components;&lt;BR&gt;using RoboticsConnection.Serializer.Controllers;&lt;BR&gt;using System;&lt;BR&gt;&lt;BR&gt;namespace test&lt;BR&gt;{&lt;BR&gt;   class Program&lt;BR&gt;   {&lt;BR&gt;      static Serializer serializer;&lt;BR&gt;      static GP2D12 irSensor;&lt;BR&gt;&lt;BR&gt;      static void Main(string[] args)&lt;BR&gt;      {&lt;BR&gt;          serializer = new Serializer();&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;          serializer.PortName = "COM11";&lt;BR&gt;&lt;BR&gt;          // Sign up for the CommunicationStarted event, so we'll know&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;          // that the library has established communications w/ the board&lt;BR&gt;          serializer.CommunicationStarted += new SerializerEventHandler(serializer_CommunicationStarted);&lt;BR&gt;&lt;BR&gt;          // Create some object:&lt;BR&gt;&lt;FONT size=3&gt;&lt;FONT size=2&gt;          irSensor = new GP2D12(serializer);&lt;BR&gt;          irSensor.Pin = AnalogPinId.Pin2; &lt;BR&gt;          irSensor.UpdateFrequency = 50;&lt;BR&gt;          irSensor.DistanceChangedThreshold = 0.5;  &lt;BR&gt;&lt;BR&gt;          // Sign up to receive events from the GP2D12 object:    &lt;BR&gt;          irSensor.DistanceChanged += new SerializerComponentEventHandler(irSensor_DistanceChanged);&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&lt;FONT size=3&gt;&lt;FONT size=2&gt;&lt;FONT size=2&gt;&lt;/P&gt;&lt;P&gt;          serializer.StartCommunication();&lt;/FONT&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;BR&gt;          // Enter an infinite run loop, which constantly invokes Serializer.PumpEvents().&lt;BR&gt;          // This is doing the same thing as Serializer.Run(), however, you get a chance to&lt;BR&gt;          // do other tasks in between the call to Serializer.PumpEvents().&lt;BR&gt;          while(true)&lt;BR&gt;          {&lt;BR&gt;              // Do whatever you need to do here, such as set motor speeds, move servos,&lt;BR&gt;              // make navigation decisions, etc.&lt;BR&gt;&lt;BR&gt;              // Invoke Serializer.PumpEvents() so that any outstanding events &lt;BR&gt;              // that need to be triggered occurs.&lt;BR&gt;              Serializer.PumpEvents();&lt;BR&gt;          }&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&lt;BR&gt;      }&lt;BR&gt;&lt;BR&gt;      // This gets invoked once the Serializer object has established communication&lt;BR&gt;      // with the Serializer board:&lt;BR&gt;      static void serializer_CommunicationStarted(Serializer sender)&lt;BR&gt;      {&lt;BR&gt;          Console.WriteLine("Communication Started.");&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&lt;BR&gt;      }&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;      // This gets invoked once the GP2D12 distance has changed&lt;BR&gt;      // more than the threshold amount specified above.&lt;BR&gt;      static void irSensor_DistanceChanged(SerializerComponent sender)&lt;BR&gt;      {&lt;BR&gt;          Console.WriteLine("new gp2d12 distance: {0}", irSensor.Distance);&lt;BR&gt;      }&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;BR&gt;&lt;FONT size=2&gt;   }&lt;BR&gt;}&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Windows Forms...&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;If you want to build a Windows Form application, and interface the Serializer .NET library, it's really as simple as adding a Timer object (Components ToolBox) to your form...&lt;/P&gt;&lt;P&gt;&lt;IMG src="http://www.roboticsconnection.com/userForums/Uploads/Images/109ff106-5b45-46a5-8833-5138.jpg"&gt;&lt;/P&gt;&lt;P&gt;setting the timer1 Enabled property to false, setting the Interval property to 10 (for example).   If you enable the timer1 Enabled property to True at this point, a race condition exists, and it may attempt to invoke serializer.PumpEvents() in the timer handler before the serializer object exists.&lt;/P&gt;&lt;P&gt;&lt;IMG src="http://www.roboticsconnection.com/userForums/Uploads/Images/f7a25f9a-97e0-4525-b526-82c6.jpg"&gt;&lt;/P&gt;&lt;P&gt;and adding a handler to handle the Tick event...&lt;/P&gt;&lt;FONT color=#0000ff size=2&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;using&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New" color=#000000&gt; System;&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT face="Courier New"&gt;using&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New" color=#000000&gt; System.Collections.Generic;&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT face="Courier New"&gt;using&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New" color=#000000&gt; System.ComponentModel;&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT face="Courier New"&gt;using&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New" color=#000000&gt; System.Data;&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT face="Courier New"&gt;using&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New" color=#000000&gt; System.Drawing;&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT face="Courier New"&gt;using&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New" color=#000000&gt; System.Text;&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT face="Courier New"&gt;using&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New" color=#000000&gt; System.Windows.Forms;&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT face="Courier New"&gt;using&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New" color=#000000&gt; RoboticsConnection.Serializer;&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT face="Courier New"&gt;using&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New" color=#000000&gt; RoboticsConnection.Serializer.Components;&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT face="Courier New"&gt;using&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New" color=#000000&gt; RoboticsConnection.Serializer.Controllers;&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT face="Courier New"&gt;using&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New" color=#000000&gt; RoboticsConnection.Serializer.Ids;&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;FONT face="Courier New"&gt;using&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New" color=#000000&gt; RoboticsConnection.Serializer.Sensors;&lt;/FONT&gt;&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;namespace&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New" color=#000000&gt; SerializerTestApp&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" color=#000000&gt;{&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=2&gt;    public&lt;/FONT&gt;&lt;FONT color=#000000 size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;partial&lt;/FONT&gt;&lt;FONT color=#000000 size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;class&lt;/FONT&gt;&lt;FONT color=#000000 size=2&gt; &lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;Form1&lt;/FONT&gt;&lt;FONT color=#000000 size=2&gt; : &lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;Form&lt;BR&gt;    &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New" color=#000000&gt;{&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=2&gt;        private&lt;/FONT&gt;&lt;FONT color=#000000 size=2&gt; &lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;Serializer&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New" color=#000000&gt; serializer;&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=2&gt;        private&lt;/FONT&gt;&lt;FONT color=#000000 size=2&gt; &lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;PwmDCMotorController&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New" color=#000000&gt; motor;&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=2&gt;        private&lt;/FONT&gt;&lt;FONT color=#000000 size=2&gt; &lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;GP2D12&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New" color=#000000&gt; ir;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" color=#0000ff size=2&gt;        public&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt; Form1()&lt;BR&gt;        &lt;/FONT&gt;&lt;FONT face="Courier New"&gt;{&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;            InitializeComponent();&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;            serializer = &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=2&gt;new&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;Serializer&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt;();&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;            serializer.PortName = &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" color=#a31515 size=2&gt;"COM1"&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt;;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;            serializer.BaudRate = 19200;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;            serializer.Units = &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" color=#2b91af size=2&gt;Units&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt;.English;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;            serializer.CommunicationStarted += &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=2&gt;new&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;SerializerEventHandler&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt;(serializer_CommunicationStarted);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;            motor = &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=2&gt;new&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;PwmDCMotorController&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt;(serializer);&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;            motor.DCMotorId = &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" color=#2b91af size=2&gt;DCMotorId&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt;.DCMotor1;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;            motor.Speed = 0;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;            ir = &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=2&gt;new&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;GP2D12&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt;(serializer);&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;            ir.DistanceChangedThreshold = 0.1;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;            ir.Pin = &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" color=#2b91af size=2&gt;AnalogPinId&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt;.Pin0;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;            ir.UpdateFrequency = 50; &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;&lt;FONT face="Courier New"&gt;// 50 msec&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt;            ir.DistanceChanged += &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=2&gt;new&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;SerializerComponentEventHandler&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt;(ir_DistanceChanged);&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt;            serializer.StartCommunication();&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;        } &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=2&gt;        void&lt;/FONT&gt;&lt;FONT size=2&gt; ir_DistanceChanged(&lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;SerializerComponent&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt; sender)&lt;BR&gt;        &lt;/FONT&gt;&lt;FONT face="Courier New"&gt;{&lt;BR&gt;            &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#2b91af size=2&gt;Console&lt;/FONT&gt;&lt;FONT size=2&gt;.WriteLine(&lt;/FONT&gt;&lt;FONT color=#a31515 size=2&gt;"Distance Changed: {0}"&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt;, ir.Distance);&lt;BR&gt;        &lt;/FONT&gt;&lt;FONT face="Courier New"&gt;}&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=2&gt;        void&lt;/FONT&gt;&lt;FONT size=2&gt; serializer_CommunicationStarted(&lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;Serializer&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt; sender)&lt;BR&gt;        &lt;/FONT&gt;&lt;FONT face="Courier New"&gt;{&lt;BR&gt;            &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#2b91af size=2&gt;Console&lt;/FONT&gt;&lt;FONT size=2&gt;.WriteLine(&lt;/FONT&gt;&lt;FONT color=#a31515 size=2&gt;"Communication Started"&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt;);&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;P&gt;&lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;            // Enable timer, which is disabeld by default...&lt;/P&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;P&gt;            timer1.Enabled = &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;true&lt;/FONT&gt;&lt;FONT size=2&gt;;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt;&lt;BR&gt;        }&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#0000ff size=2&gt;        private&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;void&lt;/FONT&gt;&lt;FONT size=2&gt; timer1_Tick(&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;object&lt;/FONT&gt;&lt;FONT size=2&gt; sender, &lt;/FONT&gt;&lt;FONT color=#2b91af size=2&gt;EventArgs&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt; e)&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;        {&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;&lt;FONT face="Courier New"&gt;            // Set motor speed based on distance of object&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;&lt;FONT face="Courier New"&gt;            // in front of ir sensor.&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt;            motor.Speed = (&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" color=#0000ff size=2&gt;int&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt;)(ir.Distance * 3.22);&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#008000 size=2&gt;&lt;FONT face="Courier New"&gt;            // Let library signal outstanding events:&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt;            serializer.PumpEvents();&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;        }&lt;BR&gt;    &lt;/FONT&gt;&lt;FONT face="Courier New"&gt;}&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;}&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;So, every 100 msec, the timer1_Tick() handler is setting the speed of a motor based on the distance of the object in front of the ir sensor.  Since the speed range of the motor is 0 to 100, and the distance range of the GP2D12 is 4 to 31, the ir distance is scaled by a factor of 3.22 so you can use the full motor speed range for this experiement.   In fact, it's just and example...and the logic isn't really that useful, eh? :)  Notice that even though the motor speed is being set every 100 msec, the internal distance property for the GP2D12 is being set every 50 msec.  When the distance changes more than the specified threshold specified above, then the &lt;FONT face="Courier New" size=2&gt;DistanceChanged event will occur, and the ir_DistanceChanged() handler will be invoked.&lt;/FONT&gt;&lt;/P&gt;&lt;/SPAN&gt;</description><pubDate>Sat, 19 Jan 2008 17:41:04 GMT</pubDate><dc:creator>jsummerour</dc:creator></item></channel></rss>