Main Site Documentation

What am I missing... I have all the references in the right place


#1

I have a Panda II with a ProtoShield… I am workong on CAN project. On the protoshield I have my MCP2551 all wired up…I have the 4.1.8.0 firmware loaded…
I copied and pasted the CAN demo code from the wiki into my project…
Without making any changes, I am having problems accessing the CAN class.

static CAN.Message[] msgList;

Throws the following error:

[quote]The type or namespace name ‘Message’ does not exist in the namespace ‘CAN’ (are you missing an assembly reference?)
[/quote]
I made sure that I have the GHIElectronics.NETMF.Hardware reference in place, as well as the FEXPanda_II_GHIElectronics.NETMF.FEZ.

I can get around the error by using the full, long reference for all variables. Like

static GHIElectronics.NETMF.Hardware.CAN.Message[] msgList;

That will compile, but it’s really long… What am I missing?


#2

Please post the complete code here.


#3

Here is the “broken” code:

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.Hardware;
 
public class Program
{
    // Messages list
    static CAN.Message[] msgList;
 
    public static void Main()
    {
        // Set the system time. CAN messages will have a time stamp
        Utility.SetLocalTime(new DateTime(2011, 2, 14, 0, 0, 0));
 
        int T1, T2, BRP;
 
        // These numbers were calculated using the calculator on this link:
        // http://www.kvaser.com/can/protocol/index.htm
        // We used the very first value from the calculator output
 
        /////////////////////////////////////////////////////////////////////////////////////////////
        // Bitrate 250Kbps
        // CLK = 72 Mhz, with BRP = 12 -> 6Mhz CAN clock
        // 6Mhz/250Kbps = 24 TQ
        // T1 = 16 minus 1 for sync = 15
        // T2 = 8
        // 15 + 1 + 8 = 24 TQs which is what we need
        /////////////////////////////////////////////////////////////////////////////////////////////
        // uncomment to use this bit timing
        //BRP = 12;
        //T1 = 15;
        //T2 = 8;
 
        /////////////////////////////////////////////////////////////////////////////////////////////
        // Bitrate 125Kbps
        // CLK = 72 Mhz, with BRP = 24 -> 3Mhz CAN clock
        // 3Mhz/125Kbps = 24 TQ
        // T1 = 16 minus 1 for sync = 15
        // T2 = 8
        // 15 + 1 + 8 = 24 TQs which is what we need
        /////////////////////////////////////////////////////////////////////////////////////////////
        // uncomment to use this bit timing
        BRP = 24;
        T1 = 15;
        T2 = 8;
 
        // For 500Kbps you can use BRP=6 and for 1Mbps you can use BRP=3 ...and so on
 
        // Use channel 1
        CAN can = new CAN(CAN.Channel.Channel_1, (uint)(((T2 - 1) << 20) | ((T1 - 1) << 16) | ((BRP - 1) << 0)));
 
        // create a message list of 100 messages
        msgList = new CAN.Message[100];
        for (int i = 0; i < msgList.Length; i++)
            msgList[i] = new CAN.Message();
 
        /*
        // example for sending one message
        // msg ID
        msgList[0].ArbID = 0x55;
        msgList[0].Data[0] = 1;
        msgList[0].Data[1] = 2;
        msgList[0].Data[2] = 3;
        msgList[0].Data[3] = 4;
        msgList[0].Data[4] = 5;
        msgList[0].Data[5] = 6;
        msgList[0].Data[6] = 7;
        msgList[0].Data[7] = 8;
        // Send the 8 bytes for example
        msgList[0].DLC = 8;
        msgList[0].IsEID = false;
        msgList[0].IsRTR = false;
        // Send one messages
        int numberOfMessagesPosted = can.PostMessages(msgList, 0, 1);
        */
 
        // subscribe to events
        can.DataReceivedEvent += new CANDataReceivedEventHandler(can_DataReceivedEvent);
        can.ErrorReceivedEvent += new CANErrorReceivedEventHandler(can_ErrorReceivedEvent);
 
        // sleep forever
        Thread.Sleep(Timeout.Infinite);
    }
 
