コード例 #1
0
        /// <summary>
        ///     Delays 15ms and tries to get response from device. Maximum attempts can be specified
        /// </summary>
        /// <param name="command"></param>
        /// <param name="type"></param>
        /// <param name="maxAttempts"></param>
        /// <returns></returns>
        private int SensorRead()
        {
            if (IoSerial.Instance.Connected)
            {
                var found = false;
                var i     = 0;
                // we dont know when the response will come, so lets just go through the messages till we hit gold.
                while (!found)
                {
                    var val = IoSerial.Instance.ReadBuffer(ReaderFunctionIndex.ToString(), CommandType.Analog);
                    if (val != null)
                    {
                        return(Convert.ToInt32(val));
                    }

                    //quit after max attempts
                    if (i++ > BufferReadAttempts)
                    {
                        break;
                    }

                    //because duh..
                    //give the device some time.
                    Thread.Sleep(15);
                }
            }
            return(-1);
        }
コード例 #2
0
 /// <summary>
 ///     Initialize the contnous sensor loop.
 ///     This method must not be used if sensor was initialized with continous mode.
 /// </summary>
 /// <param name="readinterval"></param>
 public void StartContinousReading(long readinterval)
 {
     //            if (timer == null)
     //            {
     //                timer = new Timer(readinterval);
     //
     //                // Hook up the Elapsed event for the timer.
     //                timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
     //
     //                timer.Enabled = true;
     //            }
     IoSerial.Instance.Write(ReaderFunctionIndex.ToString(), CommandType.Command);
 }
コード例 #3
0
 private void InstanceOnNewMessageArrived(Message msg)
 {
     if (SensorValueChanged != null)
     {
         if (msg != null && msg.Command != String.Empty)
         {
             try
             {
                 if (msg.Response.FunctionIndex == ReaderFunctionIndex.ToString())
                 //update the events if the this is the right sensor
                 {
                     CurrentValue = Convert.ToInt32(Response.GetResponse(msg).Value);
                     SensorValueChanged?.Invoke(Convert.ToInt32(Response.GetResponse(msg).Value));
                 }
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.Message);
             }
         }
     }
 }