打开网易新闻 查看精彩图片
一、UDS 概述与定位 1.1 什么是UDS?
UDS(Unified Diagnostic Services)是ISO 14229定义的应用层诊断协议,用于汽车电子控制单元(ECU)的故障读取、数据通信、例程控制、软件升级等。
1.2 UDS 与 OBD 的区别
项目
OBD
UDS
目的
排放相关强制诊断
全车ECU增强诊断
范围
有限(发动机、排放)
所有ECU(BCM、GW、BMS等)
扩展性
固定服务
支持自定义服务
网络层
ISO 15765-4
ISO 15765-2(DoCAN)
1.3 DoCAN 协议栈
应用层:UDS(ISO 14229)
网络层:ISO 15765-2(多帧传输)
数据链路层:CAN(ISO 11898)
请求 :SID + SubFunction + 参数
肯定响应 :SID + 0x40 + 参数
否定响应 :0x7F + SID + NRC
请求读取DID
0xF190:22 F1 90肯定响应:
62 F1 90 01 02 03否定响应:
7F 22 13(无效长度)
3.2 ISO 15765-2 网络层简版封装#include
#include
#include
#include
using Byte = uint8_t;
using Bytes = std::vector ;
// UDS 服务 ID(部分)
enum class UDS_SID : Byte {
DIAG_SESSION_CTRL = 0x10,
ECU_RESET = 0x11,
READ_DATA = 0x22,
WRITE_DATA = 0x2E,
SECURITY_ACCESS = 0x27,
TESTER_PRESENT = 0x3E,
ROUTINE_CTRL = 0x31,
READ_DTC = 0x19,
CLEAR_DTC = 0x14
};// NRC
enum class NRC : Byte {
OK = 0x00,
GENERAL_REJECT = 0x10,
SERVICE_NOT_SUPPORTED = 0x11,
INVALID_FORMAT = 0x13,
CONDITIONS_NOT_CORRECT = 0x22,
SECURITY_ACCESS_DENIED = 0x33,
INVALID_KEY = 0x35
};
3.3 UDS 基础处理类// 帧类型
enum class N_PCI_TYPE : Byte {
SINGLE = 0x00,
FIRST = 0x10,
CONSECUTIVE = 0x20,
FLOW_CTRL = 0x30
};
// 单帧/多帧处理(简单示例)
class DoCANTransport {
public:
static Bytes packRequest(const Bytes& udsReq) {
if (udsReq.size() <= 7) {
// 单帧
Byte pci = static_cast (N_PCI_TYPE::SINGLE) | (Byte)udsReq.size();
Bytes frame = {pci};
frame.insert(frame.end(), udsReq.begin(), udsReq.end());
return frame;
} else {
// 简化:实际需拆分为 FF/CF/FC,此处仅示意
std::cout << "[DoCAN] Multi-frame not fully implemented\n";
return {};
}
}static Bytes unpackResponse(const Bytes& canFrame) {
if (canFrame.empty()) return {};
Byte pci = canFrame[0];
N_PCI_TYPE type = static_cast (pci & 0xF0);
if (type == N_PCI_TYPE::SINGLE) {
int len = pci & 0x0F;
return Bytes(canFrame.begin() + 1, canFrame.begin() + 1 + len);
}
// 实际需重组多帧
return {};
}
};
class UDSHandler {
private:
std::map
std
::function
const
Bytes&)>> handlers;
bool
securityLocked =
true
;
public
:
UDSHandler() {
// 注册服务
handlers[UDS_SID::READ_DATA] = [
this
](
const
Bytes& req) {
return
handleReadData(req); };
handlers[UDS_SID::DIAG_SESSION_CTRL] = [
this
](
const
Bytes& req) {
return
handleSessionCtrl(req); };
handlers[UDS_SID::TESTER_PRESENT] = [
this
](
const
Bytes& req) {
return
handleTesterPresent(req); };
}
// 主处理入口
Bytes processRequest(const Bytes& udsReq)
{
if
(udsReq.empty())
return
buildNegativeResponse(
0x00
, NRC::GENERAL_REJECT);
Byte sid = udsReq[
0
];
UDS_SID service =
static_cast
(sid);
if
(handlers.find(service) == handlers.end()) {
return
buildNegativeResponse(sid, NRC::SERVICE_NOT_SUPPORTED);
}
return
handlers[service](udsReq);
}
private
:
// 否定响应
Bytes buildNegativeResponse(Byte sid, NRC nrc)
{
return
{
0x7F
, sid,
static_cast
(nrc)};
}
// 肯定响应
Bytes buildPositiveResponse(Byte sid, const Bytes& data = {})
{
Bytes resp;
resp.push_back(sid +
0x40
);
resp.insert(resp.end(), data.begin(), data.end());
return
resp;
}
// 22 读数据
Bytes handleReadData(const Bytes& req)
{
if
(req.size() <
3
)
return
buildNegativeResponse(
0x22
, NRC::INVALID_FORMAT);
Byte didHigh = req[
1
];
Byte didLow = req[
2
];
uint16_t
did = (didHigh <<
8
) | didLow;
// 模拟数据字典
if
(did ==
0xF190
) {
return
buildPositiveResponse(
0x22
, {
0xF1
,
0x90
,
0x01
,
0x02
,
0x03
});
}
else
if
(did ==
0xF187
) {
return
buildPositiveResponse(
0x22
, {
0xF1
,
0x87
,
'V'
,
'1'
,
'.'
,
'0'
});
}
return
buildNegativeResponse(
0x22
, NRC::GENERAL_REJECT);
}
// 10 会话控制
Bytes handleSessionCtrl(const Bytes& req)
{
if
(req.size() <
2
)
return
buildNegativeResponse(
0x10
, NRC::INVALID_FORMAT);
Byte subfunc = req[
1
];
// 抑制肯定响应检查(bit7 = 1 时不发响应)
bool
suppressResp = (subfunc &
0x80
) !=
0
;
Byte session = subfunc &
0x7F
;
std
::
cout
<<
"[UDS] Switch to session: "
<<
std
::hex << (
int
)session <<
std
::
endl
;
if
(!suppressResp) {
return
buildPositiveResponse(
0x10
, {subfunc,
0x00
,
0x32
,
0x01
,
0xF4
});
// 仿真实例
}
return
{};
// 无响应
}
// 3E 待机握手
Bytes handleTesterPresent(const Bytes& req)
{
bool
suppress = (req.size() >
1
) && ((req[
1
] &
0x80
) !=
0
);
if
(!suppress) {
return
buildPositiveResponse(
0x3E
, {});
}
return
{};
}
};
3.4 主程序示例(诊断请求模拟)输出示例:int main() {
UDSHandler uds;
DoCANTransport canLayer;
// 模拟发送:22 F1 90 读取 DID 0xF190
Bytes udsReq = {0x22, 0xF1, 0x90};
Bytes canFrame = DoCANTransport::packRequest(udsReq);
std::cout << "Send CAN: ";
for (auto b : canFrame) printf("%02X ", b);
std::cout << std::endl;
// 模拟 ECU 收到 CAN 帧,解包得到 UDS 请求
Bytes receivedUdsReq = DoCANTransport::unpackResponse(canFrame);
Bytes udsResp = uds.processRequest(receivedUdsReq);
std::cout << "UDS Response: ";
for (auto b : udsResp) printf("%02X ", b);
std::cout << std::endl;
// 测试会话切换(抑制响应)
Bytes sessionReq = {0x10, 0x83}; // subfunc 0x83 = 0x03 + 抑制bit
canFrame = DoCANTransport::packRequest(sessionReq);
receivedUdsReq = DoCANTransport::unpackResponse(canFrame);
udsResp = uds.processRequest(receivedUdsReq);
if (udsResp.empty()) {
std::cout << "[TesterPresent] No response (suppressed)" << std::endl;
}return 0;
}
Send CAN: 03 22 F1 90
UDS Response: 62 F1 90 01 02 03
[UDS] Switch to session: 3
[TesterPresent] No response (suppressed)
四、关键服务实现要点 4.1 $27 安全访问(种子密钥简化)4.2 14 DTC 处理示例class SecureUDSHandler : public UDSHandler {
int seed = 0x1234;
int expectedKey = 0x5678; // 实际应为算法:key = seed ^ 0x5555Bytes handleSecurityAccess(const Bytes& req) {
Byte subfunc = req[1];
if (subfunc == 0x05) { // 请求种子
return buildPositiveResponse(0x27, {0x05, (Byte)(seed >> 8), (Byte)seed});
} else if (subfunc == 0x06) { // 发送密钥
int key = (req[2] << 8) | req[3];
if (key == expectedKey) {
securityLocked = false;
return buildPositiveResponse(0x27, {0x06});
}
return buildNegativeResponse(0x27, NRC::INVALID_KEY);
}
return buildNegativeResponse(0x27, NRC::INVALID_FORMAT);
}
};
// 简化 DTC 读取(按状态掩码 0xFF)
Bytes handleReadDTC(const Bytes& req) {
if (req.size() >= 2 && req[1] == 0x02) {
// 模拟返回 2 个 DTC
return {0x59, 0x02,
0x01, 0x23, 0x45, 0x80, // DTC 1 + status
0x01, 0x67, 0x89, 0x20}; // DTC 2
}
return buildNegativeResponse(0x19, NRC::INVALID_FORMAT);
}
五、总结特性
描述
标准化
UDS 统一了诊断服务格式,减少重复开发
灵活性
支持自定义 DID、Routine、安全算法
可扩展
基于 ISO 15765-2 可承载任意长度诊断数据
工程落地
现代汽车诊断工具(CANoe、PCAN、ZLG)均支持 UDS + DoCAN
本 C++ 示例展示了:
UDS 请求/响应处理框架
DoCAN 单帧打包/解包
22/$3E 服务的简化实现
否定响应与抑制响应机制
实际 ECU 开发中需完善多帧传输、定时管理、会话状态机和安全访问算法。
热门跟贴