Skip to main content

a uds package

Project description

org/uds.py 使用说明

1. 作用简介

提供了一个基于 CAN 总线的 UDS(Unified Diagnostic Services,统一诊断服务)封装类。它可以用于:

  • 通过 Vector 硬件发送 UDS 请求
  • 接收 CAN 总线上的响应数据
  • 自动处理简单的 UDS 响应解析和日志输出

使用 Vector 接口进行通信。


2. 依赖环境

请先确认环境中已经安装以下内容:

  • Python 3.x
  • Vector 驱动 / Vector 硬件(如果你要实际通过 Vector 设备通信)

如果没有安装 Vector 驱动,初始化时可能会出现连接失败或无法收发数据的情况。


3. 基本使用流程

3.1 导入模块

from py_uds import VECTOR

3.2 配置参数

config = {
    "tr_id": "0x7E0",   # 发送请求的 CAN ID
    "rx_id": "0x7E8",   # 期望接收响应的 CAN ID
    "ch": 1,            # Vector 通道号
    "fd": False,        # 是否使用 CAN FD
    "app_name": "CANoe",
    "bitrate": 500000,
    "stmin": 20,        # 发送间隔,单位 ms
    "mask": "0xFF",
    "pad": "0x55",
}

3.3 初始化对象

uds = VECTOR(config, log_path="uds.log")

初始化后会自动:

  • 创建日志文件
  • 初始化 CAN 总线
  • 检测 Vector 驱动是否可用

4. 发送 UDS 请求

4.1 使用 send_req 发送请求

resp = uds.send_req("10 01", dlc=8, t=30)
print(resp)

参数说明:

  • req: UDS 请求内容,使用十六进制字节,以空格分隔,例如 10 01
  • dlc: CAN 数据长度,默认 8
  • t: 超时时间,单位为秒,默认 30

这个方法会把请求发送出去,并等待响应返回。

4.2 示例:发送读数据标识请求

resp = uds.send_req("22 F1 90", dlc=8, t=10)
print(resp)

这里的 22 F1 90 是一个典型的 UDS 读数据请求示例。


5. 发送原始 CAN 数据

如果你想直接发送自定义十六进制字节,可以使用 send_msg

uds.send_msg(["10", "01", "00", "00", "00", "00", "00", "00"], dlc=8, t=30)

参数说明:

  • data: 需要发送的数据列表,每项是十六进制字符串
  • dlc: 数据长度
  • t: 发送后等待的时间,单位为毫秒(内部会转换为 sleep 间隔)

6. 接收消息

如果你只想接收一条消息,可以使用:

msg = uds.recv_msg(timeout=10)
print(msg)

其中 timeout 的单位是秒。该方法会持续监听总线上的消息,直到超时或收到有效数据。


7. 关闭连接

通信结束后建议关闭总线对象:

uds.close()

8. 常见参数说明

  • tr_id: 发送请求时使用的 CAN 标识符
  • rx_id: 期望接收响应时匹配的 CAN 标识符
  • ch: Vector 通道号
  • fd: 是否启用 CAN FD
  • stmin: 两次发送之间的最小间隔,单位 ms
  • mask: CAN 过滤掩码
  • pad: 补齐字节时使用的默认值
  • log_path: 日志文件路径
  • log_level: 日志级别

9. 日志说明

初始化后会生成日志文件,例如:

uds.log

日志中会记录:

  • 总线初始化情况
  • 发送的数据
  • 接收到的数据
  • 错误信息

10. 一个完整示例

from org.uds import VECTOR

config = {
    "tr_id": "0x7E0",
    "rx_id": "0x7E8",
    "ch": 1,
    "fd": False,
    "app_name": "CANoe",
    "bitrate": 500000,
    "stmin": 20,
    "mask": "0xFF",
    "pad": "0x55",
}

uds = VECTOR(config, log_path="uds.log")

try:
    resp = uds.send_req("10 01", dlc=8, t=10)
    print("响应:", resp)
finally:
    uds.close()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

uds_pypkg-1.0.0-cp310-cp310-win_amd64.whl (59.7 kB view details)

Uploaded CPython 3.10Windows x86-64

File details

Details for the file uds_pypkg-1.0.0-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: uds_pypkg-1.0.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 59.7 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.20

File hashes

Hashes for uds_pypkg-1.0.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 bd93df20a6597ade13455587efc0b64602c02e8f91f084ea0770d70f1ba48d46
MD5 3daccadfa3f3f3da71e0f62a5d82f49e
BLAKE2b-256 8959d5d578468a928d6bdc12743aef26e5db82f02ef93e65a92191e9a5a92de6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page