Having Trouble Sending CAN Messages

A free API for the transfer of data packages according to ISO-TP (ISO 15765-2)
Locked
luliily
Posts: 11
Joined: Sun 22. Jan 2017, 14:13

Having Trouble Sending CAN Messages

Post by luliily » Sat 4. Feb 2017, 07:58

Hello,

I'm an new user of PEAK-CAN.

When I use PCAN-ISO TP API to send a message,which's configurated correctly,and the CANTP_Initialize function returns PCANTP_ERROR_OK.But CANTP_Write function PCANTP_ERROR_NOT_INITIALIZED!I'm so confused here.

Thasnk you
Best Wishes~

Code: Select all

void CUDSDlg::OnBnClickedButtonBoschTp()
{
	TPCANTPMsg MsgTP_READ;
	TPCANTPMsg MsgTP_WRITE;
	TPCANTPStatus result;

	MsgTP_WRITE.SA = 0xFA;
	MsgTP_WRITE.TA = 0x00;
	MsgTP_WRITE.TA_TYPE = PCANTP_ADDRESSING_PHYSICAL;
	MsgTP_WRITE.RA = 0x00;
	MsgTP_WRITE.IDTYPE = PCANTP_ID_CAN_29BIT;
	MsgTP_WRITE.FORMAT = PCANTP_FORMAT_NORMAL;

	MsgTP_WRITE.LEN = 0x02;
	MsgTP_WRITE.DATA[0] = 0x10;
	MsgTP_WRITE.DATA[1] = 0x81;

	MsgTP_WRITE.MSGTYPE = PCANTP_MESSAGE_DIAGNOSTIC;


	result = CANTP_Initialize(PCANTP_USBBUS1, PCANTP_BAUD_250K);//Degug Point A: result == 0x00;
	if (result != PCANTP_ERROR_OK)
	{
		MessageBox(L"Initialize Fail.");
		return;
	}

	result = CANTP_Write(PCANTP_USBBUS1, &MsgTP_WRITE);//Degug Point B: result == 0x01; It is contradict with Degug Point A.
	if (result != PCANTP_ERROR_OK)
	{
		CString str;
		str.Format(L"Error Code = %d", result);
		str += L",Send Failure.";
		MessageBox(str);
		PostMessage(WM_CLOSE);
	}
}

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

Re: Having Trouble Sending CAN Messages

Post by F.Vergnaud » Mon 6. Feb 2017, 09:48

Hello Luliily,

The message you are trying to send requires a mapping (29-bits with Normal addressing format). When you try to transmit the message, the API fails to find it stating that the mapping is not initialized (indeed the error code could be clearer).
You either need to use FIXED NORMAL addressing format for your message (as no mapping is required with this format) or add the corresponding mapping using CANTP_AddMapping function.
Best regards,
Fabrice

luliily
Posts: 11
Joined: Sun 22. Jan 2017, 14:13

Re: Having Trouble Sending CAN Messages

Post by luliily » Wed 1. Mar 2017, 10:17

I add the function CANTP_AddMapping which returns code 0.And the function CANTP_Write returns code 0,too.

But the CAN message that the API acctually send is quite weird.Is there something wrong?
20170301164926.png
20170301164926.png (27.51 KiB) Viewed 3748 times
Another question,when the program runs to function CANTP_Uninitialize,it means CANTP_Read receives message,right?But the data stored in MsgTP_READ doesn't match the actual message.
20170301170340.png
20170301170340.png (23.81 KiB) Viewed 3748 times
Here is my code:

Code: Select all

