/// <summary>
 /// Sends the oldest message in the queue to the CAN.
 /// </summary>
 private static void SendNextInQueue()
 {
     if (messageQueue.Count > 0)
     {
         int error             = 0;
         C4UFX.CANMessage mess = messageQueue.Dequeue();
         C4UFX.SendCANMess(0, 0, mess, out error);
         MessageSent?.Invoke(mess);
     }
 }
        /// <summary>
        /// Checks the status of the CAN.
        /// </summary>
        /// <param name="ForceErr">If true, force an error even if there is none.</param>
        private static void CANCheckStatus(bool ForceErr)
        {
            int Result;

            if (CANInterfaceStatus == CANSTAT.OK && !ForceErr)
            {
                byte[] DataBuffer = new byte[10];

                Result = C4UFX.ReadReg(0, 0, 0x2D, 1, ref DataBuffer); // EFLG
                if (Result == C4UFX.Resp_Ack)
                {
                    InterfaceWarning?.Invoke(Warning.OK, DataBuffer[0] == 0);
                    InterfaceWarning?.Invoke(Warning.RxWarning, (DataBuffer[0] & 0x02) > 0);
                    InterfaceWarning?.Invoke(Warning.TxWarning, (DataBuffer[0] & 0x04) > 0);
                    InterfaceWarning?.Invoke(Warning.Buffer0Warning, (DataBuffer[0] & 0x08) > 0);
                    InterfaceWarning?.Invoke(Warning.Buffer1Warning, (DataBuffer[0] & 0x10) > 0);
                    InterfaceWarning?.Invoke(Warning.RxPassive, (DataBuffer[0] & 0x20) > 0);
                    InterfaceWarning?.Invoke(Warning.TxPassive, (DataBuffer[0] & 0x40) > 0);
                    InterfaceWarning?.Invoke(Warning.BussOff, (DataBuffer[0] & 0x80) > 0);

                    if ((DataBuffer[0] & 0x40) > 0 || (DataBuffer[0] & 0x80) > 0)
                    {
                        errorResetTmr.Start();
                    }
                }
            }
            else
            {
                C4UFX.CANMessage errorMessage = new C4UFX.CANMessage();
                int errorCode;

                errorMessage.ID  = 0;
                errorMessage.DLC = 0;
                for (int i = 0; i < 8; i++)
                {
                    errorMessage.Data[i] = 0;
                }
                errorMessage.EID        = true;
                errorMessage.RTR        = false;
                errorMessage.MO         = 0;
                errorMessage.SendOption = 7;

                Result = C4UFX.SendCANMess(0, 0, errorMessage, out errorCode);

                switch (Result)
                {
                case C4UFX.Resp_Ack:
                    CANStatus?.Invoke(CANSTAT.OK);
                    break;

                case C4UFX.Resp_NAck:
                    // message was transmitted but there was an error, so reset
                    CANInit();
                    CANStatus?.Invoke(CANSTAT.SENDERROR);
                    break;

                case C4UFX.Resp_ErrImproperResponse:
                    CANStatus?.Invoke(CANSTAT.NOINTERFACE);
                    break;

                default:
                    CANStatus?.Invoke(CANSTAT.ERROR);
                    break;
                }
            }
        }