Main Site Documentation

CAN communication


#1

Hi there,
is CAN communication working with the 4.0.2.0 firmware (release candidate) on FEZ Domino? I cannot get a message from one board to another.
The interface seems changed because
canChannel.GetMessages(messages);
and
canChannel.PostMessages(messages);
are taking arrays of CAN.Message[].
Can you please provide a current sample for CAN communication on domino?
thx a lot.
Holger


#2

Yes it is working. The example is already in documentation, just look for the CAN class http://ghielectronics.com/downloads/NETMF/Library%20Documentation/Index.html


#3

Hi again,
i just realized that the can pins only provide td and rd and the can transceiver is missing… i thought the transceiver was included… i will be adding the transceiver and try again afterwards.
Holger


#4

Yes the pins are TTL. We leave the transceiver out so you can pick whichever fit your needs best, dual wire, isolated, single wire, LSFT…etc.


#5

I just started working with the FEZ and have been having some trouble with the CAN class.

I copied the code straight from the example at ghielectronics.com and included the reference to GHIElectronics.NETMF.Hardware. I have also followed the firmware updater video. The code compiles without errors (but a lot of obsolete warnings though) but the CAN Constructor seems to throw an exception. I have tried using both the two and three argument constructors with the same results.

the Output:

‘Microsoft.SPOT.Emulator.Sample.SampleEmulator.exe’ (Managed): Loaded ‘C:\Program Files\Microsoft .NET Micro Framework\v4.0\Assemblies\mscorlib.dll’
‘Microsoft.SPOT.Emulator.Sample.SampleEmulator.exe’ (Managed): Loaded ‘C:\Program Files\Microsoft .NET Micro Framework\v4.0\Assemblies\Microsoft.SPOT.Native.dll’
‘Microsoft.SPOT.Emulator.Sample.SampleEmulator.exe’ (Managed): Loaded ‘C:\Program Files\Microsoft .NET Micro Framework\v4.0\Assemblies\Microsoft.SPOT.Hardware.dll’
‘Microsoft.SPOT.Emulator.Sample.SampleEmulator.exe’ (Managed): Loaded ‘C:\Program Files\GHI Electronics\GHI NETMF v4.0 SDK\Assemblies\GHIElectronics.NETMF.Hardware.dll’
‘Microsoft.SPOT.Emulator.Sample.SampleEmulator.exe’ (Managed): Loaded ‘C:\Documents and Settings\Daniel Fowler\My Documents\Visual Studio 2008\Projects\MFConsoleApplication3\MFConsoleApplication3\bin\Debug\MFConsoleApplication3.exe’, Symbols loaded.
The thread ‘’ (0x2) has exited with code 0 (0x0).
#### Exception System.NotSupportedException - CLR_E_NOT_SUPPORTED (1) ####
#### Example.Program::Main [IP: 001e] ####
A first chance exception of type ‘System.NotSupportedException’ occurred in MFConsoleApplication3.exe
An unhandled exception of type ‘System.NotSupportedException’ occurred in MFConsoleApplication3.exe

The code:

using GHIElectronics.NETMF.Hardware;

namespace Example
{
    public class Program
    {
        public static void Main()
        {
            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)),10);

            // This is the message
            CAN.Message message = new CAN.Message();

            // We only use one message, more can be added.
            CAN.Message[] msgList = new CAN.Message[] { message };

            // Random data
            message.data[0] = 1;
            message.data[1] = 2;
            message.data[2] = 3;
            message.data[3] = 4;
            message.data[4] = 5;
            message.data[5] = 6;
            message.data[6] = 7;
            message.data[7] = 8;

            message.DLC = 8;
            message.ArbID = 0x55;
            message.isEID = true;
            message.isRTR = false;

            // send a message
            int sentCount;
            if (can.IsTxBufferAvailable())
                sentCount = can.PostMessages(msgList);

            // wait for response
            while (can.GetRxQueueCount() == 0)
                ;

            int receivedCount = can.GetMessages(msgList);
        }
    }
}

#6

Were you able to blink LED? Use buttons? Print debug messages?

If yes then we can start using CAN


#7

I think you are using the Emulator in your code example up there. Did you make sure to set the code to deploy on the FEZ?


#8

I have successfully deployed the blink LED and several other programs from the beginner book and I have tried this code on the board itself (would the constructor act differently on the emulator?) to the same effect.


#9

Sorry, I just ran deployed it to the FEZ and it seems to be working now. perhaps I haddn’t tessted it on the actual board since using the beginer book code which needed to be modified for the new PostMessages() and GetMessages() functions.

Anyway thanks for the help.


#10

Currently I do not have a CAN transceiver or something to talk to with CAN but I can see that the program gets to the while (can.GetRxQueueCount() == 0) delay. Is there an easy way to test the receiving of CAN messages? I tried a wire from TD to RD (like the example for serial) without success.


#11

How would the emulator handle CAN or any other GHI features?

The “Microsoft emulator” doesn’t work with GHI libraries.

So please try on the device and let us know if you need help.


#12

Absolutely not! CAN is far more complex than most people think. You actually can NOT test CAN with one device! You must have at least 2 and both MUST be configured to the perfect baud rate with 0% error to give you any reading or you will never see any messages :slight_smile:

I am trying to make things extra clear because I have seen too many trying to use CAN the wrong way :wink:


#13

I am trying to test CAN communication (which I am discovering) on FEZ card by communicating between two FEZ Cards (Domino) - so I am sure they are using the exact same baud rate.
Should I use a transceiver even if both card are communicating with 3.3V and the wires are very shorts ?
I did a test program (actually two: one for each card) and it seams to be working without transceiver but very rarely in some conditions I cannot find (only one packet is well sent each time it has worked)… So I guess there must be something wrong.


#14

Yes in the way CAN works, a transceiver is a requirement. Note that there is 2 CAN channels on our devices so you can test CAN on one device by connecting the 2 channels, through transceiver of course.