CAN connection Detection

Hello Fellow Developers!

question, i have a FEZ cerberus board connected to a CANDW module, long story short… i need to “ping” some CAN devices, i would like to know if its possible to detect the network is up before doing it to avoid the system exception give for lack of connection.

i already tried with canDW.ErrorReceived, but i guess its to another kind of errors (transmission ones), because its never thrown when disconnected and i try to send something.

thanks for your time

I do not think there is a way as CAN is not meant to be hot pluggable. The only possibility I can think of is to listen for other messages.

1 Like

@ Gus - Thanks a lot for your answer, i was afraid of this answer, the point of asking is because normally (altough not always) my program stops working when there is a cable failure, and instead of me being able to send error message… the microcontroller simply stops and freezes… wanted to avoid tthat and send a warning or something for the troubleshooting.

t

@ Ingcuervo - it shouldn’t freeze. Which device and what firmware version?

When you are debugging, does it not show what the exception is?

If so, you can put in code to handle the exception with a try and catch block and raise an error.

@ Dave McLaughlin - All the info i get is:
exception of type ‘system.exception’ ocurred in .Hardware.dll (see attached image).

if i try to see further info, i get the: "GHI.Hardware.PDB not included " and asks me if it should try to include the microsoft symbol servers… which i dont think will help :slight_smile:

I try and catch, but it simply doesnt work:

         
  try
           {
               canDW.SendMessage(CanMessage);
           }
           catch (Exception e)
           {
              // Whatever i do here never happens, never enters here, not even breakpoints
               Debug.Print("CAN out error");
           }
  

@ Gus- im using a FEZ Cerberus board with latest sdk (3.4.7.10).

@ andre - yeah, i figured it, but the point is that i was expecting something like a candw.IsConnected; or canDW.IsBusUp;-… (or whatever… i think you get the point), that allowed me to pinpoint a specific part of the code, and not necesarily use a complete program supervisor for only one particular instruction.

Thansk to you all for your time

Can you capture a stack trace and share it? Anybody looking at this will need to know the stack trace to see where the exception is really happening.

When the exception occurs, go down to the Call Stack window, select all, copy, and paste here. If you have trouble getting a useful stack trace, then uncheck “Enable Just My Code” (in Debug->Options and Settings). If you have trouble catching the exception in the debugger, then consider catching first-chance exceptions (Debug->Exceptions and check “Thrown” next to Common Language Runtime Exceptions". You will want to turn both of those off when you are done.

extra comment:

of course the problem only exists when i unplug the CAN connection, if its connected it works perfectly fine

@ mcalsyn - hello, thanks for your reply’; “enable Just my code” is unchecked, and this is all i got in the “call stack window”

[quote]> GHI.Hardware.dll!GHI.IO.ControllerAreaNetwork.NativeSendMessages(GHI.IO.ControllerAreaNetwork.Message[] messages, int offset, int count)
[Managed to Native Transition]
GHI.Hardware.dll!GHI.IO.ControllerAreaNetwork.SendMessages(GHI.IO.ControllerAreaNetwork.Message[] buffer, int offset, int count) + 0x7c bytes
GTM.GHIElectronics.CANDW.dll!Gadgeteer.Modules.GHIElectronics.CANDW.DoWork(GHI.IO.ControllerAreaNetwork.Message[] messages, int offset, int count) + 0x2b bytes
GTM.GHIElectronics.CANDW.dll!Gadgeteer.Modules.GHIElectronics.CANDW.SendMessages.AnonymousMethod__0() + 0x1a bytes
[/quote]

I agree with @ andre.m. You can’t catch this exception because the call stack is not rooted in your code. Once an uncaught exception like this happens, the watchdog is your only way out of the situation, as Andre said.

@ Ingcuervo -

Try to use directly from our example ad add try catch to see if you can catch an exception.

Or you can take a look from here
https://www.ghielectronics.com/community/codeshare/entry/1044
It is for extender msg but standard msg should work.

@ Dat - Hello, i dont really get the point of your answer… i started from example and it works, but the “issue” comes when disconnection occurs (already explained and clear) and the exception is not caught(also already explained and cleared why :D), about the second link, this is the fix for the extended ID issue, i dont see the point.

Anyway thanks for your time :slight_smile:

If the string is pulled tight enough, you should be able to hear any messages. I’ve included a diagram.

3 Likes

On the G120 i use the ErrorReceived event and do a reset on the sender when a ControllerAreaNetwork.Error.BusOff is raised, this works for recovering when data is send and no other devices are connected.

I meant, your code looks like gadgeteer driver, so I wanted to try NETMF directly.
In the second link, that uses Register class, you can modify, check its status and try catch.