Main Site Documentation

Problem with CAN en emission


#1

Hi all,

I’m posting here because I started to work with the FEZ Cobra board a few weeks ago and I have a problem with the CAN module.
I’m trying to send some words thrue the CAN protocole but it doesn’t work. I’m doing all that is said in the help, tuto, example etc, and it seems to work in the soft, but there is no word on the output pins.

here is my code :


using System;
using Microsoft.SPOT;
using System.Threading;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.Net;
using GHIElectronics.NETMF.Native;


namespace MFConsoleApplication1
{
    public class Program
    {
        // Initialisation du bus CAN pour la communication entre l'EMX et l'EPOS TRANS //
        static CAN canChannel_1 = new CAN(CAN.Channel.Channel_1, ((2 - 1) << 20) | ((5 - 1) << 16) | ((4 - 1) << 0));
        static CAN.Message[] Message_CAN_Envoye;
        static CAN.Message[] Message_CAN_Recu;

        // DEBUG //
        static OutputPort LED = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, false);
        
        public static void Main()
        {
            // create a thread handler
            Thread LED_Debug_Handler;
            LED_Debug_Handler = new Thread(LED_Debug);
            LED_Debug_Handler.Start();

            CAN_Initialisation();

            while (true)
            {
                byte[] Tableau = new byte[8];
                Tableau[0] = 0;
                Tableau[1] = 1;
                Tableau[2] = 2;
                Tableau[3] = 3;
                Tableau[4] = 4;
                Tableau[5] = 5;
                Tableau[6] = 6;
                Tableau[7] = 7;
                Send_CAN_Message(Tableau);
            }

            Debug.Print("Amazing!");

            Thread.Sleep(Timeout.Infinite);
        }

        public static void LED_Debug()
        {
            while (true)
            {
                LED.Write(!LED.Read());

                Thread.Sleep(200);
            }
        }

/// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// ///// GESTION DE LA COMMUNICATION CAN
/// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public static void CAN_Initialisation()
        {        
            // subscribe to events
            canChannel_1.DataReceivedEvent += new CANDataReceivedEventHandler(can_DataReceivedEvent);
            canChannel_1.ErrorReceivedEvent += new CANErrorReceivedEventHandler(can_ErrorReceivedEvent);
        }

        public static void Send_CAN_Message(byte[] Tab)
        {
            Message_CAN_Envoye = new CAN.Message[10];
            for (int i = 0; i < Message_CAN_Envoye.Length; i++)
                Message_CAN_Envoye[i] = new CAN.Message();


            Message_CAN_Envoye[0].ArbID = 0x55;
            for (int i = 0; i < Tab.Length; i++)
                Message_CAN_Envoye[0].Data[i] = Tab[i];
            Message_CAN_Envoye[0].DLC = Tab.Length;
            Message_CAN_Envoye[0].IsEID = false;
            Message_CAN_Envoye[0].IsRTR = false;

            int numberOfMessagesPosted = canChannel_1.PostMessages(Message_CAN_Envoye, 0, 1);
        }

        static void can_DataReceivedEvent(CAN sender, CANDataReceivedEventArgs args)
        {
            Debug.Print(">>> can_DataReceivedEvent <<<");

            Message_CAN_Recu = new CAN.Message[10];
            for (int i = 0; i < Message_CAN_Recu.Length; i++)
                Message_CAN_Recu[i] = new CAN.Message();
 
            // read as many messages as possible
            int count = sender.GetMessages(Message_CAN_Recu, 0, Message_CAN_Recu.Length);
            for (int i = 0; i < count; i++)
            {
                Debug.Print("MSG: ID = " + Message_CAN_Recu[i].ArbID + " at time = " + Message_CAN_Recu[i].TimeStamp);
            }
        }
 
        static void can_ErrorReceivedEvent(CAN sender, CANErrorReceivedEventArgs args)
        {
            Debug.Print(">>> can_ErrorReceivedEvent <<<");
 
            switch (args.Error)
            {
                case CAN.Error.Overrun:
                    Debug.Print("Overrun error. Message lost");
                    break;
 
                case CAN.Error.RXOver:
                    Debug.Print("RXOver error. Internal buffer is full. Message lost");
                    break;
 
                case CAN.Error.BusOff:
                    Debug.Print("BusOff error. Reset CAN controller.");
                    sender.Reset();
                    break;
            }
        }

When I execute the code, all seems to be ok, but if I look with an oscillo there is no message on the output CAN1TD.
If I look the PostedMessageSent properties of the CAN class, it’s seems to be ok. So I don’t understand why it doesn’t work. If someone as any idea ?


#2

CAN has many requirements to work. Did you add transceiver? Do you have 2 nodes minimum? Do you have termination resistors?

All details in this tutorial http://wiki.tinyclr.com/index.php?title=CAN

Welcome to the community.


#3

No now I don’t have all these things. Forenow I only have the board “flying” (I don’t know if the word in english). And I thought that I could see something on the oscillo, even if there weren’t any of the things you said :(.