I decided to not go with RLP since speed is not one of my requirements; I only need to sample the bus and I am not worried about losing messages.
using System;
using System.Collections;
using System.Threading;
using System.IO.Ports;
using System.Text;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using GHIElectronics.NETMF.Hardware;
namespace CANstuff
{
public partial class Program
{
CAN.Message[] msgList;
int i, j, offset = 0;
// This method is run when the mainboard is powered up or reset.
void ProgramStarted()
{
int T1, T2, BRP;
Debug.EnableGCMessages(true);
BRP = 3;
T1 = 15;
T2 = 8;
// Use channel 1 and initialize module
CAN can = new CAN(CAN.Channel.Channel_1, (uint)(((T2 - 1) << 20) | ((T1 - 1) << 16) | ((BRP - 1) << 0)), 100);
// and set filter
uint[] explicitIDs = new uint[] { 0x5F0 };
can.SetExplicitFilters(explicitIDs);
// create a message list of 65 messages
msgList = new CAN.Message[65];
for (int i = 0; i < msgList.Length; i++)
msgList[i] = new CAN.Message();
// Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
Debug.Print("Program Started");
while (true)
{
try
{
can.GetMessages(msgList, 0, msgList.Length);
}
finally
{
//can.Dispose();
Debug.GC(true);
}
Thread.Sleep(10);
}
}
}
}
The GC doesn’t seem to be releasing msgList ( I believe it is msgList, as this happens when I use: can.GetMessages(msgList, 0, msgList.Length); ), as BINARY_BLOB_HEAD and STACK_FRAME grow uncontrolled (STACK_FRAME grows much faster).
From what I have read, I may need to dispose of the data before using GC (at least that is what I got from reading through the forum and going over “Destructors and Finalizers in Visual C++” on the Visual Studio site), but if I uncomment “can.Dispose();” I get this error:
Exception System.ObjectDisposedException - CLR_E_OBJECT_DISPOSED (1)
#### Message:
#### GHIElectronics.NETMF.Hardware.CAN::GetMessages [IP: 0000] ####
#### CANstuff.Program::ProgramStarted [IP: 0087] ####
#### CANstuff.Program::Main [IP: 0015] ####
A first chance exception of type ‘System.ObjectDisposedException’ occurred in GHIElectronics.NETMF.Hardware.dll
An unhandled exception of type ‘System.ObjectDisposedException’ occurred in GHIElectronics.NETMF.Hardware.dll
Now I know that this means that I am trying to use a resource that has already been disposed, as I receive this error when I reach can.Message for a second time. So how do I release these resources???
As a side note: I am using the cAN_DW module, and I haven’t seen any documentation of whether I should use its methods or not… For example, I could also use: cAN_DW.InitializeCAN((uint)(((T2 - 1) << 20) | ((T1 - 1) << 16) | ((BRP - 1) << 0)), 100);
Thanks everyone!