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());
}
}
}