Is there any API to check if the CAN Devies are connected or not ?

This forum covers PCAN-Linux and Linux development issues concerning our products
Post Reply
vijayendranss
Posts: 12
Joined: Thu 10. Sep 2020, 10:25

Is there any API to check if the CAN Devies are connected or not ?

Post by vijayendranss » Thu 10. Sep 2020, 10:55

I use PCAN Driver for Linux v8.

By seeing the manual, i completed till

Code: Select all

sudo modprobe pcan 
When no devices connected to my PC, i execute

Code: Select all

lspcan -T -t -a
No devices are displayed. Till this fine.

In my application i implement like below :

Code: Select all

fd = pcanfd_open(
	st_CanInfoTable[en_channel].dev_name,
	st_CanInfoTable[en_channel].set_flag,
	st_CanInfoTable[en_channel].bitrate
	);
	if (fd > 0){
		pcanfd_set_init(...);
	}
	else{
		printf("pcanfd_open ch=%d ERROR!! errno=%d:%s \n",en_channel, errno, strerror(errno));
	}
When the above is executed, as there are no devices currently connected above ERROR statement will be thrown.
I dont want this to happen.

Library code provided along with software goes like this :

Code: Select all

\peak-linux-driver-8.10.2\lib\src\libpcanfd.c
int pcanfd_open()
{
	.......
	.......
	fd = __open(dev_pcan, o_flags);
	if (fd < 0)
		goto open_exit_1; //return negative fd 
	....
	pcanfd_set_init(...) 
	....
}
I want to modify as below.
  • First i would like to "check if the drivers are succesfully loaded" - Is there any API to check this ?
  • Then "is there any devices connected or not ?" - Is there any APIs for this ?
If no devices are connected i will not call __open function

Code: Select all

int pcanfd_open()
{
	.......
	.......
	//Check if drivers are loaded ?
	//If loaded{
		// Is there any devices connected or not ? 
		// If (Connected) {
			fd = __open(dev_pcan, o_flags);
			if (fd < 0)
				goto open_exit_1; //return negative fd 
			....
			pcanfd_set_init(...) 
			....
		} //Connected
	} //Loaded
}

M.Heidemann
Sales & Support
Sales & Support
Posts: 1083
Joined: Fri 20. Sep 2019, 13:31

Re: Is there any API to check if the CAN Devies are connected or not ?

Post by M.Heidemann » Thu 10. Sep 2020, 11:39

Hello,

There are no specific functions for this in the CAN FD API in the PCAn Linux Driver package, but you can do the following in your code
to get the needed information:

1) Check if the driver is loaded:

You could check if "cat /proc/pcan" returns anything or if /proc/pcan exists, if it does not, the driver is not loaded.

Example:

Driver is not loaded:
DriverNotLoaded.PNG
DriverNotLoaded.PNG (19.6 KiB) Viewed 4572 times
Driver is loaded:
DriverIsLoaded.PNG
DriverIsLoaded.PNG (20.95 KiB) Viewed 4572 times

2) Check if the device is connected:

You could check for the device in /dev, it should contain the string "pcan" in its name, if no device with the substring "pcan" can be found, the device is not connected.

PCAN-USB does not show up in /dev if not connected.

Below you can see, once connect it does show up in /dev:

DeviceConnected.PNG
DeviceConnected.PNG (44.63 KiB) Viewed 4572 times

You can check for those two conditions in your code to determine if the device is connected and the driver is loaded.


Best Regards

Marvin
---
Marvin Heidemann
PEAK-Support Team

vijayendranss
Posts: 12
Joined: Thu 10. Sep 2020, 10:25

Re: Is there any API to check if the CAN Devies are connected or not ?

Post by vijayendranss » Thu 10. Sep 2020, 11:44

thank you very much for looking into this.

It would be helpful if you can share sample code for this.

M.Heidemann
Sales & Support
Sales & Support
Posts: 1083
Joined: Fri 20. Sep 2019, 13:31

Re: Is there any API to check if the CAN Devies are connected or not ?

Post by M.Heidemann » Thu 10. Sep 2020, 11:55

We do not have specific examples for this.

You can take a look at the /test/src directory of the PCAN Linux driver package, it contains the sources for the example applications.

There you can also see how some of these cases are handled, for example if the device cannot be opened:

Here an example from receivetest.c:
OpenDeviceError.PNG
OpenDeviceError.PNG (14.92 KiB) Viewed 4567 times

For further questions feel to contact me again.

Best Regards

Marvin
---
Marvin Heidemann
PEAK-Support Team

vijayendranss
Posts: 12
Joined: Thu 10. Sep 2020, 10:25

Re: Is there any API to check if the CAN Devies are connected or not ?

Post by vijayendranss » Fri 11. Sep 2020, 05:12

@Marvin
Thanks.. My ultimate purpose is mentioned in the below query.. Can you please have look and guide me.

viewtopic.php?f=119&t=6110

Thanks

Post Reply