Peak Can Basic Read Write Time over problem

This forum covers PCAN-Linux and Linux development issues concerning our products
Post Reply
aidenkang
Posts: 2
Joined: Sat 11. Feb 2023, 08:35

Peak Can Basic Read Write Time over problem

Post by aidenkang » Sat 11. Feb 2023, 08:49

hello I use pcan basic for mobile robot motor driver control.

When I use pcan using Can_read and Can_write, sometimes this function execution time is more than 3~20ms over.

our using source code is

Code: Select all

int usbPCanDriver::Write(TPCANMsg msg)
{
	std::chrono::system_clock::time_point start_time = std::chrono::system_clock::now();
	TPCANStatus status = CAN_Write(pcan_device_, &msg);
	std::chrono::duration<double> sec = std::chrono::system_clock::now() - start_time;
	if(sec.count()*1000 > 3)
	{
		DBG_ERROR("PCANWRITE: %f ms",sec.count()*1000);
	}
	if (status != PCAN_ERROR_OK)
	{
		DBG_ERROR("Tx Error: %x",status);
	}

	return status;
}

void usbPCanDriver::Read_Loop()
{
	TPCANMsg Message;
	while (1) 
	{
		std::chrono::system_clock::time_point start_time = std::chrono::system_clock::now();
		TPCANStatus status = CAN_Read(pcan_device_, &Message, NULL);
		std::chrono::duration<double> sec = std::chrono::system_clock::now() - start_time;

		if (status == PCAN_ERROR_OK)
		{
        	Notify("RxCANMsg_Callback", Message);
			std::chrono::duration<double> sec2 = std::chrono::system_clock::now() - start_time;

			if(sec.count()*1000 > 3)
			{
				DBG_ERROR("PCANREAD: %f ms",sec.count()*1000);
			}
		}
		std::this_thread::sleep_for(std::chrono::milliseconds(1));
	}
}

imformation about linux pcaninfo
PCAN driver version: 8.15.2
PCAN-Basic version: 4.6.2.36

* pcanusb32 "PCAN_USBBUS1" (0x051), PCAN-USB #1, devid=0x1E (/sys/class/pcan/pcanusb32)

There is only 1 usb for pcan-usb on the PC.

We use this model.
https://ko.aliexpress.com/item/10050028 ... pt=glo2kor

Is there any problem in our codes?

aidenkang
Posts: 2
Joined: Sat 11. Feb 2023, 08:35

Re: Peak Can Basic Read Write Time over problem

Post by aidenkang » Sat 11. Feb 2023, 08:56

We will give you additional information.

If the pcan device is used for more than 24 hours, many of the above timeout errors are logged.

So, if we do Can_uninitialize, Can_reset, and Can_initialize once a day, fewer errors will occur for the time being after using this functions.

However, using pcan in this way has a problem that the motor keeps initializing. We don't want to do motor initialization.

Thank you.

M.Maidhof
Support
Support
Posts: 1652
Joined: Wed 22. Sep 2010, 14:00

Re: Peak Can Basic Read Write Time over problem

Post by M.Maidhof » Mon 13. Feb 2023, 10:22

Hi,

please send us a picture and the serial number of the used PCAN-USB device by email to our support[at]peak-system.com email address, to check that this is an original PEAK device.

regards

Michael

F.Vergnaud
Software Development
Software Development
Posts: 265
Joined: Mon 9. Sep 2013, 12:21

Re: Peak Can Basic Read Write Time over problem

Post by F.Vergnaud » Wed 15. Feb 2023, 11:09

Hello,

Based on your sample code, I guess you're using at least 2 threads:
  • one thread for reading CAN frames, calling usbPCanDriver::Read_Loop(...)
  • another to write CAN frames, calling usbPCanDriver::Write(...).
Reminder: to ensure thread-safety, PCAN-Basic API internally uses locking mechanism. As a result if one thread is executing CAN_Read, the other will have to wait before entering CAN_Write (and vice-versa).

Your function usbPCanDriver::Read_Loop() reads CAN frames using an infinite polling mechanism, with a check every 1 millisecond.
This design creates a concurrency race between this thread and any threads calling PCAN-Basic API functions (note: you can monitor PCAN-Basic API access with parameter PCAN_LOG_CONFIGURE and values LOG_FUNCTION_ENTRY and LOG_FUNCTION_EXIT).
You should consider using event to get notified when a message is available as shown in our C Linux sample "pcaneventhread" or the C++ one "08_EventDrivenRead".
This should definitely improve the performances of your application.

Otherwise to get a more precise analyze of your issue, it would be interesting to know the average bus load of your CAN bus (a trace file with occurences of your issue would be best), and whether CAN_Reset is enough to improve your issue or if you really need to Unitialize/Initialize your channel.
Best regards,
Fabrice

Post Reply