void CUDSDlg::OnBnClickedButtonBoschTp()
{
	TPCANTPMsg MsgTP_READ = {};
	TPCANTPMsg MsgTP_WRITE = {};
	TPCANTPStatus result;

	MsgTP_WRITE.SA = 0xFA;
	MsgTP_WRITE.TA = 0x00;
	MsgTP_WRITE.TA_TYPE = PCANTP_ADDRESSING_PHYSICAL;
	MsgTP_WRITE.RA = 0x00;
	MsgTP_WRITE.IDTYPE = PCANTP_ID_CAN_29BIT;
	MsgTP_WRITE.FORMAT = PCANTP_FORMAT_NORMAL;

	MsgTP_WRITE.LEN = 2;
	MsgTP_WRITE.DATA[0] = 0x10;
	MsgTP_WRITE.DATA[1] = 0x81;

	MsgTP_WRITE.MSGTYPE = PCANTP_MESSAGE_DIAGNOSTIC;

	result = CANTP_Initialize(PCANTP_USBBUS1, PCANTP_BAUD_250K);//Degug Point A: result == 0x00;---OK
	if (result != PCANTP_ERROR_OK)
	{
		MessageBox(L"Initialize Fail.");
		return;
	}


	result = CANTP_AddMapping(PCANTP_USBBUS1, 
		0x18DA00FA, 
		0x18DAFA00,
		PCANTP_ID_CAN_29BIT, 
		PCANTP_FORMAT_NORMAL, 
		PCANTP_MESSAGE_DIAGNOSTIC,
		0xFA, 
		0x00, 
		PCANTP_ADDRESSING_PHYSICAL, 
		0x00);

	if (result != PCANTP_ERROR_OK)//New Degug Point C: result == 0x00;---OK
	{
		MessageBox(L"Mapping Fail.");
		return;
	}

	result = CANTP_Write(PCANTP_USBBUS1, &MsgTP_WRITE);//Degug Point B: result == 0x00;---OK---But the CAN message is wrong.
	if (result != PCANTP_ERROR_OK)
	{
		CString str;
		str.Format(L"Error Code = %d", result);
		str += L",Send Failure.";
		MessageBox(str);
		PostMessage(WM_CLOSE);
	}

	BOOL bStop = TRUE;
	while (bStop)
	{
		result = CANTP_Read(PCANTP_USBBUS1, &MsgTP_READ);//It receives wired data,all zero.
		if (result == PCANTP_ERROR_OK)
		{
			bStop = FALSE;
		}
	}

	result = CANTP_Uninitialize(PCANTP_USBBUS1);
	if (result != PCANTP_ERROR_OK)
	{
		MessageBox(L"Uninitialize Fail.");
		return;
	}
}
Thanks for your attention.Best wishes.

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

Re: Having Trouble Sending CAN Messages

Post by F.Vergnaud » Wed 1. Mar 2017, 12:10

First thing I see from the CAN IDs you are using is that you are confusing NORMAL addressing format and FIXED NORMAL addressing format. Fixed Normal addressing format is well defined in the ISO-TP standard: based on the network addressing information (SA, TA, priority, etc.) it will create a specific CAN ID. Since the API will try to decode those specific CAN IDs first when receiving a 29 bits CAN message, it can lead to unforeseen behaviour.

Here is your code simplified with the use of FIXED NORMAL addressing :

Code: Select all

void CUDSDlg::OnBnClickedButtonBoschTp()
{
   TPCANTPMsg MsgTP_READ = {};
   TPCANTPMsg MsgTP_WRITE = {};
   TPCANTPStatus result;

	MsgTP_WRITE.SA = 0xFA;
	MsgTP_WRITE.TA = 0x00;
	MsgTP_WRITE.TA_TYPE = PCANTP_ADDRESSING_PHYSICAL;
	MsgTP_WRITE.RA = 0x00;
	MsgTP_WRITE.IDTYPE = PCANTP_ID_CAN_29BIT;
	MsgTP_WRITE.MSGTYPE = PCANTP_MESSAGE_DIAGNOSTIC;
	MsgTP_WRITE.FORMAT = PCANTP_FORMAT_FIXED_NORMAL;

	MsgTP_WRITE.LEN = 2;
	MsgTP_WRITE.DATA[0] = 0x10;
	MsgTP_WRITE.DATA[1] = 0x81;

   result = CANTP_Initialize(PCANTP_USBBUS1, PCANTP_BAUD_250K);//Degug Point A: result == 0x00;---OK
   if (result != PCANTP_ERROR_OK)
   {
      MessageBox(L"Initialize Fail.");
      return;
   }

   result = CANTP_Write(PCANTP_USBBUS1, &MsgTP_WRITE);//Degug Point B: result == 0x00;---OK---But the CAN message is wrong.
   if (result != PCANTP_ERROR_OK)
   {
      CString str;
      str.Format(L"Error Code = %d", result);
      str += L",Send Failure.";
      MessageBox(str);
      PostMessage(WM_CLOSE);
   }

   BOOL bStop = TRUE;
   while (bStop)
   {
      result = CANTP_Read(PCANTP_USBBUS1, &MsgTP_READ);//It receives wired data,all zero.
      if (result == PCANTP_ERROR_OK)
      {
         bStop = FALSE;
      }
   }

   result = CANTP_Uninitialize(PCANTP_USBBUS1);
   if (result != PCANTP_ERROR_OK)
   {
      MessageBox(L"Uninitialize Fail.");
      return;
   }
}
Best regards,
Fabrice

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

