cant receive frame after a query !

The free CAN Software API (Application Programming Interface) for Windows®
elpimous12
Posts: 59
Joined: Tue 3. Aug 2021, 23:34

cant receive frame after a query !

Post by elpimous12 » Mon 20. Sep 2021, 01:19

Hello, after sending a frame, when i query informations, i receive a frame with 0 0 0....
Is this test code correct ?

Code: Select all

#include <unistd.h>
#include <iostream>
#include <iterator>
#include "PCANBasic.h"
// Pcanbasic params
TPCANMsgFD msg;
TPCANStatus Status;
int HandleReadError(TPCANStatus status);
char strMsg[256];
TPCANBitrateFD BitrateFD = (char*) "f_clock_mhz=80,nom_brp=1,nom_tseg1=50,nom_tseg2=29,nom_sjw=10,data_brp=1,data_tseg1=8,data_tseg2=7,data_sjw=12";
unsigned int moteus_id(0x8002);

bool initialize_port()
{
	Status = CAN_InitializeFD(PCAN_PCIBUS1, BitrateFD); // Bus1 for motor 02 (8002). the 80 byte is needed to ask a query
		    if (Status != PCAN_ERROR_OK)
		    {
		    	// if error message, show it
		    	CAN_GetErrorText(Status, 0, strMsg);
		    	std::cout<<(strMsg); // TODO add a rospy.loginfo or error, later...
				return false;
		    }
			else {return true;}
}

void send()
{
	//send frame to controller
	msg.MSGTYPE = PCAN_MESSAGE_FD;
	msg.DLC = 3; // DLC (data length code, for canFD message) value.  ->  11 is 20 bytes, 12 is 24, 13 is 32, 14 is 48, 15 is 64
	msg.ID = moteus_id;
	msg.DATA[0] = 42;
	msg.DATA[1] = 01;
	msg.DATA[2] = 20;

	Status = CAN_WriteFD(PCAN_PCIBUS1, &msg);
	if (Status != PCAN_ERROR_OK)
	{
		CAN_GetErrorText(Status, 0, strMsg);
		std::cout<<(strMsg);
	}
   	else
	{
		std::cout<<"\nmessage correctly sent     : ";
		std::copy(std::begin(msg.DATA), std::end(msg.DATA), std::ostream_iterator<int>(std::cout, " "));
		// it should respond with 41 01 00 
   	}
}

void receive()
{
	//send frame to controller
	msg.MSGTYPE = PCAN_MESSAGE_FD;
	msg.ID = moteus_id;

	Status = CAN_ReadFD(PCAN_PCIBUS1, &msg, NULL);
	if (Status != PCAN_ERROR_OK)
	{
		CAN_GetErrorText(Status, 0, strMsg);
		std::cout<<(strMsg);
	}
   	else
	{
		std::cout<<"\nmessage correctly received : ";
		std::copy(std::begin(msg.DATA), std::end(msg.DATA), std::ostream_iterator<int>(std::cout, " "));
		std::cout<<"\n";
		// it should respond with 41 01 00 
   	}
}

int main()
{
	initialize_port();
	usleep(50);
	send();
	usleep(50);
	receive();
}
and the terminal return :

Code: Select all

message correctly sent     : 42 1 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
message correctly received : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
on pcanview, I enter ID: (hex) = 8002 it works, and 42 1 20 returns 41 01 00.

Could the problem come from the ID ? I use msg.ID=8002;

Without the recognized 80 byte on id, the controller doesn't send any informations, even if I query it !

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

Re: cant receive frame after a query !

Post by M.Heidemann » Mon 20. Sep 2021, 07:57

Hello,

Just an assumption,
as we do not know the full extend of your application:

You message uses an ID given as Hex,
however the data-byte are given as decimal values:

Code: Select all

//send frame to controller
	msg.MSGTYPE = PCAN_MESSAGE_FD;
	msg.DLC = 3; // DLC (data length code, for canFD message) value.  ->  11 is 20 bytes, 12 is 24, 13 is 32, 14 is 48, 15 is 64
	msg.ID = moteus_id;
	msg.DATA[0] = 42;
	msg.DATA[1] = 01;
	msg.DATA[2] = 20;
In PCAN-View the given values are interpretated as hex.

Furthermore do you not use the "MSG_TYPE_EXTENDED" while
using an extended ID, please see the PCANBasic documentation
regarding this:
Extended_ID.png
Extended_ID.png (29.88 KiB) Viewed 8400 times
You message is both extended and FD, you need to combine the message types.

Are you getting a response if this is corrected?


Also does the correct message seem to be of DLC=64, or
are 0 to be ignored in your application?

Please let us know.

Best Regards

Marvin
---
Marvin Heidemann
PEAK-Support Team

elpimous12
Posts: 59
Joined: Tue 3. Aug 2021, 23:34

Re: cant receive frame after a query !

Post by elpimous12 » Mon 20. Sep 2021, 10:00

