Page 1 of 1

Scanning available CAN hardware / channels

Posted: Fri 2. Oct 2015, 09:51
by A.Hartmann
Hello,

PCANView has the nice function to show all available hardware interfaces in the connection window.

I'd like to have something similar for my own applications using CanApi2 (we have version 2.56.2). Is this possible and if yes, how?

Best regards,
André Hartmann

Re: Scanning available CAN hardware / channels

Posted: Fri 2. Oct 2015, 10:37
by M.Maidhof
Hi,

you can use a for loop to scan for valid hardware handles on each driver:

Code: Select all

for(i=0;i<=MAX_HCANHW;i++)
  {
   ret=CAN_GetHwParam(i,CAN_PARAM_NAME,buff,sizeof(buff));
   if (ret==CAN_ERR_OK)
    {
    // Hardware found on hardwarehandle i
    // use handle i to get more details of the hardware by CAN_GetHwParam
    }
  }
Change the active device with SetDeviceName, to search other device drivers too (PCI, USB, ISA, DNG etc.).

Please have a look into the canapi2 help file, to get more information how to use those APIs

regards

Michael

Re: Scanning available CAN hardware / channels

Posted: Fri 2. Oct 2015, 12:34
by A.Hartmann
Hi Michael,

thanks a lot for this information.

It allowed me to build a simple example, nothing fancy but working.

Only one note/question: the header file says, only Hardware 1...MAX_HCANHW permitted, so I guess the loop should run from 1?

Code: Select all

for (int i = 1; i <= MAX_HCANHW; i++) {
  // ...
}
Anyway, thanks for your fast help.

Best regards,
André

Re: Scanning available CAN hardware / channels

Posted: Fri 2. Oct 2015, 12:55
by M.Maidhof
Hi,

"0" is equal to an internal hardware, when the pcan-network is not linked to a real PCAN hardware. That is why a hardware handle of 0 is still possible in the canapi2. But you are right, in that context you can start with 1, because 0 will always return ILLHW.

regards

Michael