CANID MAPPING

A free API for the communication with control devices according to UDS (ISO 14229-1)
Post Reply
Ken0711
Posts: 21
Joined: Wed 19. Jul 2023, 03:04

CANID MAPPING

Post by Ken0711 » Thu 24. Aug 2023, 05:34

Hi Peak Support,

PCAN-UDS API Version-2.3.0.256

When I used UDSApi.AddMapping_2013 function to mapping CANID(request 0X784,Response 0x 78C) and used the sample (UDS specification page 166 with C# )

and then send the uds diagnostic session control message with function UDSApi.SvcDiagnosticSessionControl_2013(sample from page260)

and after running the code ,initialization and mapping seems successful ,but arose error(PUDS_STATUS_SERVICE_TX_ERROR) when sent the diagnostic session message

Below is my code

Code: Select all

        private void Init_Click(object sender, EventArgs e)
        {
            
            StringBuilder buffer = new StringBuilder(BUFFER_SIZE);
            stsResult = UDSApi.GetValue_2013(cantp_handle.PCANTP_HANDLE_NONEBUS, uds_parameter.PUDS_PARAMETER_API_VERSION, buffer, BUFFER_SIZE);
            MessageBox.Show("PCAN-UDS API Version - " + buffer.ToString());
            stsResult = UDSApi.Initialize_2013(m_clientHandle, m_baudrate, (cantp_hwtype)0, 0, 0);
            if(!UDSApi.StatusIsOk_2013(stsResult))
            {
                MessageBox.Show(UDS_STATUS_OK_KO(stsResult));
            }

            PUDS_SetRequestResponse_CANID();
        }

        private void ReadMsg_Click(object sender, EventArgs e)
        {
            //Thread uds_client = new Thread(() => uds_client_task());
            //uds_client.Start();
            UDS_BOOT_Process1();
        }

        public void PUDS_SetRequestResponse_CANID()
        {
            UInt16 mapping_count;
            uds_mapping[] mapping_buffer = new uds_mapping[BUFFER_SIZE];

            uds_mapping request_mapping = new uds_mapping();
            request_mapping.can_id = 0x784;
            request_mapping.can_id_flow_ctrl = 0x78C;
            request_mapping.can_msgtype = cantp_can_msgtype.PCANTP_CAN_MSGTYPE_STANDARD;
            request_mapping.nai.extension_addr = 0;
            request_mapping.nai.protocol = uds_msgprotocol.PUDS_MSGPROTOCOL_ISO_15765_2_11B_NORMAL;
            request_mapping.can_tx_dlc = 8;
            request_mapping.nai.source_addr =(UInt16)uds_address.PUDS_ADDRESS_ISO_15765_4_ADDR_TEST_EQUIPMENT;
            request_mapping.nai.target_addr = (UInt16)uds_address.PUDS_ADDRESS_ISO_15765_4_ADDR_ECU_1;
            request_mapping.nai.target_type = cantp_isotp_addressing.PCANTP_ISOTP_ADDRESSING_PHYSICAL;
     
            uds_mapping response_mapping;
            response_mapping = request_mapping;
            response_mapping.can_id = request_mapping.can_id_flow_ctrl;
            response_mapping.can_id_flow_ctrl = request_mapping.can_id;
            response_mapping.nai.source_addr = request_mapping.nai.target_addr;
            response_mapping.nai.target_addr = request_mapping.nai.source_addr;

            mapping_count = 0;


            uds_status status;
            status = UDSApi.AddMapping_2013(m_clientHandle, ref request_mapping);
            if (UDSApi.StatusIsOk_2013(status))
            {
                MessageBox.Show("Add request mapping", "Success");               
            }
            else
            {
                MessageBox.Show("Failed to add request mapping", "Error");                
            }                
            status = UDSApi.AddMapping_2013(cantp_handle.PCANTP_HANDLE_USBBUS1, ref response_mapping);
            if (UDSApi.StatusIsOk_2013(status))
            {
                MessageBox.Show("Add response mapping", "Success");
            }              
            else
            {
                MessageBox.Show("Failed to add response mapping", "Error");
            }


            status = UDSApi.GetMappings_2013(m_clientHandle, mapping_buffer, (UInt16)BUFFER_SIZE, ref mapping_count);

            bool mapp_sur= mapping_list_contains(ref mapping_buffer, mapping_count, ref request_mapping);
            bool mapp_res= mapping_list_contains(ref mapping_buffer, mapping_count, ref response_mapping);
        }

        private void UDS_BOOT_Process1()
        {
            uds_status result;
            uds_msg request = new uds_msg();
            uds_msg request_confirmation = new uds_msg();
            uds_msg response = new uds_msg();
            uds_msgconfig config = new uds_msgconfig();
            // Set request message configuration
                     
            config.can_msgtype = cantp_can_msgtype.PCANTP_CAN_MSGTYPE_STANDARD;
            config.nai.extension_addr = 0x0;
            config.nai.protocol = uds_msgprotocol.PUDS_MSGPROTOCOL_ISO_15765_2_11B_NORMAL;
            config.nai.source_addr = (UInt16)uds_address.PUDS_ADDRESS_ISO_15765_4_ADDR_TEST_EQUIPMENT;
            config.nai.target_addr = (UInt16)uds_address.PUDS_ADDRESS_ISO_15765_4_ADDR_ECU_1;
            config.nai.target_type = cantp_isotp_addressing.PCANTP_ISOTP_ADDRESSING_PHYSICAL;
            //config.type = uds_msgtype.PUDS_MSGTYPE_USDT;
            // Sends a physical DiagnosticSessionControl message
            result = UDSApi.SvcDiagnosticSessionControl_2013(m_clientHandle, config,
            out request, UDSApi.uds_svc_param_dsc.PUDS_SVC_PARAM_DSC_ECUPS);
            if (UDSApi.StatusIsOk_2013(result))
                result = UDSApi.WaitForService_2013(cantp_handle.PCANTP_HANDLE_USBBUS1, ref request, out
                response, out request_confirmation);
            if (UDSApi.StatusIsOk_2013(result))
                MessageBox.Show("Response was received", "Success");
            else
                // An error occurred
                MessageBox.Show("An error occurred", "Error");
            // Free structures
            UDSApi.MsgFree_2013(ref request);
            UDSApi.MsgFree_2013(ref response);
            UDSApi.MsgFree_2013(ref request_confirmation);
        }

        static bool mapping_list_contains(ref uds_mapping[] mapping_list, UInt16 mapping_list_size, ref uds_mapping searched_mapping)
        {
            bool res = false;
            for (int i = 0; i < mapping_list_size; i++)
            {

                // If unique identifier are the same, the mapping is in the list
                if (mapping_list[i].uid == searched_mapping.uid)
                {
                    res = true;
                    break;
                }
            }
            return res;
        }
so what's the problem caused the error ?

thanks !
Last edited by K.Wagner on Thu 24. Aug 2023, 08:38, edited 1 time in total.
Reason: Code formatting for better reading

K.Wagner
Software Development
Software Development
Posts: 1082
Joined: Wed 22. Sep 2010, 13:36

Re: CANID MAPPING

Post by K.Wagner » Thu 24. Aug 2023, 09:59

Hello,

your code seems to be OK. Just be careful, that within the function PUDS_SetRequestResponse_CANID you are using a fix channel value in the second call to AddMapping_2013:

Code: Select all

            status = UDSApi.AddMapping_2013(m_clientHandle, ref request_mapping);
            if (UDSApi.StatusIsOk_2013(status))
            {
                MessageBox.Show("Add request mapping", "Success");               
            }
            else
            {
                MessageBox.Show("Failed to add request mapping", "Error");                
            }                
            status = UDSApi.AddMapping_2013(cantp_handle.PCANTP_HANDLE_USBBUS1, ref response_mapping);

Probably the problem is that are not receiving any response. The service timeouts because of this and you get a TX error. Please make sure yourself, that the ECU is configured to listend and response using the CAN-IDs you are confiuring. If you are emulating an ECU, for instance, using the sample 05_server_simulator, you need to configurate the mappings for it too, otherwise all messages will be ignored.
Best regards,
Keneth

Ken0711
Posts: 21
Joined: Wed 19. Jul 2023, 03:04

Re: CANID MAPPING

Post by Ken0711 » Mon 13. Nov 2023, 13:20

Hello ,PCAN Support,

About the CANID Mapping ,i met a new issue, when i used the differented CANID ,and Like this CANID( 784,78C),the MAPPING is Ok,udsstatus feedback PUDS_ERROR_OK

Code: Select all

 		 
            TPUDSStatus result;
            TPUDSMsg mMessage = new TPUDSMsg();
            int mMessageSize;
            IntPtr mMessagePtr;
            mMessage.NETADDRINFO.SA= (byte)TPUDSAddress.PUDS_ISO_15765_4_ADDR_TEST_EQUIPMENT;
            mMessage.NETADDRINFO.TA = (byte)TPUDSAddress.PUDS_ISO_15765_4_ADDR_ECU_1;
            mMessage.NETADDRINFO.TA_TYPE = TPUDSAddressingType.PUDS_ADDRESSING_PHYSICAL;
            mMessage.NETADDRINFO.RA = 0x00;
            mMessage.NETADDRINFO.PROTOCOL = TPUDSProtocol.PUDS_PROTOCOL_ISO_15765_2_11B;
            mMessage.LEN = 8;
            mMessage.DATA = new byte[4095];
             mMessage.DATA[0] = (byte)((0x784 >> 24) & 0xFF);
            mMessage.DATA[1] = (byte)((0x784 >> 16) & 0xFF);
            mMessage.DATA[2] = (byte)((0x784 >> 8) & 0xFF);
            mMessage.DATA[3] = (byte)((0x784) & 0xFF);
            mMessage.DATA[4] = (byte)((0x78C >> 24) & 0xFF);
            mMessage.DATA[5] = (byte)((0x78C >> 16) & 0xFF);
            mMessage.DATA[6] = (byte)((0x78C >> 8) & 0xFF);
            mMessage.DATA[7] = (byte)((0x78C) & 0xFF);

            mMessageSize = Marshal.SizeOf(mMessage);
            mMessagePtr = Marshal.AllocHGlobal(mMessageSize);
            Marshal.StructureToPtr(mMessage, mMessagePtr, false);
            result = UDSApi.SetValue(UDSApi.PUDS_USBBUS1, TPUDSParameter.PUDS_PARAM_MAPPING_ADD, mMessagePtr, (uint)mMessageSize);
            Marshal.FreeHGlobal(mMessagePtr);

            if (result != TPUDSStatus.PUDS_ERROR_OK)
                return false;
            else
                return true;

But when i mapped with the CANID(7E4,7EC),it would be mapping failure(PUDS_ERROR_ALREADY_INITIALIZED)

Code: Select all

   	   mMessage.DATA[0] = (byte)((0x7E4 >> 24) & 0xFF);
            mMessage.DATA[1] = (byte)((0x7E4 >> 16) & 0xFF);
            mMessage.DATA[2] = (byte)((0x7E4 >> 8) & 0xFF);
            mMessage.DATA[3] = (byte)((0x7E4) & 0xFF);
            mMessage.DATA[4] = (byte)((0x7EC >> 24) & 0xFF);
            mMessage.DATA[5] = (byte)((0x7EC >> 16) & 0xFF);
            mMessage.DATA[6] = (byte)((0x7EC >> 8) & 0xFF);
            mMessage.DATA[7] = (byte)((0x7EC) & 0xFF);

I used the same code just different CANID ,what's the problem ?

please check!

Thanks!
Last edited by K.Wagner on Mon 13. Nov 2023, 13:43, edited 1 time in total.
Reason: Text formatted for better reading

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

Re: CANID MAPPING

Post by F.Vergnaud » Mon 13. Nov 2023, 14:00

Hello,

You can't use the same network address information on different CAN IDs: it would mean there are several ways to communicate with the same ECU but the API has no way to identify how to select which one.
In your case you should change change the Target Address (mMessage.NETADDRINFO.TA).

Please also note that 0x7E4/0x7EC is a legislated OBD pair of CAN ID to communicate with ECU #5 (PUDS_ISO_15765_4_ADDR_ECU_5), these CAN IDs are defined in the API as:
- PUDS_ISO_15765_4_CAN_ID_PHYSICAL_REQUEST_5
- and PUDS_ISO_15765_4_CAN_ID_PHYSICAL_RESP0NSE_5.
This mapping is configured by default by the API, see chapter 4.3 "UDS and ISO-TP Network Addressing Information" in the UDS documentation.
Best regards,
Fabrice

Ken0711
Posts: 21
Joined: Wed 19. Jul 2023, 03:04

Re: CANID MAPPING

Post by Ken0711 » Tue 14. Nov 2023, 04:42

Hi Vergnaud,

Thanks your feedback !

i tried your advise and checked it ,it also was PUDS_ERROR_ALREADY_INITIALIZED

and i also read the document ,4.3

below is my code !please check !

Code: Select all

  static void Main(string[] args)
        {
            if (InitUDS_Device() == true)
            {
                PUDS_SetRequest_CANID();
                PUDS_SetResponse_CANID();
            }
       

        bool InitUDS_Device()
        {
            TPUDSStatus result_uds;
            try
            {
                result_uds = UDSApi.Initialize(UDSApi.PUDS_USBBUS1, TPUDSBaudrate.PUDS_BAUD_500K);
                if (TPUDSStatus.PUDS_ERROR_OK != result_uds)
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
            return true;
        }

        bool PUDS_SetRequest_CANID()
        {
            TPUDSStatus result;
            TPUDSMsg mMessage = new TPUDSMsg();
            int mMessageSize;
            IntPtr mMessagePtr;

            mMessage.NETADDRINFO.SA = (byte)TPUDSAddress.PUDS_ISO_15765_4_ADDR_TEST_EQUIPMENT;
            mMessage.NETADDRINFO.TA = (byte)TPUDSAddress.PUDS_ISO_15765_4_ADDR_ECU_5;
            mMessage.NETADDRINFO.TA_TYPE = TPUDSAddressingType.PUDS_ADDRESSING_PHYSICAL;
            mMessage.NETADDRINFO.RA = 0x00;
            mMessage.NETADDRINFO.PROTOCOL = TPUDSProtocol.PUDS_PROTOCOL_ISO_15765_2_11B;
            mMessage.LEN = 8;
            mMessage.DATA = new byte[4095];
            mMessage.DATA[0] = (byte)((0x7E4 >> 24) & 0xFF);
            mMessage.DATA[1] = (byte)((0x7E4 >> 16) & 0xFF);
            mMessage.DATA[2] = (byte)((0x7E4 >> 8) & 0xFF);
            mMessage.DATA[3] = (byte)((0x7E4) & 0xFF);
            mMessage.DATA[4] = (byte)((0x7EC >> 24) & 0xFF);
            mMessage.DATA[5] = (byte)((0x7EC >> 16) & 0xFF);
            mMessage.DATA[6] = (byte)((0x7EC >> 8) & 0xFF);
            mMessage.DATA[7] = (byte)((0x7EC) & 0xFF);

            mMessageSize = Marshal.SizeOf(mMessage);
            mMessagePtr = Marshal.AllocHGlobal(mMessageSize);
            Marshal.StructureToPtr(mMessage, mMessagePtr, false);
            result = UDSApi.SetValue(UDSApi.PUDS_USBBUS1, TPUDSParameter.PUDS_PARAM_MAPPING_ADD, mMessagePtr, (uint)mMessageSize);
            Marshal.FreeHGlobal(mMessagePtr);

            if (result != TPUDSStatus.PUDS_ERROR_OK)
                return false;
            else
                return true;
        }

        bool PUDS_SetResponse_CANID()
        {
            TPUDSStatus result;
            TPUDSMsg mMessage = new TPUDSMsg();
            int mMessgeSize;
            IntPtr mMessagePtr;

            mMessage.NETADDRINFO.SA = (byte)TPUDSAddress.PUDS_ISO_15765_4_ADDR_ECU_5;
            mMessage.NETADDRINFO.TA = (byte)TPUDSAddress.PUDS_ISO_15765_4_ADDR_TEST_EQUIPMENT;
            //mMessage.NETADDRINFO.TA = (byte)0x09;
            mMessage.NETADDRINFO.TA_TYPE = TPUDSAddressingType.PUDS_ADDRESSING_PHYSICAL;
            mMessage.NETADDRINFO.RA = 0x00;
            mMessage.NETADDRINFO.PROTOCOL = TPUDSProtocol.PUDS_PROTOCOL_ISO_15765_2_11B;
            mMessage.LEN = 8;
            mMessage.DATA = new byte[4095];
            mMessage.DATA[0] = (byte)((0x7E8 >> 24) & 0xFF);
            mMessage.DATA[1] = (byte)((0x7E8 >> 16) & 0xFF);
            mMessage.DATA[2] = (byte)((0x7E8 >> 8) & 0xFF);
            mMessage.DATA[3] = (byte)((0x7E8) & 0xFF);
            mMessage.DATA[4] = (byte)((0x7EC >> 24) & 0xFF);
            mMessage.DATA[5] = (byte)((0x7EC >> 16) & 0xFF);
            mMessage.DATA[6] = (byte)((0x7EC >> 8) & 0xFF);
            mMessage.DATA[7] = (byte)((0x7EC) & 0xFF);
            mMessgeSize = Marshal.SizeOf(mMessage);
            mMessagePtr = Marshal.AllocHGlobal(mMessgeSize);
            Marshal.StructureToPtr(mMessage, mMessagePtr, false);
            result = UDSApi.SetValue(UDSApi.PUDS_USBBUS1, TPUDSParameter.PUDS_PARAM_MAPPING_ADD, mMessagePtr, (uint)mMessgeSize);
            Marshal.FreeHGlobal(mMessagePtr);
            if (result != TPUDSStatus.PUDS_ERROR_OK)
                return false;
            else
                return true;
        }
        }
    }

thanks!

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

Re: CANID MAPPING

Post by F.Vergnaud » Tue 14. Nov 2023, 09:42

As mentioned previously the mapping 0x7E4/0x7EC is already configured by default by the API, this is why you get status PUDS_ERROR_ALREADY_INITIALIZED.
You can start sending UDS request with the corresponding NETADDRINFO.

Please note, you are using the old C# header "PCAN-UDS_2006.cs", it is strongly advised for new projects to use PCAN-UDS_2013.cs.
For instance, in your case you would get a more precise status: PUDS_STATUS_MAPPING_ALREADY_INITIALIZED.
Best regards,
Fabrice

Ken0711
Posts: 21
Joined: Wed 19. Jul 2023, 03:04

Re: CANID MAPPING

Post by Ken0711 » Tue 14. Nov 2023, 10:01

Good morning Vergnaud

This measn it was not necessary to mapping the canid again ,and i can directly used it to send the UDS service ?

PUDS_ISO_15765_4_CAN_ID_PHYSICAL_RESPONSE_5 = 0x7EC,
/// <summary>
/// physical request CAN ID from external test equipment to ECU #6
/// </summary>
PUDS_ISO_15765_4_CAN_ID_PHYSICAL_REQUEST_6 = 0x7E5

as below this is not necessary

mMessage.NETADDRINFO.SA = (byte)TPUDSAddress.PUDS_ISO_15765_4_ADDR_TEST_EQUIPMENT;
mMessage.NETADDRINFO.TA = (byte)TPUDSAddress.PUDS_ISO_15765_4_ADDR_ECU_5;
mMessage.NETADDRINFO.TA_TYPE = TPUDSAddressingType.PUDS_ADDRESSING_PHYSICAL;
mMessage.NETADDRINFO.RA = 0x00;
mMessage.NETADDRINFO.PROTOCOL = TPUDSProtocol.PUDS_PROTOCOL_ISO_15765_2_11B;

thanks!

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

Re: CANID MAPPING

Post by F.Vergnaud » Tue 14. Nov 2023, 10:08

Yes: do not add the mapping it is already added when the channel is initialized. You can for instance service ECUReset like so:

Code: Select all

            TPUDSMsg mMessage = new TPUDSMsg();
            mMessage.NETADDRINFO.SA = (byte)TPUDSAddress.PUDS_ISO_15765_4_ADDR_TEST_EQUIPMENT;
            mMessage.NETADDRINFO.TA = (byte)TPUDSAddress.PUDS_ISO_15765_4_ADDR_ECU_5;
            mMessage.NETADDRINFO.TA_TYPE = TPUDSAddressingType.PUDS_ADDRESSING_PHYSICAL;
            mMessage.NETADDRINFO.RA = 0x00;
            mMessage.NETADDRINFO.PROTOCOL = TPUDSProtocol.PUDS_PROTOCOL_ISO_15765_2_11B;

            TPUDSStatus result_uds = UDSApi.SvcECUReset(UDSApi.PUDS_USBBUS1, ref mMessage, UDSApi.TPUDSSvcParamER.PUDS_SVC_PARAM_ER_HR);
Best regards,
Fabrice

Ken0711
Posts: 21
Joined: Wed 19. Jul 2023, 03:04

Re: CANID MAPPING

Post by Ken0711 » Tue 14. Nov 2023, 10:59

Hi,Vergnaud

Thanks For your support !

I got it and solved it !

you can close tht topic !

Thanks again!

Post Reply