Hi Marvin.
I could need until a 64 bytes frame, in canFD mode, so I must use the following combinaison, correct ?
TPCANBitrateFD BitrateFD
msg.MSGTYPE = PCAN_MESSAGE_FD
msg.DLC
msg.ID = 0x1F42

or do I need an extended combinaison and just change msg.MSGTYPE = PCAN_MESSAGE_EXTENDED ?

ps: tested the previous code with all values in hexa, with PCAN_MESSAGE_EXTENDED or PCAN_MESSAGE_FD, but no query values other than 0 0 0....

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

Re: cant receive frame after a query !

Post by M.Heidemann » Mon 20. Sep 2021, 10:30

"Extended" only refers to the type of CAN-ID (11bit /29bit) used,
how many data-bytes there are is idenpendent from that.

You message is both a FD messages and message using an extended ID.

Your code is not correct.

Code: Select all

TPCANBitrateFD BitrateFD
msg.MSGTYPE = PCAN_MESSAGE_FD | PCAN_MESSAGE_EXTENDED
msg.DLC = 64
msg.ID = 0x8002
---
Marvin Heidemann
PEAK-Support Team

elpimous12
Posts: 59
Joined: Tue 3. Aug 2021, 23:34

Re: cant receive frame after a query !

Post by elpimous12 » Mon 20. Sep 2021, 10:39

Oh OK.. I had to mention both!!
Trying..
Thanks

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

Re: cant receive frame after a query !

Post by M.Heidemann » Mon 20. Sep 2021, 12:19

Please, if they are new findings regarding your support case, post them.

How are we supposed to keep track of your edits?

Furthermore:

Can you show us:

a) Connection Dialog PCAN-View
b) Message sent in PCAN-View and response
---
Marvin Heidemann
PEAK-Support Team

elpimous12
Posts: 59
Joined: Tue 3. Aug 2021, 23:34

Re: cant receive frame after a query !

Post by elpimous12 » Tue 21. Sep 2021, 01:27

hi.
"How are we supposed to keep track of your edits?" Humm don't know !! perhaps could I send you a pastebin ? The code is really basic : 1 send, 1 receive. (for process anderstanding)

here is the result (success) over pcanview :
Attachments
IMG_20210921_011735_edit_906903553307448_resized_20210921_012929175.jpg
IMG_20210921_011735_edit_906903553307448_resized_20210921_012929175.jpg (153.69 KiB) Viewed 8368 times
IMG_20210921_011755_edit_906873053225682_resized_20210921_012929272.jpg
IMG_20210921_011755_edit_906873053225682_resized_20210921_012929272.jpg (194.78 KiB) Viewed 8368 times
IMG_20210921_011834_edit_906852027018914_resized_20210921_012929374.jpg
IMG_20210921_011834_edit_906852027018914_resized_20210921_012929374.jpg (210.61 KiB) Viewed 8368 times

elpimous12
Posts: 59
Joined: Tue 3. Aug 2021, 23:34

Re: cant receive frame after a query !

Post by elpimous12 » Tue 21. Sep 2021, 01:31

you can see pcanview with a success rx message
here is the same message coded in a simple code :

Code: Select all


#include <unistd.h>
#include <iostream>
#include <iterator>
#include "PCANBasic.h"
// Pcanbasic params
TPCANMsgFD msg;
TPCANStatus Status;
int HandleReadError(TPCANStatus status);
char strMsg[256];
TPCANBitrateFD BitrateFD = (char*) "f_clock_mhz=80,nom_brp=1,nom_tseg1=50,nom_tseg2=29,nom_sjw=10,data_brp=1,data_tseg1=8,data_tseg2=7,data_sjw=12";
unsigned int moteus_id(0x1F42);

bool initialize_port()
{
	Status = CAN_InitializeFD(PCAN_PCIBUS1, BitrateFD); // Bus1 for motor 02 (8002). the 80 byte is needed to ask a query
		    if (Status != PCAN_ERROR_OK)
		    {
		    	// if error message, show it
		    	CAN_GetErrorText(Status, 0, strMsg);
		    	std::cout<<(strMsg); // TODO add a rospy.loginfo or error, later...
				return false;
		    }
			else {return true;}
}

