Example #1
0
        /// <summary>
        /// Sends the given command to the given device, then fires the PollRecieved event with the result.
        /// </summary>
        /// <param name="sendCommand">The command to send.</param>
        /// <param name="nodeId">The Node to poll.</param>
        /// <param name="deviceId">The Device to poll on that Node.</param>
        public CANPing(Commands sendCommand, byte nodeId, byte deviceId)
        {
            expectedResult = responseCommands[sendCommand];

            lastMessage = CANInterface.SendMessage(nodeId, deviceId, sendCommand);

            CANInterface.MessageSent     += CANInterface_MessageSent;
            CANInterface.MessageReceived += CANInterface_MessageReceived;
            waitForResponse.Interval      = 100;
            waitForResponse.Tick         += WaitForResponse_Tick;
        }
Example #2
0
        private void CANInterface_MessageReceived(C4UFX.CANMessage message)
        {
            if (message.Data[0] == (byte)Commands.CmdStatLightValue && CANInterface.IdToFndTnd(message.ID).FromDevice == deviceId)
            {
                if (brightness != message.Data[1])
                {
                    BrightnessChanged?.Invoke(message.Data[1]);
                }

                brightness = message.Data[1];
            }
        }
        /// <summary>
        /// Checks the CAN for incoming messages.
        /// </summary>
        private static void GetIncomingMessages()
        {
            try
            {
                int Result = C4UFX.Resp_Ack;
                C4UFX.CANMessage CANMess = new C4UFX.CANMessage();

                while (Result == C4UFX.Resp_Ack)
                {
                    Result = C4UFX.GetCANMess(0, 0, ref CANMess);
                    if (Result == C4UFX.Resp_Ack)
                    {
                        MessageReceived?.Invoke(CANMess);
                    }
                }
            }
            catch
            {
                CANStatus?.Invoke(CANSTAT.NOINTERFACE); //Error found by not connecting interface, unsure if that's the only possible reason
            }
        }
Example #4
0
        /// <summary>
        /// Polls the network with the given command, then fires the PollRecieved event with the results.
        /// </summary>
        /// <param name="sendCommand">The command to send.</param>
        /// <param name="nodes">The Nodes to poll.</param>
        /// <param name="devices">The Devices to poll on each Node.</param>
        public CANPing(Commands sendCommand, Dictionary <byte, List <byte> > nodeDevices)
        {
            if (nodeDevices.Count == 0)
            {
                throw new Exception("Cannot ping 0 nodes.");
            }

            expectedResult = responseCommands[sendCommand];

            foreach (byte node in nodeDevices.Keys)
            {
                lastMessage = CANInterface.SendMessage(node, 0, sendCommand);
                foreach (byte device in nodeDevices[node])
                {
                    lastMessage = CANInterface.SendMessage(node, device, sendCommand);
                }
            }

            CANInterface.MessageSent     += CANInterface_MessageSent;
            CANInterface.MessageReceived += CANInterface_MessageReceived;
            waitForResponse.Interval      = 100;
            waitForResponse.Tick         += WaitForResponse_Tick;
        }
        /// <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;
                }
            }
        }