    static void can_DataReceivedEvent(CAN sender, CANDataReceivedEventArgs args)
    {
        Debug.Print(">>> can_DataReceivedEvent <<<");
 
        // read as many messages as possible
        int count = sender.GetMessages(msgList, 0, msgList.Length);
        for (int i = 0; i < count; i++)
        {
            Debug.Print("MSG: ID = " + msgList[i].ArbID + " at time = " + msgList[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;
        }
    }

Here is what I had to do to get around the problem. This compiles, but the read event never triggers.

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

namespace Example
{
    public class Program
    {
        // Messages list
        static GHIElectronics.NETMF.Hardware.CAN.Message[] msgList;

        public static void Main()
        {
            // Set the system time. CAN messages will have a time stamp
            Utility.SetLocalTime(new DateTime(2011, 2, 14, 0, 0, 0));

            int T1, T2, BRP;

            // These numbers were calculated using the calculator on this link:
            // http://www.kvaser.com/can/protocol/index.htm
            // We used the very first value from the calculator output

            /////////////////////////////////////////////////////////////////////////////////////////////
            // Bitrate 250Kbps
            // CLK = 72 Mhz, with BRP = 12 -> 6Mhz CAN clock
            // 6Mhz/250Kbps = 24 TQ
            // T1 = 16 minus 1 for sync = 15
            // T2 = 8
            // 15 + 1 + 8 = 24 TQs which is what we need
            /////////////////////////////////////////////////////////////////////////////////////////////
            // uncomment to use this bit timing
            //BRP = 12;
            //T1 = 15;
            //T2 = 8;

            /////////////////////////////////////////////////////////////////////////////////////////////
            // Bitrate 125Kbps
            // CLK = 72 Mhz, with BRP = 24 -> 3Mhz CAN clock
            // 3Mhz/125Kbps = 24 TQ
            // T1 = 16 minus 1 for sync = 15
            // T2 = 8
            // 15 + 1 + 8 = 24 TQs which is what we need
            /////////////////////////////////////////////////////////////////////////////////////////////
            // uncomment to use this bit timing
            BRP = 24;
            T1 = 15;
            T2 = 8;

            // For 500Kbps you can use BRP=6 and for 1Mbps you can use BRP=3 ...and so on

            // Use channel 1
            GHIElectronics.NETMF.Hardware.CAN MYcan = new GHIElectronics.NETMF.Hardware.CAN(GHIElectronics.NETMF.Hardware.CAN.Channel.Channel_1, (uint)(((T2 - 1) << 20) | ((T1 - 1) << 16) | ((BRP - 1) << 0)));

            // create a message list of 100 messages
            msgList = new GHIElectronics.NETMF.Hardware.CAN.Message[100];
            for (int i = 0; i < msgList.Length; i++)
                msgList[i] = new GHIElectronics.NETMF.Hardware.CAN.Message();

            /*
            // example for sending one message
            // msg ID
            msgList[0].ArbID = 0x55;
            msgList[0].Data[0] = 1;
            msgList[0].Data[1] = 2;
            msgList[0].Data[2] = 3;
            msgList[0].Data[3] = 4;
            msgList[0].Data[4] = 5;
            msgList[0].Data[5] = 6;
            msgList[0].Data[6] = 7;
            msgList[0].Data[7] = 8;
            // Send the 8 bytes for example
            msgList[0].DLC = 8;
            msgList[0].IsEID = false;
            msgList[0].IsRTR = false;
            // Send one messages
            int numberOfMessagesPosted = can.PostMessages(msgList, 0, 1);
            */

            // subscribe to events
            MYcan.DataReceivedEvent += new CANDataReceivedEventHandler(can_DataReceivedEvent);
            MYcan.ErrorReceivedEvent += new CANErrorReceivedEventHandler(can_ErrorReceivedEvent);

            // sleep forever
            Debug.Print("Waiting for wakeup call");
            Thread.Sleep(Timeout.Infinite);
            Debug.Print("How did I get here");
        }

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

            // read as many messages as possible
            int count = sender.GetMessages(msgList, 0, msgList.Length);
            for (int i = 0; i < count; i++)
            {
                Debug.Print("MSG: ID = " + msgList[i].ArbID + " at time = " + msgList[i].TimeStamp);
            }
        }

        static void can_ErrorReceivedEvent(GHIElectronics.NETMF.Hardware.CAN sender, CANErrorReceivedEventArgs args)
        {
            Debug.Print(">>> can_ErrorReceivedEvent <<<");

            switch (args.Error)
            {
                case GHIElectronics.NETMF.Hardware.CAN.Error.Overrun:
                    Debug.Print("Overrun error. Message lost");
                    break;

                case GHIElectronics.NETMF.Hardware.CAN.Error.RXOver:
                    Debug.Print("RXOver error. Internal buffer is full. Message lost");
                    break;

                case GHIElectronics.NETMF.Hardware.CAN.Error.BusOff:
                    Debug.Print("BusOff error. Reset CAN controller.");
                    sender.Reset();
                    break;
            }
        }
    }
}

#4

Some progress… I changed the bit timing to


BRP = 12;
T1 = 15;
T2 = 8;

and at least now I’m getting bus errors… ???
It’s better than nothing…


#5

Nenver mind… Figured it out… I’m a totall noob :-[
I am happily receiving CAN bus events and I got my references sorted out… User Error ;D