Controller Area Network

I am receiving some can messages have noticed that the received data the length is always 0 is this a bug?

Probably not likely. Best way to get eyes on the situation is for you to show a short program that demonstrates this (and describe your CAN network).

Here is the code:

void ProgramStarted()
{

        // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
        Debug.Print("Program Started");
        can = new ControllerAreaNetwork(ControllerAreaNetwork.Channel.One, ControllerAreaNetwork.Speed.Kbps500);
        sd_Card = new GHI.IO.Storage.SDCard();
        sd_Card.Mount(8000);
        
        bool fs_ready = false;
        RemovableMedia.Insert += (a, b) =>
        {
            fs_ready = true;
        };
        while (!fs_ready)
        {
            System.Threading.Thread.Sleep(50);
        }
        if (VolumeInfo.GetVolumes()[0].IsFormatted)
        {
            rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
            string lookat = rootDirectory;
        }

        fileHandle = new FileStream(rootDirectory + "\\AYLDynoChart.jpg", FileMode.Open, FileAccess.Read);
        byte[] data = new byte[10000];
        int read_count = fileHandle.Read(data, 0, data.Length);
        displayN18.DrawRaw(data, 0, 0, displayN18.Width, displayN18.Height);
        can.ErrorReceived += can_ErrorReceived;
        can.MessageAvailable += can_MessageAvailable;
        Joystick.Position position;
        timer = new GT.Timer(100);
        timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);
        timer.Start();
        can.Enabled = true;
        
    }

private void can_MessageAvailable(ControllerAreaNetwork sender, ControllerAreaNetwork.MessageAvailableEventArgs e)
{
FileStream canFile = new FileStream(rootDirectory + “\CANData.txt”, FileMode.Open, FileAccess.ReadWrite);
var received = sender.ReadMessage();
ControllerAreaNetwork.Message[] messageBuffer = can.ReadMessages();
var data = string.Empty;
for (int i = 0; i < 8; i++)
data += “0x” + received.Data[i].ToString(“x2”) + " ";

        Debug.Print("   CAN Message   ");
        Debug.Print("-----------------");
        Debug.Print("    ID: " + received.ArbitrationId.ToString("X"));
        Debug.Print("  Time: " + received.TimeStamp.ToString());
        Debug.Print("   RTR: " + received.IsRemoteTransmissionRequest.ToString());
        Debug.Print("   EID: " + received.IsExtendedId.ToString());
        Debug.Print("Length: " + received.Length.ToString());
        Debug.Print("  Data: " + data);
        Debug.Print("");
        byte[] arbID = BitConverter.GetBytes(received.ArbitrationId);
        byte[] sendData = new byte[received.Data.Length + arbID.Length];
        for (int i = 0; i < received.Data.Length + arbID.Length; i++)
        {
            if (i < arbID.Length)
                sendData[i] = arbID[i];
            else
                sendData[i] = received.Data[i - arbID.Length];
        }
        canFile.Write(sendData, 0, sendData.Length);
        canFile.Close();
        var lcd = new Bitmap(displayN18.Width,displayN18.Height);
        lcd.DrawText(data,Resources.GetFont(Resources.FontResources.NinaB),Colors.Green,0,0);
        displayN18.Draw(lcd);
    }

Zero size packets is valid and expected on CAN.

Sorry I didn’t explain it well enough. I receive 8 data bytes but the length says 0. I have actually sent several different sized messages but they all come back with 0 as the length.

CAN Frames always have 8 data Bytes. It’s a fixed size Frame.
The length Byte specifies how many Bytes are valid.

Okay but no matter how many valid bytes there are I still get 0 as the length. I am monitoring it on another device and it has the length correct on it.

CAN allows zero to eight data bytes. The length field DLC specifies how many bytes follow, which can be zero.

Any possible ideas of why this maybe happening?

@ culigan - To try to narrow down the cause, can you create a small program that just receives the messages and checks to see what the length is?

I will try to get that done this morning and let you know the results.

This is just can.

using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Touch;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using GHI.IO;