Re: Having Trouble Sending CAN Messages

Post by F.Vergnaud » Wed 1. Mar 2017, 12:15

I forgot to mention that the first message that you will read is the confirmation of your transmission.
You need to check the MSGTYPE value which should be PCANTP_MESSAGE_REQUEST_CONFIRMATION (and also always check the RESULT value to ensure that the transmission was ok).

Then the following message should be the response.
Best regards,
Fabrice

luliily
Posts: 11
Joined: Sun 22. Jan 2017, 14:13

Re: Having Trouble Sending CAN Messages

Post by luliily » Thu 2. Mar 2017, 10:26

I used the code you wrote.But the result is still the same,like yesterday.
Screenshot:
20170302170956.png
20170302170956.png (5.79 KiB) Viewed 3744 times
Code:

Code: Select all

void CUDSDlg::OnBnClickedButtonBoschTp()
{
	TPCANTPMsg MsgTP_READ = {};
	TPCANTPMsg MsgTP_WRITE = {};
	TPCANTPStatus result;

	MsgTP_WRITE.SA = 0xFA;
	MsgTP_WRITE.TA = 0x00;
	MsgTP_WRITE.TA_TYPE = PCANTP_ADDRESSING_PHYSICAL;
	MsgTP_WRITE.RA = 0x00;
	MsgTP_WRITE.IDTYPE = PCANTP_ID_CAN_29BIT;
	MsgTP_WRITE.MSGTYPE = PCANTP_MESSAGE_DIAGNOSTIC;
	MsgTP_WRITE.FORMAT = PCANTP_FORMAT_FIXED_NORMAL;

	MsgTP_WRITE.LEN = 2;
	MsgTP_WRITE.DATA[0] = 0x10;
	MsgTP_WRITE.DATA[1] = 0x81;

	result = CANTP_Initialize(PCANTP_USBBUS1, PCANTP_BAUD_250K);//Degug Point A: result == 0x00;---OK
	if (result != PCANTP_ERROR_OK)
	{
		MessageBox(L"Initialize Fail.");
		return;
	}

	result = CANTP_Write(PCANTP_USBBUS1, &MsgTP_WRITE);//Degug Point B: result == 0x00;---OK---But the CAN message is wrong.
	if (result != PCANTP_ERROR_OK)
	{
		CString str;
		str.Format(L"Error Code = %d", result);
		str += L",Send Failure.";
		MessageBox(str);
		PostMessage(WM_CLOSE);
	}

	BOOL bStop = TRUE;
	while (bStop)
	{
		result = CANTP_Read(PCANTP_USBBUS1, &MsgTP_READ);//---OK
		if (result == PCANTP_ERROR_OK)
		{
			if (MsgTP_READ.MSGTYPE != PCANTP_MESSAGE_REQUEST_CONFIRMATION)//matching the actual message---OK
			{
				bStop = FALSE;
				break;
			}	
		}
	}

	result = CANTP_Uninitialize(PCANTP_USBBUS1);
	if (result != PCANTP_ERROR_OK)
	{
		MessageBox(L"Uninitialize Fail.");
		return;
	}
}

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

Re: Having Trouble Sending CAN Messages

Post by F.Vergnaud » Thu 2. Mar 2017, 11:07

I've copied/pasted your code and validated your code: the message sent on the CAN bus contains the correct data

Code: Select all

ID=0x18DA00FA, Length=8, Data=02 10 81 55 55 55 55 55
Your result shows that the sent message had a length of 1 and the data was 0x00.. as if the content of the variable MsgTP_WRITE was not set correctly. You should check it while debugging.

Otherwise which version of the ISO-TP and PCANBasic API do you use ? Current versions are respectively 2.0.0.63 and 4.1.0.96. You can get ISO-TP version info with the following function :

Code: Select all

	char buf[256];
	CANTP_GetValue(PCANTP_NONEBUS, PCANTP_PARAM_API_VERSION, buf, sizeof(buf));
	printf("ISO-TP API = %s", buf);
Best regards,
Fabrice

luliily
Posts: 11
Joined: Sun 22. Jan 2017, 14:13

Re: Having Trouble Sending CAN Messages

Post by luliily » Mon 6. Mar 2017, 05:28

PROBLEM SOLVED!!THANK YOU!!!

My Basic and TP API Version was outdated.Then I update the TP API and BASIC API to the latest version.The message is finally normal.

Again,thanks for your patience.Thank you very much!

Locked