void send()
{
	msg.MSGTYPE = PCAN_MESSAGE_FD | PCAN_MESSAGE_EXTENDED;
	msg.DLC = 11; // 11 is 20 bytes
	msg.ID = moteus_id;
	msg.DATA[0]  = 0x01; // same tx message than pcanview
	msg.DATA[1]  = 0x00;
	msg.DATA[2]  = 0x0a;
	msg.DATA[3]  = 0x0e;
	msg.DATA[4]  = 0x20;
	msg.DATA[5]  = 0x00;
	msg.DATA[6]  = 0x00;
	msg.DATA[7]  = 0x00;
	msg.DATA[8]  = 0x00;
	msg.DATA[9]  = 0x00;
	msg.DATA[10] = 0x00;
	msg.DATA[11] = 0x00;
	msg.DATA[12] = 0x00;
	msg.DATA[13] = 0x11;
	msg.DATA[14] = 0x00;
	msg.DATA[15] = 0x1f;
	msg.DATA[16] = 0x01;
	msg.DATA[17] = 0x13;
	msg.DATA[18] = 0x0d;

	Status = CAN_WriteFD(PCAN_PCIBUS1, &msg);
	if (Status != PCAN_ERROR_OK)
	{
		CAN_GetErrorText(Status, 0, strMsg);
		std::cout<<(strMsg);
	}
   	else
	{
		std::cout<<"\nmessage correctly sent     : ";
		std::copy(std::begin(msg.DATA), std::end(msg.DATA), std::ostream_iterator<int>(std::cout, " "));
   	}
}


void receive()
{
	msg.MSGTYPE = PCAN_MESSAGE_FD | PCAN_MESSAGE_EXTENDED;
	msg.ID = moteus_id;

	Status = CAN_ReadFD(PCAN_PCIBUS1, &msg, NULL);
	if (Status != PCAN_ERROR_OK)
	{
		CAN_GetErrorText(Status, 0, strMsg);
		std::cout<<(strMsg);
	}
   	else
	{
		// it should respond same rx as in pcanview
		std::cout<<"\nmessage correctly received : ";
		std::copy(std::begin(msg.DATA), std::end(msg.DATA), std::ostream_iterator<int>(std::cout, " "));
		std::cout<<"\n";
   	}
}

int main()
{
	initialize_port();
	usleep(50);
	send(); // send tx message
	usleep(50);
	receive();// receive rx message
}
and the terminal result !! no feeded RX message !

Code: Select all

message correctly sent     : 1 0 10 14 32 0 0 0 0 0 0 0 0 17 0 31 1 19 13 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
message correctly received : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

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

Re: cant receive frame after a query !

Post by M.Heidemann » Tue 21. Sep 2021, 07:51

Hello,

Regarding my comment:
How are we supposed to keep track of your edits?
I was not refering to your code, but you have edited a previous post of yours with new information,
we do not get any notifications regarding this and may miss that new information is available for this request, hence my plea to write a new post instead (For which will be notified).

Now seeing what i assume is the whole code, i see one issue which propably explains
why you do not receive feedback even though your transmission - as far as i can tell ( i dont know the protocol used by your device) - seems to be correct:

You only read once.

The PCANBasic API works with so called queues (FIFO), you only ever read the oldest message
in that queue. If your response came after the very first frame received, you'll never read it.
You can use the status return " PCAN_ERROR_QRCVEMPTY" as a break condition for a while loop to make sure all message in the queue are read.

Code: Select all

TPCANMsgFD msg;
TPCANTimestampFD timestamp;
TPCANStatus result;
char strMsg[256];

do
{
    // Check the receive queue for new messages
    //
    result = CAN_ReadFD(PCAN_USBBUS1,&msg,&timestamp);
    if(result != PCAN_ERROR_QRCVEMPTY)
    {
        // Process the received message
        //
        MessageBox("A message was received");
        ProcessMessage(msg)
    }
    else
    {
        // An error occurred, get a text describing the error and show it
        // and handle the error
        //
        CAN_GetErrorText(result, 0, strMsg);
        MessageBox(strMsg);
        // Here can be decided if the loop has to be  terminated (eg. the bus
        // status is  bus-off)
        //
        HandleReadError(result);
    }
// Try to read a message from the receive queue of the PCAN-USB, Channel 1,
// until the queue is empty
//
}while((result & PCAN_ERROR_QRCVEMPTY) != PCAN_ERROR_QRCVEMPTY);

This way you can ensure you did not miss your expected response in case it is not the very first message on the bus.

I'd recommend to create another instance of TPCANMsgFD, TPCANTimestampFD,
and TPCANStatus for your reads as well, just for good measure.

Another point i want to address:

The sample points used in PCAN-View are very odd (We'd recommed Sample-points of 75% and higher), are these bitrate configurations a result of experimentation or are these given by the manufacturer of your device? (as i remember you were working with robotics?)
Especially with CAN FD the proper bitrate configuration can make or break an application (Errors will lead to delays or even nodes stopping communication), you might want to double check this.

Also your PCANView-communication also uses the bitrate switch, so you have to add another message type "PCAN_MESSAGE_BRS" as well.

Check my suggested fixes and report back to us with your findings.

Best Regards

Marvin
---
Marvin Heidemann
PEAK-Support Team

elpimous12
Posts: 59
Joined: Tue 3. Aug 2021, 23:34

Re: cant receive frame after a query !

Post by elpimous12 » Tue 21. Sep 2021, 08:27

Hoo, what a complete and interesting answer...
Testing today..
Thanks a lot

Post Reply