Using ISO TP send and recive messages in one PCAN
Posted: Mon 1. Aug 2022, 07:05
Hi
We want to try to use one of PCAN to complete the send and recive of ISOTP(API from PCAN-ISO-TP_2016.cs), so we called the following C# function, we can send the first message and recive the feedback message, but when we call the 2nd MsgDataInit_2016, it will be failed.
Could you help to point out the cause of the error or where it needs to be corrected? The following is our code flow.
Thx
We want to try to use one of PCAN to complete the send and recive of ISOTP(API from PCAN-ISO-TP_2016.cs), so we called the following C# function, we can send the first message and recive the feedback message, but when we call the 2nd MsgDataInit_2016, it will be failed.
Could you help to point out the cause of the error or where it needs to be corrected? The following is our code flow.
Thx
Code: Select all
// Initialize channels: CAN2.0 - 500Kbit/s
res = CanTpApi.Initialize_2016(transmitter_handle, cantp_baudrate.PCANTP_BAUDRATE_500K);
Console.WriteLine("Initialize transmitter : {0}", STATUS_OK_KO(res));
// Allocate tx CAN message
res = CanTpApi.MsgDataAlloc_2016(out tx_msg, cantp_msgtype.PCANTP_MSGTYPE_CAN);
Console.WriteLine("Allocate tx CAN message : {0}", STATUS_OK_KO(res));
// Allocate rx message
res = CanTpApi.MsgDataAlloc_2016(out rx_msg, cantp_msgtype.PCANTP_MSGTYPE_NONE);
Console.WriteLine("Allocate rx message : {0}", STATUS_OK_KO(res));
// Initialize Tx message containing "SartDownload"
uint can_id = 0x700;
byte[] dataSartDownload = { 0x81, 0x27, 0x72, 0xd7, 0x68, 0xfa, 0x81, 0xed };
res = CanTpApi.MsgDataInit_2016(out tx_msg, can_id, cantp_can_msgtype.PCANTP_CAN_MSGTYPE_EXTENDED, 8, dataSartDownload);
Console.WriteLine("Initialize tx message : {0}", STATUS_OK_KO(res));
// Write "dataSartDownload" message
res = CanTpApi.Write_2016(transmitter_handle, ref tx_msg);
Console.WriteLine("Write \"SartDownload\" message : {0}", STATUS_OK_KO(res));
// Free messages space
res = CanTpApi.MsgDataFree_2016(ref tx_msg);
Console.WriteLine("Free tx message : {0}", STATUS_OK_KO(res));
// Uninitialize channels
res = CanTpApi.Uninitialize_2016(transmitter_handle);
Console.WriteLine("Uninitialize transmitter : {0}", STATUS_OK_KO(res));
// Initialize channels: CAN2.0 - 500Kbit/s
res = CanTpApi.Initialize_2016(receiver_handle, cantp_baudrate.PCANTP_BAUDRATE_500K);
Console.WriteLine("Initialize receiver : {0}", STATUS_OK_KO(res));
// Allocate rx message
res = CanTpApi.MsgDataAlloc_2016(out rx_msg, cantp_msgtype.PCANTP_MSGTYPE_NONE);
Console.WriteLine("Allocate rx message : {0}", STATUS_OK_KO(res));
// Create and set a receive event on receiver
System.Threading.AutoResetEvent receive_event = new System.Threading.AutoResetEvent(false);
if (IntPtr.Size == 4)
{
UInt32 iBuffer = Convert.ToUInt32(receive_event.SafeWaitHandle.DangerousGetHandle().ToInt32());
res = CanTpApi.SetValue_2016(transmitter_handle, cantp_parameter.PCANTP_PARAMETER_RECEIVE_EVENT, ref iBuffer, sizeof(UInt32));
}
else if (IntPtr.Size == 8)
{
Int64 iBuffer = receive_event.SafeWaitHandle.DangerousGetHandle().ToInt64();
byte[] byteArray = BitConverter.GetBytes(iBuffer);
res = CanTpApi.SetValue_2016(receiver_handle, cantp_parameter.PCANTP_PARAMETER_RECEIVE_EVENT, byteArray, sizeof(UInt64));
}
Console.WriteLine("Set receive event on receiver : {0}", STATUS_OK_KO(res));
// Wait a receive event on receiver
bool wait_result = receive_event.WaitOne(100000);
Console.WriteLine("Wait a message on receiver : {0}", OK_KO(wait_result));
// If we receive something on the receiver, read the message
if (wait_result)
{
res = CanTpApi.Read_2016(receiver_handle, out rx_msg);
Console.WriteLine("Read message on receiver : {0}", ByteArrayToString(rx_msg.Msgdata_can_Copy.data_max));
String valSartDownload = "0000000000000000";
var rx_result = ByteArrayToString(rx_msg.Msgdata_can_Copy.data_max);
byte val = 0;
if (CanTpApi.getData_2016(ref rx_msg, 0, out val))
Console.WriteLine("Check if the message is \"SartDownload\" : {0}", OK_KO(ByteArrayToString(rx_msg.Msgdata_can_Copy.data_max) == valSartDownload));
else
Console.WriteLine("Check if the message is \"SartDownload\" : NOK");
}
// Free messages space
res = CanTpApi.MsgDataFree_2016(ref rx_msg);
Console.WriteLine("Free rx message : {0}", STATUS_OK_KO(res));
// Uninitialize channels
res = CanTpApi.Uninitialize_2016(receiver_handle);
Console.WriteLine("Uninitialize receiver : {0}", STATUS_OK_KO(res));
// Free messages space
res = CanTpApi.MsgDataFree_2016(ref tx_msg);
Console.WriteLine("Free tx message : {0}", STATUS_OK_KO(res));
// Uninitialize channels
res = CanTpApi.Uninitialize_2016(transmitter_handle);
Console.WriteLine("Uninitialize transmitter : {0}", STATUS_OK_KO(res));
// Initialize channels: CAN2.0 - 500Kbit/s
res = CanTpApi.Initialize_2016(transmitter_handle, cantp_baudrate.PCANTP_BAUDRATE_500K);
Console.WriteLine("Initialize transmitter : {0}", STATUS_OK_KO(res));
// Allocate tx CAN message
res = CanTpApi.MsgDataAlloc_2016(out tx_msg, cantp_msgtype.PCANTP_MSGTYPE_CAN);
Console.WriteLine("Allocate tx CAN message : {0}", STATUS_OK_KO(res));
// Allocate rx message
res = CanTpApi.MsgDataAlloc_2016(out rx_msg, cantp_msgtype.PCANTP_MSGTYPE_NONE);
Console.WriteLine("Allocate rx message : {0}", STATUS_OK_KO(res));
uint can_id = 0x700;
byte[] dataSartDownload = { 0x81, 0x27, 0x72, 0xd7, 0x68, 0xfa, 0x81, 0xed };
---------// the 2nd MsgDataInit will fail-------------------------
res = CanTpApi.MsgDataInit_2016(out tx_msg, can_id, cantp_can_msgtype.PCANTP_CAN_MSGTYPE_EXTENDED, 8, dataSartDownload);
Console.WriteLine("Initialize tx message : {0}", STATUS_OK_KO(res));
// Write "dataSart2nd" message
res = CanTpApi.Write_2016(transmitter_handle, ref tx_msg);
Console.WriteLine("Write \"SartDownload\" message : {0}", STATUS_OK_KO(res));