Removing CAN-clients on termination

CAN Development Packages for Windows®
Post Reply
K.Goe
Posts: 3
Joined: Mon 23. Mar 2015, 16:01

Removing CAN-clients on termination

Post by K.Goe » Mon 23. Mar 2015, 16:34

Hello!

I am developing a program in C using the PCAN-Developer, beginning at which a number of PCAN-clients are being registered. The program is being used for debugging purposes, which means it may be quite frequently started.

So what I'd like to do is call a routine to remove/kill all registered clients on termination of that program.
However, when disconnecting and removing the clients using atexit(DisconnectAll) I receive an CAN_ERR_NODRIVER. This doesn't occur if DisconnectAll is called during runtime.

Now I've read here, regarding PCAN-Basic:
http://www.peak-system.com/forum/viewto ... =41&t=1226
PCAN-Basic just know when applications attached to it terminates (normal, or abnormal) and closes all opened channels. So atexit calls Uninitialize on an uninitialized channel ...
CanApi2 doesn't contain CAN_Uninitialize, but I'm still wondering whether the same applies to the PCAN-Developer and if so, is there a way to work around this?

Thanks & best regards,
K.Goe

K.Wagner
Software Development
Software Development
Posts: 1080
Joined: Wed 22. Sep 2010, 13:36

Re: Removing CAN-clients on termination

Post by K.Wagner » Tue 24. Mar 2015, 09:15

Hello,
K.Goe wrote:CanApi2 doesn't contain CAN_Uninitialize,

Unfortunatelly there is no such a function within the CanApi.
K.Goe wrote:However, when disconnecting and removing the clients using atexit(DisconnectAll) I receive an CAN_ERR_NODRIVER. This doesn't occur if DisconnectAll is called during runtime.
Please look into your code for a call of "CAN_SetDeviceName". This function sets your environment to use a PCAN driver. The "no driver" error normally appears when you are trying to use a CanApi function without setting this, so that the default driver is one that is not present on your system. You can set the default using the Peak Applet (Control Panel, German):
PEAK-Hardware Applet / Control Panel
PEAK-Hardware Applet / Control Panel
Applet.jpg (58.7 KiB) Viewed 9600 times
K.Goe wrote: is there a way to work around this?
You need to iterate on all client handles of the device being used, and delete only those created by you (eg. save the handles of the created clients). Look for the example "ICRegClients" -->Samples/Initialization. Basically you should do something like this (pseudocode):

Code: Select all

// Set the device needed
//
CAN_SetDeviceName("pcan_usb"); // if using PCAN-USB devices.

// Iterate on YOUR array of created handles
//
for(int i=0; i < AMOUNT_OF_CLIENTS_CREATED; i++)
{
      // Remove each of them using CAN_RemoveClient
      //
      CAN_RemoveClient(MY_CLIENT_LIST[i]);
}
Best regards,
Keneth

K.Goe
Posts: 3
Joined: Mon 23. Mar 2015, 16:01

Re: Removing CAN-clients on termination

Post by K.Goe » Tue 24. Mar 2015, 16:39

Thank you for your quick response.

I'm not calling CAN_SetDeviceName, but the default driver is set to USB within the PEAK Applet.
You need to iterate on all client handles of the device being used, and delete only those created by you (eg. save the handles of the created clients)
That's what I'm doing, and it is working just fine. It just doesn't work when called during termination using atexit(...), throwing out a CAN_ERR_NODRIVER.
Since the other Topic, to which I referred to in my first post, states, that PCANBasic closes all opened channels on termination, I'm wondering if the same functionality applies for the CanApi, so that my channels can no longer be addressed when entering my atexit method.
Unfortunatelly there is no such a function within the CanApi.
I know, it is part of PCANBasic. I wonder if PCANBasic is used internally within the CanApi, or if not maybe the same mechanism is being adapted.

Thanks & best regards,
K.Goe

K.Wagner
Software Development
Software Development
Posts: 1080
Joined: Wed 22. Sep 2010, 13:36

Re: Removing CAN-clients on termination

Post by K.Wagner » Wed 25. Mar 2015, 08:52

Hello,

it is something strange that this happens. We have written a test that calls a cleaning-function with atexit and it works as expected.
K.Goer wrote:I know, it is part of PCANBasic. I wonder if PCANBasic is used internally within the CanApi, or if not maybe the same mechanism is being adapted.
PCAN-Basic is based on CanApi2, i.e. SetDeviceName is called with the used device and then all clients are removed, in similar manner as you are doing.
K.Goe wrote:I'm not calling CAN_SetDeviceName, but the default driver is set to USB within the PEAK Applet.
It is always recommended to set at least one time per process the device you want to use. Nevertheless, if you don't use this function and the default (Control Panel) is set to your device, it should work. Could you please call CAN_GetDeviceName within your function and check which device is set when the issue occurs?
Best regards,
Keneth

K.Goe
Posts: 3
Joined: Mon 23. Mar 2015, 16:01

Re: Removing CAN-clients on termination

Post by K.Goe » Wed 25. Mar 2015, 16:58

Thank you again for your support.

According to CAN_GetDeviceName, 'pcan_usb' is set at all time.
I've attached the code that is invoked during termination.

Code: Select all

[...]
	atexit(exitHandler);
[...]

void exitHandler()
{
	DisconnectAll();	// remove PCAN-Clients
}

boolean DisconnectAll()
{
	int no;
	for(no=0;no<CAN_ON_MODULE;no++)
	{
		if (ClientHandle[no] != 0)
		{
			TCANStatus res0 = CAN_DisconnectFromNet(ClientHandle[no], NetHandle[no]);
			TCANStatus res1 = CAN_RemoveClient(ClientHandle[no]);

			char c[MAX_DRIVERNAMELEN+1];
			TCANStatus res2 = CAN_GetDeviceName(c);
			printf("device: %s - getDevice: %d - disconnect: %d - remove: %d\n",c,res2,res0,res1);
		}
	}
	Sleep(10000);

	return true;
} /* DisconnectAll */
I've also included CAN_SetDeviceName at startup, but it makes no difference, as expected.
I've checked the conditions of ClientHandle[no] and NetHandle[no] during termination as well, but their data is correct.

Thanks & best regards,
K.Goe

K.Wagner
Software Development
Software Development
Posts: 1080
Joined: Wed 22. Sep 2010, 13:36

Re: Removing CAN-clients on termination

Post by K.Wagner » Thu 26. Mar 2015, 08:44

Ok,

it seems to be all OK. We need to investigate this deeper. Please send us an Email to support(at)peak.system.com with your license number. We will then contact you directly per email.
Best regards,
Keneth

Post Reply