Ejemplo n.º 1
0
      public virtual void Update()
      {
         CommStates processedState;

         do 
         {
            processedState = this.commState;

            switch (this.commState)
            {
               case CommStates.idle:
               {
                  lock (this)
                  {
                     if (this.actionQueue.Count != 0)
                     {
                        this.action = (CommAction)this.actionQueue.Dequeue();
                     }
                  }

                  if (null != this.action)
                  {
                     this.commAttemptCount = 0;
                     this.commState = CommStates.tx;
                     //Tracer.WriteHigh(TraceGroup.CANBUS, "", "action start");
                  }

                  break;
               }
               case CommStates.tx:
               {
                  this.commAttemptCount++;

                  if (this.commAttemptCount <= this.action.RetryAttemptLimit)
                  {
                     int cobId = 0;
                     byte[] frameData = this.action.GetTransmitFrame(ref cobId, this.NodeId);

                     if (null != frameData)
                     {
                        bool txResult = this.Transmit(cobId, frameData);

                        if (false != txResult)
                        {
                           this.action.TransmitComplete();

                           bool responseNeeded = this.action.ResponseNeeded();

                           if (false != responseNeeded)
                           {
                              //Tracer.WriteHigh(TraceGroup.CANBUS, "", "tx pending");
                              this.commTimeLimit = DateTime.Now.AddMilliseconds(this.action.RetryTime);
                              this.commState = CommStates.rxWait;
                           }
                           else
                           {
                              this.EvaluateAction(this.action);
                              this.action = null;
                              this.commState = CommStates.idle;
                           }
                        }
                        else
                        {
                           Tracer.WriteHigh(TraceGroup.CANBUS, "", "tx fail");
                           this.commTimeLimit = DateTime.Now.AddMilliseconds(this.TransmitFailureHoldoffTime);
                           this.commState = CommStates.txWait;
                        }
                     }
                     else
                     {
                        this.action = null;
                        this.commState = CommStates.idle;
                     }
                  }
                  else
                  {
                     this.Fault("comm failure");
                     this.action = null;
                  }

                  break;
               }
               case CommStates.txWait:
               {
                  if (DateTime.Now > this.commTimeLimit)
                  {
                     this.commState = CommStates.tx;
                  }

                  break;
               }
               case CommStates.rxWait:
               {
                  if (false != this.action.Done)
                  {
                     this.EvaluateAction(this.action);

                     if (false != this.action.Aborted)
                     {
                        this.Fault("comm abort");
                     }

                     this.action = null;
                     this.commState = CommStates.idle;
                  }
                  else if (false != this.action.Transmit)
                  {
                     this.commAttemptCount = 0;
                     this.commState = CommStates.tx;
                  }
                  else if (DateTime.Now > this.commTimeLimit)
                  {
                     this.commState = CommStates.tx;
                     Tracer.WriteHigh(TraceGroup.CANBUS, "", "response timeout");
                  }

                  break;
               }
               case CommStates.error:
               {
                  break;
               }                
            }

         }
         while (processedState != this.commState);

         if (null == this.FaultReason)
         {
            if ((false != this.consumerHeartbeatActive) &&
                (DateTime.Now > this.consumerHeartbeatTimeLimit))
            {
               this.Fault("heartbeat missing");
            }
         }
      }
Ejemplo n.º 2
0
      public virtual bool SDODownload(UInt16 index, byte subIndex, byte[] data)
      {
         SDODownload download = new SDODownload(index, subIndex, data, 0, data.Length);

         this.pendingAction = download;
         this.ScheduleAction(this.pendingAction);

         this.commEvent.Reset();
         bool actionResult = this.commEvent.WaitOne(500);

         return (actionResult);
      }
Ejemplo n.º 3
0
      public virtual byte[] SDOUpLoad(UInt16 index, byte subIndex)
      {
         byte[] result = null;
         SDOUpload upload = new SDOUpload(index, subIndex);

         this.pendingAction = upload;
         this.ScheduleAction(this.pendingAction);

         this.commEvent.Reset();
         bool actionResult = this.commEvent.WaitOne(500);

         if (false != actionResult)
         {
            result = upload.Data;
         }

         return (result);
      }
Ejemplo n.º 4
0
      /// <summary>
      /// Sets RPDO transmission type.  Functional while in device pre-operational state.
      /// </summary>
      /// <param name="pdo">TPDO to set, 1..4</param>
      /// <param name="typeValue">value to program into transmission type object</param>
      /// <returns>true when successful</returns>
      protected bool SetRPDOType(byte pdo, byte typeValue)
      {
         UInt16 index = this.GetRPDOParameterAddress(pdo);

         this.pendingAction = new SDODownload(index, 2, 1, typeValue);
         bool result = this.ExchangeCommAction(this.pendingAction);
         return (result);
      }
Ejemplo n.º 5
0
 protected virtual void EvaluateAction(CommAction action)
 {
    if (this.pendingAction == action)
    {
       this.pendingAction = null;
       this.commEvent.Set();
    }
 }
