Esempio n. 1
0
        //handles communication and processing of queue
        private void BackgroundLoop()
        {
            while (true)
            {
                if (LinkActive)
                {
                    try
                    {
                        //update all queued devices
                        while (devices.Count > 0)
                        {
                            //send the request or command
                            devices.TryDequeue(out GenericAbstractDevice device);
                            port.TransmitRequestOrCommand(device.GetMessage());
                            //if it's a sensor and needs a reply
                            if (device.NeedsResponse)
                            {
                                //get the reply
                                ROVMessage msg = port.WaitReceiveData(1000);
                                if (msg != null) //if it worked and we got data back, otherwise just ignore
                                {
                                    //update the device's data
                                    device.UpdateData(msg);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        //show exception dialog
                        if (CommunicationException != null)
                        {
                            CommunicationException(this, ex);
                        }
                        //stop serial communication code by closing port
                        LinkActive = false;
                    }
                    //fire timers if necessary

                    /*if ((DateTime.Now.Ticks - prevTime) > 10 * TimeSpan.TicksPerMillisecond)
                     * {
                     *  //System.Diagnostics.Debug.WriteLine(DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond);
                     *  //System.Diagnostics.Debug.WriteLine(devices.Count);
                     *  prevTime = DateTime.Now.Ticks;
                     *  thousandCount++;
                     *  hundredCount++;
                     *  if (thousandCount > 100)
                     *  {
                     *      thousandCount = 0;
                     *      if (ThousandElapsed != null)
                     *      {
                     *          ThousandElapsed(this, null);
                     *      }
                     *  }
                     *  else if (hundredCount > 10)
                     *  {
                     *      hundredCount = 0;
                     *      if (HundredElapsed != null)
                     *      {
                     *          HundredElapsed(this, null);
                     *      }
                     *  }
                     *  else
                     *  {
                     *      if (TenElapsed != null)
                     *      {
                     *          TenElapsed(this, null);
                     *      }
                     *  }
                     * }*/
                }
                else
                {
                    Thread.Sleep(10);
                }
            }
        }
Esempio n. 2
0
        //handles communication and processing of queue
        private void BackgroundLoop()
        {
            while (!shuttingDown)
            {
                if (linkActive)
                {
                    try
                    {
                        //update all queued devices
                        while (devices.Count > 0)
                        {
                            //send the request or command
                            devices.TryDequeue(out GenericAbstractDevice device);
                            port.TransmitRequestOrCommand(device.GetMessage());
                            //if it's a sensor and needs a reply
                            if (device.NeedsResponse)
                            {
                                //get the reply
                                ROVMessage msg = port.WaitReceiveData(1000);
                                //update the device's data
                                device.UpdateData(msg);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        //log history before exception for debugging
                        Logger.LogString("Start Communication Log Dump\n" + port.GetHistory() + "\nEnd Communication Log Dump");
                        Logger.LogException(ex);
                        //cease communication
                        LinkActive = false;
                        //show exception dialog
                        if (CommunicationException != null)
                        {
                            CommunicationException(this, ex);
                        }
                    }
                    //fire timers if necessary

                    /*if ((DateTime.Now.Ticks - prevTime) > 10 * TimeSpan.TicksPerMillisecond)
                     * {
                     *  //System.Diagnostics.Debug.WriteLine(DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond);
                     *  //System.Diagnostics.Debug.WriteLine(devices.Count);
                     *  prevTime = DateTime.Now.Ticks;
                     *  thousandCount++;
                     *  hundredCount++;
                     *  if (thousandCount > 100)
                     *  {
                     *      thousandCount = 0;
                     *      if (ThousandElapsed != null)
                     *      {
                     *          ThousandElapsed(this, null);
                     *      }
                     *  }
                     *  else if (hundredCount > 10)
                     *  {
                     *      hundredCount = 0;
                     *      if (HundredElapsed != null)
                     *      {
                     *          HundredElapsed(this, null);
                     *      }
                     *  }
                     *  else
                     *  {
                     *      if (TenElapsed != null)
                     *      {
                     *          TenElapsed(this, null);
                     *      }
                     *  }
                     * }*/
                }
                else
                {
                    Thread.Sleep(100);
                }
            }
        }