Until now, the company I'm working with was using PCAN-BASIC and managing the whole ISO-TP on their own which sometimes turns the slow comnunication due to not so efficient frame management.
As an improvement I'm trying to start with ISO-TP API.
To do so I'm trying to bring on of the examples to "our reality", example: 06_isotp_segmented_read_write
The goal was to at least be able to send the message to the bus so I can see it PCAN View, but I could not pull that off.
My current setup is a PCAN USB connected to a ECU -> This works using a PCAN BASIC so hardware should not be a problem.
ECU adresses info, Request to ECU:
-MODE: Extended
-CAN ID: 0x6F1
-ECU: 0x5E
Respondese from ECU:
-MODE: Extended
-CAN ID: 0x65E
-ECU:0xF1
What I'm trying to do is send a Tester Present message and wait for it's response, I have a trace from this:
123.495988 1 6F1 Tx d 8 5E 02 3E 00 00 00 00 00 Length = 236000 BitCount = 121 ID = 1777 // 1 OTP(1D) Atom F1->5E : SF Length: 02 [ 3E 00 ]
123.504712 1 65E Rx d 4 F1 02 7E 00 Length = 157914 BitCount = 83 ID = 1630 // 1 OTP(1E) Atom 5E->F1 : SF Length: 02 [ 7E 00 ]
To try and replicate this I have the following code:
Code: Select all
// Local variables
cantp_status res = new cantp_status();
StringBuilder buffer = new StringBuilder(500);
cantp_msg rx_msg = new cantp_msg();
cantp_msg tx_msg = new cantp_msg();
cantp_handle transmitter_handle = new cantp_handle();
// Initialize handles
transmitter_handle = cantp_handle.PCANTP_HANDLE_USBBUS1;
// Print version informations
CanTpApi.GetValue_2016(cantp_handle.PCANTP_HANDLE_NONEBUS, cantp_parameter.PCANTP_PARAMETER_API_VERSION, buffer, 500);
Console.WriteLine("PCAN-ISO-TP API Version : {0}", buffer);
// 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));
// 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(transmitter_handle, cantp_parameter.PCANTP_PARAMETER_RECEIVE_EVENT, byteArray, sizeof(UInt64));
}
Console.WriteLine("Set receive event on receiver : {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
uint can_id = 0x6F1;
byte[] data = { 0x3E, 0x00 };
res = CanTpApi.MsgDataInit_2016(out tx_msg, can_id, cantp_can_msgtype.PCANTP_CAN_MSGTYPE_EXTENDED, 2, data);
Console.WriteLine("Initialize tx message : {0}", STATUS_OK_KO(res));
res = CanTpApi.Write_2016(transmitter_handle, ref tx_msg);
Console.WriteLine("Write DATA message : {0}", STATUS_OK_KO(res));
// Wait a receive event on receiver
bool wait_result = receive_event.WaitOne(100);
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(transmitter_handle, out rx_msg);
Console.WriteLine("Read message on receiver : {0}", STATUS_OK_KO(res));
byte val = 0;
if (CanTpApi.getData_2016(ref rx_msg, 0, out val))
Console.WriteLine("Check if the message is \"42\" : {0}", OK_KO(val == 0x42));
else
Console.WriteLine("Check if the message is \"42\" : NOK");
}
// Free messages space
res = CanTpApi.MsgDataFree_2016(ref rx_msg);
Console.WriteLine("Free rx message : {0}", STATUS_OK_KO(res));
// Close receive event
if (IntPtr.Size == 4)
{
UInt32 iBuffer = 0;
res = CanTpApi.SetValue_2016(transmitter_handle, cantp_parameter.PCANTP_PARAMETER_RECEIVE_EVENT, ref iBuffer, sizeof(UInt32));
}
else if (IntPtr.Size == 8)
{
Int64 iBuffer = 0;
byte[] byteArray = BitConverter.GetBytes(iBuffer);
res = CanTpApi.SetValue_2016(transmitter_handle, cantp_parameter.PCANTP_PARAMETER_RECEIVE_EVENT, byteArray, sizeof(UInt64));
}
Console.WriteLine("Stop receive event : {0}", STATUS_OK_KO(res));
receive_event.Close();
// Uninitialize channels
res = CanTpApi.Uninitialize_2016(transmitter_handle);
Console.WriteLine("Uninitialize transmitter : {0}", STATUS_OK_KO(res));
// Exit
Console.WriteLine("Press any key to exit...");
Console.In.Read();
Thanks in advance for the time, and I'm looking forward for feedback.