namespace GadgeteerApp3
{
public partial class Program
{
ControllerAreaNetwork can;

    void ProgramStarted()
    {            
        Debug.Print("Program Started");
        can = new ControllerAreaNetwork(ControllerAreaNetwork.Channel.One, ControllerAreaNetwork.Speed.Kbps500);
        can.ErrorReceived += can_ErrorReceived;
        can.MessageAvailable += can_MessageAvailable;
        can.Enabled = true;        
    }
    private static void can_MessageAvailable(ControllerAreaNetwork sender, ControllerAreaNetwork.MessageAvailableEventArgs e)
    {
        var received = sender.ReadMessage();

        var data = string.Empty;
        for (int i = 0; i < received.Length; i++)
            data += "0x" + received.Data[i].ToString("x2") + " ";

        Debug.Print("   CAN Message   ");
        Debug.Print("-----------------");
        Debug.Print("    ID: " + received.ArbitrationId.ToString("X"));
        Debug.Print("  Time: " + received.TimeStamp.ToString());
        Debug.Print("   RTR: " + received.IsRemoteTransmissionRequest.ToString());
        Debug.Print("   EID: " + received.IsExtendedId.ToString());
        Debug.Print("Length: " + received.Length.ToString());
        Debug.Print("  Data: " + data);
        Debug.Print("");

    }
    private static void can_ErrorReceived(ControllerAreaNetwork sender, ControllerAreaNetwork.ErrorReceivedEventArgs e)
    {
        Debug.Print("Error on CAN: " + e.Error.ToString());
    }
}

}

@ culigan - And that code still gives a length of 0 when it shouldn’t?

Yes it does.

Where are these messages coming from?

Please use code tags to tag your code so our is readable. Use the button with zeros and ones.

using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Touch;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using GHI.IO;

namespace GadgeteerApp3
{
    public partial class Program
    {
        ControllerAreaNetwork can;
        
        void ProgramStarted()
        {            
            Debug.Print("Program Started");
            can = new ControllerAreaNetwork(ControllerAreaNetwork.Channel.One, ControllerAreaNetwork.Speed.Kbps500);
            can.ErrorReceived += can_ErrorReceived;
            can.MessageAvailable += can_MessageAvailable;
            can.Enabled = true;        
        }
        private static void can_MessageAvailable(ControllerAreaNetwork sender, ControllerAreaNetwork.MessageAvailableEventArgs e)
        {
            var received = sender.ReadMessage();

            var data = string.Empty;
            for (int i = 0; i < received.Length; i++)
                data += "0x" + received.Data[i].ToString("x2") + " ";

            Debug.Print("   CAN Message   ");
            Debug.Print("-----------------");
            Debug.Print("    ID: " + received.ArbitrationId.ToString("X"));
            Debug.Print("  Time: " + received.TimeStamp.ToString());
            Debug.Print("   RTR: " + received.IsRemoteTransmissionRequest.ToString());
            Debug.Print("   EID: " + received.IsExtendedId.ToString());
            Debug.Print("Length: " + received.Length.ToString());
            Debug.Print("  Data: " + data);
            Debug.Print("");

        }
        private static void can_ErrorReceived(ControllerAreaNetwork sender, ControllerAreaNetwork.ErrorReceivedEventArgs e)
        {
            Debug.Print("Error on CAN: " + e.Error.ToString());
        }
    }
}

I am sending the messages from a Netway 72 http://www.netwaytools.com/dcs/NETWAY%2072%20DS.pdf. I can also send them from a National Instruments PCI CAN card but I haven’t tried that yet.

What is the exact message you are sending and what exactly are you receiving?

Is it RTR? EID?

4B0 AA AA
7E0 AA AA
5E0 AA AA
5E1 AA AA
I have tried all of these messages with the same result. So I have sent messages with both RTR = 0 and RTR = 1;
Sorry but I don’t know what EID is.

EID is extended identifier. If set to 0 identifier is 11bits while it is 29 bits when it is set to 1.

I haven’t tried sending any messages with extended ID’s