ControllerAreaNetwork Crash in 2014 R5

We typically Dispose of our CAN ports when the CANBUS is stopped (ie, vehicle turned off). When attempting to do so with the ControllerAreaNetwork object in 2014 R5 the microcontroller (G120) crashes hard. It disappears from USB device list on my laptop and then several minutes later it reboots itself. the following code is an easy demo of this problem. The LED stops flashing the moment the can.Dispose() method is called.

Can anyone explain? Thanks.


using System;
using Microsoft.SPOT;
using System.Threading;
using Microsoft.SPOT.Hardware;

namespace CAN_TEST
{
    public class Program
    {

        private static GHI.IO.ControllerAreaNetwork can;
        static OutputPort led;

        public static void Main()
        {
            led = new OutputPort(GHI.Pins.G120.P1_15, false);

            new Thread(() =>
                {
                    while (true)
                    {
                        led.Write(!led.Read());
                        Thread.Sleep(250);
                    }
                }).Start();

            can = new GHI.IO.ControllerAreaNetwork(GHI.IO.ControllerAreaNetwork.Channel.One, GHI.IO.ControllerAreaNetwork.Speed.Kbps250);
            can.ErrorReceived += can_ErrorReceived;
            can.Enabled = true;

            Thread.Sleep(10000);

            can.Enabled = false;

            can.Dispose(); ///Problem HERE!

            Thread.Sleep(Timeout.Infinite);
        }

        static void can_ErrorReceived(GHI.IO.ControllerAreaNetwork sender, GHI.IO.ControllerAreaNetwork.ErrorReceivedEventArgs e)
        {
            Debug.Print(e.Error.ToString());
        }

    }
}


Not that I tried it, but what happens if you remove the ErrorReceived event handler before you dispose?

Same failure. BUT if I don’t do

 it works (disposes) fine.

Interesting. Never Dispose a running system, or the other way around, or … What ever works :whistle:

Other question: Why do you dispose it? Are there measurable higher energy consumption?
I’m asking because we use CAN too, but we only set Enable to false while we don’t need it.

We try to strip back to the bare necessities when the communication is lost. Its out of interest in assuring that the system always works the next time the vehicle is switched on. In the early days we really struggled with CAN when a BUSOFF occurred. This fixed it and we’ve stuck with it.

We use a G120 with CAN for car ECU testing. Means that we have Bus off s all the time, when something is not working or disconnected.
So far we never had any problems using 4.3 firmware.

Same thing over here, main communication is alway’s CAN, we just deal and handle the BusOff error’s. But we never disable or dispose the CAN because our wake up instructions are also received on the CAN bus.

@ Diesel Engineer - We were able to reproduce the issue. We should have it fixed for the next SDK.

3 Likes