Ejemplo n.º 6
0
      /// <summary>
      /// Sets RPDO connection object id.  Functional while in device pre-operational state.
      /// </summary>
      /// <param name="pdo">TPDO to set, 1..4</param>
      /// <param name="typeValue">value to program into transmission type object</param>
      /// <returns>true when successful</returns>
      protected bool SetRPDOCobId(byte pdo, UInt32 cobId)
      {
         UInt16 index = this.GetRPDOParameterAddress(pdo);

         this.pendingAction = new SDODownload(index, 1, 4, cobId);
         bool result = this.ExchangeCommAction(this.pendingAction);
         return (result);
      }
Ejemplo n.º 7
0
      /// <summary>
      /// Sets RPDO eanble.  Functional while in device pre-operational state.
      /// </summary>
      /// <param name="pdo">RPDO to set, 1..4</param>
      /// <param name="enabled">boolean, true to enable, false to disable</param>
      /// <returns>true when successful</returns>
      protected bool SetRPDOEnable(byte pdo, bool enabled)
      {
         bool result = false;

         if ((pdo >= 1) && (pdo <= 4))
         {
            UInt32 cobId = (UInt32)((pdo * 0x100) + 0x100 + this.NodeId);

            if (false == enabled)
            {
               cobId += 0x80000000;
            }

            UInt16 index = this.GetRPDOParameterAddress(pdo);
            this.pendingAction = new SDODownload(index, 1, 4, cobId);
            result = this.ExchangeCommAction(this.pendingAction);
         }

         return (result);
      }
Ejemplo n.º 8
0
 /// <summary>
 /// Sets RPDO mapping count.  Functional while in device pre-operational state.
 /// </summary>
 /// <param name="pdo">RPDO to set, 1..4</param>
 /// <param name="count">number of mappings</param>
 /// <returns>true when successful</returns>
 protected bool SetRPDOMapCount(byte pdo, UInt32 count)
 {
    UInt16 mapIndex = this.GetRPDOMapAddress(pdo);
    this.pendingAction = new SDODownload(mapIndex, 0, 1, count);
    bool result = this.ExchangeCommAction(this.pendingAction);
    return (result);
 }
Ejemplo n.º 9
0
      /// <summary>
      /// Sets RPDO mapping.  Functional while in device pre-operational state.
      /// </summary>
      /// <param name="pdo">RPDO to set, 1..4</param>
      /// <param name="map">map to set, 1..8</param>
      /// <param name="index">index to map</param>
      /// <param name="subIndex">sub index to to map</param>
      /// <param name="octetCount">number of bytes to map</param>
      /// <returns>true when successful</returns>
      protected bool SetRPDOMap(byte pdo, byte map, UInt16 index, byte subIndex, byte octetCount)
      {
         UInt16 mapIndex = this.GetRPDOMapAddress(pdo);
         UInt32 mapDefinition = (UInt32)((index << 16) | (subIndex << 8) | (octetCount * 8));

         this.pendingAction = new SDODownload(mapIndex, map, 4, mapDefinition);
         bool result = this.ExchangeCommAction(this.pendingAction);
         return (result);
      }
Ejemplo n.º 10
0
      /// <summary>
      /// Sets TPDO event time.  Functional while in device pre-operational state.
      /// </summary>
      /// <param name="pdo">TPDO to set, 1..4</param>
      /// <param name="mS">number of milliseconds required for time event</param>
      /// <returns>true when successful</returns>
      protected bool SetTPDOEventTime(byte pdo, UInt32 mS)
      {
         UInt16 index = this.GetTPDOParameterAddress(pdo);

         this.pendingAction = new SDODownload(index, 5, 2, mS);
         bool result = this.ExchangeCommAction(this.pendingAction);
         return (result);
      }
Ejemplo n.º 11
0
      protected bool ExchangeCommAction(CommAction action, int timeout = 200, int attemptLimit = 2)
      {
         bool result = false;

         if (null == this.FaultReason)
         {
            this.pendingAction = action;
            this.ScheduleAction(action, timeout, attemptLimit);
            int exchangeLimit = (timeout * attemptLimit) + 50;
            result = this.commEvent.WaitOne(exchangeLimit);

            if (false != result)
            {
               if (false != action.Aborted)
               {
                  result = false;
               }
            }
            else
            {
               Tracer.WriteHigh(TraceGroup.CANBUS, "", "exchange failure");
            }
         }

         return (result);
      }
Ejemplo n.º 12
0
      protected void ScheduleAction(CommAction action, int timeout, int attemptLimit)
      {
         if (CommStates.error != this.commState)
         {
            lock (this)
            {
               action.RetryTime = timeout;
               action.RetryAttemptLimit = attemptLimit;
               this.actionQueue.Enqueue(action);
            }

            // this.Update(); todo how to determine calling context?
         }
      }
Ejemplo n.º 13
0
      protected void ScheduleAction(CommAction action)
      {
         if (CommStates.error != this.commState)
         {
            lock (this)
            {
               this.actionQueue.Enqueue(action);
            }

            // this.Update(); todo how to determine calling context?
         }
      }
Ejemplo n.º 14
0
 protected override void EvaluateAction(CommAction action)
 {
    base.EvaluateAction(action);
 }