Page 1 of 1

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

Posted: Thu 16. Jul 2020, 13:47
by Raph27
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

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

Posted: Thu 16. Jul 2020, 14:13
by M.Heidemann
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

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

Posted: Thu 16. Jul 2020, 15:39
by Raph27
Thanks for your quick reply Marvin !

I tried and my problem is resolved !

Thanks a lot !
Raphaël

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

Posted: Thu 16. Jul 2020, 15:47
by M.Heidemann
Hello Raphael,

Im glad to hear that you could resolve your issue!


Best Regards

Marvin

-Closed-