|
| 1 | +# UDS Server Example for RT-Thread |
| 2 | + |
| 3 | +UDS server example for RT-Thread platform. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +This example demonstrates a UDS (ISO 14229) server running on an RT-Thread based embedded system. It uses the RT-Thread CAN device driver for communication. |
| 8 | + |
| 9 | +Also included is a Linux-based client example that can be run from a host computer to test the server. This setup allows for diagnostics and interaction with the embedded device over a CAN bus. |
| 10 | + |
| 11 | +## Required Hardware |
| 12 | + |
| 13 | +#### Server (Embedded Device) |
| 14 | +* Any development board running the RT-Thread operating system. |
| 15 | +* A CAN transceiver connected to the board's CAN peripheral (e.g., a board with an integrated SN65HVD230). |
| 16 | + |
| 17 | +#### Client (Host Machine) |
| 18 | +* A Linux computer (or a virtual machine). |
| 19 | +* A SocketCAN-compatible adapter (e.g., USB-to-CAN adapter) connected to the host and the embedded device's CAN bus. |
| 20 | + |
| 21 | +## Setup: Server (RT-Thread Device) |
| 22 | + |
| 23 | +1. **Integrate the Library**: Add the UDS library source code to your RT-Thread project. |
| 24 | +2. **Configure the Port**: Ensure the `rtt_uds_example.c` file is correctly configured for your target board, especially the CAN device name (`UDS_EXAMPLE_CAN_DEVICE_NAME`) and any GPIOs used in the example. |
| 25 | +3. **Compile and Flash**: Build and flash the RT-Thread project to your target hardware using your standard toolchain (e.g., SCons). |
| 26 | +4. **Run the Example**: Connect to the device's serial console (e.g., via MSH/Finsh) and start the UDS server example. |
| 27 | + |
| 28 | +```sh |
| 29 | +# Connect to the RT-Thread serial console |
| 30 | +msh /> |
| 31 | +msh />uds_example start |
| 32 | +11-14 19:53:09 isotp.rtt: UDS example started on can1. |
| 33 | +``` |
| 34 | + |
| 35 | +## Setup: Client (Linux Host) |
| 36 | + |
| 37 | +1. **Configure SocketCAN**: Bring up the CAN interface on your Linux machine. |
| 38 | + |
| 39 | + ```sh |
| 40 | + # Example for a device at 1Mbit/s |
| 41 | + sudo ip link set can0 up type can bitrate 1000000 |
| 42 | + ``` |
| 43 | + |
| 44 | +2. **Compile and Run**: Navigate to the client example directory, compile it, and run the executable. |
| 45 | + |
| 46 | + ```sh |
| 47 | + # Assuming the client source files are in the current directory |
| 48 | + make && ./client |
| 49 | + ``` |
| 50 | + |
| 51 | +## Example Interaction Log |
| 52 | + |
| 53 | +This section shows a typical interaction between the Linux client and the RT-Thread server, along with the corresponding CAN traffic dump. |
| 54 | + |
| 55 | +### Server Output (RT-Thread MSH/Finsh Console) |
| 56 | + |
| 57 | +After starting the server with `uds_example start`, the device begins listening for CAN messages. When the client sends `WriteDataByIdentifier` requests, the server processes them and prints log messages. |
| 58 | + |
| 59 | +```sh |
| 60 | +msh />uds_example start |
| 61 | +11-14 19:53:09 isotp.rtt: UDS example started on can1. |
| 62 | +
|
| 63 | +# Client sends a request to write 0x01 to DID 0x0001 |
| 64 | +11-14 19:53:10 isotp.rtt: CAN RX ID:0x7E0 [5 bytes]: 04 2E 00 01 01 |
| 65 | +11-14 19:53:10 UDS.core: I (6703) ./iso14229.c: phys link received 4 bytes |
| 66 | +11-14 19:53:10 isotp.rtt: Server Event: UDS_EVT_WriteDataByIdent (0xA) |
| 67 | +11-14 19:53:10 isotp.rtt: --> WDBI DID:0x0001 Data [1 bytes]: 01 |
| 68 | +11-14 19:53:10 isotp.rtt: Controlling LEDs with value: 0x01 |
| 69 | +11-14 19:53:10 isotp.rtt: [TX] ID: 0x7E8 [4 bytes]: 03 6E 00 01 |
| 70 | +
|
| 71 | +# Client sends a request to write 0x02 to DID 0x0001 |
| 72 | +11-14 19:53:10 isotp.rtt: CAN RX ID:0x7E0 [5 bytes]: 04 2E 00 01 02 |
| 73 | +11-14 19:53:10 UDS.core: I (6762) ./iso14229.c: phys link received 4 bytes |
| 74 | +11-14 19:53:10 isotp.rtt: Server Event: UDS_EVT_WriteDataByIdent (0xA) |
| 75 | +11-14 19:53:10 isotp.rtt: --> WDBI DID:0x0001 Data [1 bytes]: 02 |
| 76 | +11-14 19:53:10 isotp.rtt: Controlling LEDs with value: 0x02 |
| 77 | +11-14 19:53:10 isotp.rtt: [TX] ID: 0x7E8 [4 bytes]: 03 6E 00 01 |
| 78 | +# ... and so on ... |
| 79 | +``` |
| 80 | + |
| 81 | +### Client Output (Linux Console) |
| 82 | + |
| 83 | +The client application shows the state transitions and events as it sends requests and receives responses from the server. |
| 84 | + |
| 85 | +```sh |
| 86 | +root@root:/Debug# ./client |
| 87 | +I (2431038538) src/tp/isotp_sock.c: client initialized phys link (fd 3) rx 0x7e8 tx 0x7e0 func link (fd 4) rx 0x7e8 tx 0x7df |
| 88 | +I (2431038538) client.c: polling |
| 89 | +I (2431038538) src/client.c: client state: Idle (0) -> Sending (1) |
| 90 | +I (2431038538) src/client.c: client state: Sending (1) -> AwaitSendComplete (2) |
| 91 | +I (2431038538) client.c: UDS_EVT_SendComplete (26) |
| 92 | +I (2431038538) src/client.c: client state: AwaitSendComplete (2) -> AwaitResponse (3) |
| 93 | +I (2431038559) src/client.c: received 3 bytes. Processing... |
| 94 | +I (2431038559) client.c: UDS_EVT_ResponseReceived (27) |
| 95 | +I (2431038559) client.c: WDBI response received |
| 96 | +I (2431038559) src/client.c: client state: AwaitResponse (3) -> Idle (0) |
| 97 | +I (2431038559) client.c: UDS_EVT_Idle (28) |
| 98 | +# ... loop continues, sending next request ... |
| 99 | +``` |
| 100 | + |
| 101 | +### CAN Bus Traffic (`candump`) |
| 102 | + |
| 103 | +Using the `candump` utility on the Linux host shows the raw CAN frames exchanged between the client and the server. |
| 104 | + |
| 105 | +* `7E0`: Client (tester) sending a request to the server (ECU). |
| 106 | +* `7E8`: Server (ECU) sending a response back to the client. |
| 107 | + |
| 108 | +```sh |
| 109 | +candump can0 |
| 110 | + # Request: Write Data (0x2E) to DID 0x0001 with value 0x01 |
| 111 | + can0 7E0 [5] 04 2E 00 01 01 |
| 112 | + # Response: Positive Response (0x6E) for service 0x2E, DID 0x0001 |
| 113 | + can0 7E8 [4] 03 6E 00 01 |
| 114 | + |
| 115 | + # Request: Write Data (0x2E) to DID 0x0001 with value 0x02 |
| 116 | + can0 7E0 [5] 04 2E 00 01 02 |
| 117 | + # Response: Positive Response |
| 118 | + can0 7E8 [4] 03 6E 00 01 |
| 119 | +
|
| 120 | + # Request: Write Data (0x2E) to DID 0x0001 with value 0x03 |
| 121 | + can0 7E0 [5] 04 2E 00 01 03 |
| 122 | + # Response: Positive Response |
| 123 | + can0 7E8 [4] 03 6E 00 01 |
| 124 | +# ... and so on ... |
| 125 | +``` |
| 126 | + |
| 127 | +### **⚠️ Important Note** |
| 128 | + |
| 129 | +The code and installation instructions here are merely presented as a simple example to demonstrate the basic integration and functionality. For the complete application and detailed functional code, please refer to the code repository: |
| 130 | +**[iso14229 rtt software](https://github.com/wdfk-prog/can_uds)** |
0 commit comments