Problem with Event to receive CAN message (C++)

The free CAN Software API (Application Programming Interface) for Windows®
Locked
Raph27
Posts: 2
Joined: Thu 16. Jul 2020, 13:32

Problem with Event to receive CAN message (C++)

Post by Raph27 » Thu 16. Jul 2020, 13:47

Hi everyone !
I have problem with my application.
I have a wheel which is connected by CAN. This wheel send message at 100Hz.
But when i try to receive messages, i have 5sec of delay.
The message event is in a thread
It's my code :

Code: Select all

int UseEvent() {
	std::cout << "DEMARRAGE UseEvent" << std::endl;
	TPCANStatus status;
	TPCANMsg peakMsg;
	
	//status = CAN_Initialize(PCAN_PCIBUS1, PCAN_BAUD_1M);
	
	HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, L"ReceiveEvent");

	status = CAN_SetValue(PCAN_PCIBUS1, PCAN_RECEIVE_EVENT, &hEvent, sizeof(HANDLE));

		if (status != PCAN_ERROR_OK) {
			char strMsg[256];
			CAN_GetErrorText(status, 0, strMsg);
			printf("%s", strMsg);
			return NULL;
		}
		
		while (1) {
			if (WAIT_OBJECT_0 == WaitForSingleObject(hEvent, INFINITE)) {
				//ResetEvent(hEvent);
				CAN_Read(PCAN_PCIBUS1, &peakMsg, NULL);
				}
		}
		}
Do you have a solution ?

Thanks a lot,
Raphael

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

Re: Problem with Event to receive CAN message (C++)

Post by M.Heidemann » Thu 16. Jul 2020, 14:13

Hello Raphael,

As soon as the device is initialized it is able to receive messages,
if the messages are not read, they will be stored in the buffer.

Currently you read once from the buffer when the event is triggered.

Possibly the message you want to read triggers a read of a previous messages which is still in the buffer.

To prevent this i'd recommend to use the return value TPCANStatus "PCAN_ERROR_QRCVEMPTY" as a break condition for a read loop, so you read all the messages in the buffer until empty, that should prevent this from happening:

Here an example from our PCANBasic C++MFC example:

Code: Select all

void CPCANBasicExampleDlg::ReadMessages()
{
	TPCANStatus stsResult;

	// We read at least one time the queue looking for messages.
	// If a message is found, we look again trying to find more.
	// If the queue is empty or an error occurr, we get out from
	// the dowhile statement.
	//			
	do
	{
		stsResult =  m_IsFD ? ReadMessageFD() : ReadMessage();
        if (stsResult == PCAN_ERROR_ILLOPERATION)
            break;
	} while (btnRelease.IsWindowEnabled() && (!(stsResult & PCAN_ERROR_QRCVEMPTY)));
}
Try to use "PCAN_ERROR_QRCVEMPTY" as a means to empty the buffer with every read and see if that resolves your issues

Bets Regards

Marvin

Raph27
Posts: 2
Joined: Thu 16. Jul 2020, 13:32

Re: Problem with Event to receive CAN message (C++)

Post by Raph27 » Thu 16. Jul 2020, 15:39

Thanks for your quick reply Marvin !

I tried and my problem is resolved !

Thanks a lot !
Raphaël

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

Re: Problem with Event to receive CAN message (C++)

Post by M.Heidemann » Thu 16. Jul 2020, 15:47

Hello Raphael,

Im glad to hear that you could resolve your issue!


Best Regards

Marvin

-Closed-

Locked