DiagnosticTool.Protocols.Uds 1.0.0

dotnet add package DiagnosticTool.Protocols.Uds --version 1.0.0                
NuGet\Install-Package DiagnosticTool.Protocols.Uds -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DiagnosticTool.Protocols.Uds" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DiagnosticTool.Protocols.Uds --version 1.0.0                
#r "nuget: DiagnosticTool.Protocols.Uds, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install DiagnosticTool.Protocols.Uds as a Cake Addin
#addin nuget:?package=DiagnosticTool.Protocols.Uds&version=1.0.0

// Install DiagnosticTool.Protocols.Uds as a Cake Tool
#tool nuget:?package=DiagnosticTool.Protocols.Uds&version=1.0.0                

DiagnosticTool UDS 协议

在任意底层通信库的基础上,实现 DoCAN、DoIP 以及 UDS 协议上传输。

流程

通过以下步骤,可实现 UDS 通信。

实现底层 CAN 总线或车载以太网通信

可通过 DiagnosticTool.Devices.Vector.Can nuget 包,构造经典 CAN 或 CAN FD 连接器,实现 CAN 总线的通信。

var appChannel = new Devices.Vector.XlApplicationChannelRecord("DiagnosticTool", vxlapi_NET.XLDefine.XL_BusTypes.XL_BUS_TYPE_CAN, 2);
var busParams = new Devices.Vector.Can.XlCanFdBusParameters(500 * 1000, 0.75, 2000 * 1000, 0.75);
using var connector = new Devices.Vector.Can.CanFdConnector(appChannel, busParams);
await connector.ConnectAsync();
构建发送、接收的方法

按照如下规则,构建发送、接收方法:

Task SendAsync(CanFrame canFrame, TimeSpan? timeout = null, CancellationToken cancellationToken = default);
Task<CanFrame> ReceiveAsync(TimeSpan? timeout = null, CancellationToken cancellationToken = default);

以下为使用 DiagnosticTool.Devices.Vector.Can 连接器构建的方法示例:

async Task SendAsync(CanFrame canFrame, TimeSpan? timeout = null, CancellationToken cancellationToken = default)
{
    var XlCanFrame = new Devices.Vector.Can.CanFrame()
    {
        CanId = canFrame.CanId,
        CanIdExtendFlag = canFrame.ExtendedFlag,
        DataLengthCode = canFrame.Dlc,
        CanFdFlag = canFrame.FdFlag,
        Data = canFrame.Data,
        BitRateSwitchFlag = canFrame.BrsFlag,
    };
    var sw = Stopwatch.StartNew();
    while (sw.Elapsed < timeout)
    {
        cancellationToken.ThrowIfCancellationRequested();
        try
        {
            await connector.SendAsync(XlCanFrame);
            break;
        }
        catch (Devices.Vector.Can.Exceptions.OperationFailedException)
        {
            continue;
        }
    }
}

async Task<CanFrame> ReceiveAsync(TimeSpan? timeout = null, CancellationToken cancellationToken = default)
{
    using var cts = new Utilities.AutoDisposeCancellationTokenSource(timeout, cancellationToken);
    var XlCanFrame = await connector.ReceiveAsync(cts.Token);
    return new CanFrame()
    {
        CanId = XlCanFrame.CanId,
        ExtendedFlag = XlCanFrame.CanIdExtendFlag,
        Dlc = XlCanFrame.DataLengthCode,
        FdFlag = XlCanFrame.CanFdFlag,
        BrsFlag = XlCanFrame.BitRateSwitchFlag,
        Data = XlCanFrame.Data,
    };
}
创建 DoCAN 或 DoIP 传输器实例

使用构建的发送、接收方法,以及自定义协议参数,构建对应协议的传输器。

var doCanTransmitter = new DoCanTransmitter()
{
    TransportLayerParameters = new TransportLayerParameters()
    {
        RequestId = 0x77A,
        ResponseId = 0x7BA,
        UseFD = true,
        MinDlc = 8,
        MaxDlc = 8,
        BrsEnabled = false,
    },
    SendCanFrameAsync = SendAsync,
    ReceiveCanFrameAsync = ReceiveAsync,
};
创建 UDS 传输器实例

使用创建好的 DoCAN 或 DoIP 传输器的发送、接收方法,构建 UDS 协议的传输器。

var udsTransmitter = new UdsTransmitter()
{
    SendContentAsync = doCanTransmitter.SendAsync,
    ReceiveContentAsync = doCanTransmitter.ReceiveAsync,
};
发送诊断请求并获取响应
var req = new byte[] { 0xBA, 0x10, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14 };
var resp = await udsTransmitter.SendRequestAsync(req);

完整参考代码

using DiagnosticTool.Devices.Vector.Can;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using DiagnosticTool.Protocols.Uds.DoCan;
using vxlapi_NET;

namespace DiagnosticTool.Protocols.Uds.Tests
{
    internal class UdsTransmitterWithVectorCanExamples
    {
        public static async Task Example1()
        {
            #region 构造 Vector CAN 连接器

            var vectorApplicationName = "DiagnosticTool";
            var vectorApplicationChannel = new DiagnosticTool.Devices.Vector.XlApplicationChannelRecord(vectorApplicationName, XLDefine.XL_BusTypes.XL_BUS_TYPE_CAN, 0);
            var vectorCanFdBusParameters = new XlCanFdBusParameters();
            var connector = new CanFdConnector(vectorApplicationChannel, vectorCanFdBusParameters);

            #endregion

            #region 构造 DoCAN 传输器

            async Task SendCanFrameAsync(DiagnosticTool.Protocols.Uds.DoCan.CanFrame canFrame, TimeSpan? timeout = null, CancellationToken cancellationToken = default)
            {
                var xlCanFrame = new DiagnosticTool.Devices.Vector.Can.CanFrame()
                {
                    CanId = canFrame.CanId,
                    CanIdExtendFlag = canFrame.ExtendedFlag,
                    DataLengthCode = canFrame.Dlc,
                    CanFdFlag = canFrame.FdFlag,
                    Data = canFrame.Data,
                    BitRateSwitchFlag = canFrame.BrsFlag,
                };
                var sw = Stopwatch.StartNew();
                while (sw.Elapsed < timeout)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    try
                    {
                        await connector.SendAsync(xlCanFrame);
                        break;
                    }
                    catch (DiagnosticTool.Devices.Vector.Can.Exceptions.OperationFailedException)
                    {
                        continue;
                    }
                }
            }

            async Task<DiagnosticTool.Protocols.Uds.DoCan.CanFrame> ReceiveCanFrameAsync(TimeSpan? timeout = null, CancellationToken cancellationToken = default)
            {
                using var timeoutCts = timeout == null ? new CancellationTokenSource() : new CancellationTokenSource(timeout.Value);
                using var cts = CancellationTokenSource.CreateLinkedTokenSource(timeoutCts.Token, cancellationToken);
                var xlCanFrame = await connector.ReceiveAsync(cts.Token);
                return new DiagnosticTool.Protocols.Uds.DoCan.CanFrame()
                {
                    CanId = xlCanFrame.CanId,
                    ExtendedFlag = xlCanFrame.CanIdExtendFlag,
                    Dlc = xlCanFrame.DataLengthCode,
                    FdFlag = xlCanFrame.CanFdFlag,
                    BrsFlag = xlCanFrame.BitRateSwitchFlag,
                    Data = xlCanFrame.Data,
                };
            }

            var doCanTransportLayerParameters = new DiagnosticTool.Protocols.Uds.DoCan.TransportLayerParameters()
            {
                RequestId = 0x707,
                RequestIdExtend = false,
                ResponseId = 0x727,
                ResponseIdExtend = false,
                UseFD = false,
                BrsEnabled = false,
                MaxDlc = 8,
                MinDlc = 8,
            };
            var doCanTransmitter = new DoCanTransmitter()
            {
                TransportLayerParameters = doCanTransportLayerParameters,
                SendCanFrameAsync = SendCanFrameAsync,
                ReceiveCanFrameAsync = ReceiveCanFrameAsync,
            };

            #endregion

            #region 构造 UDS 传输器

            var udsApplicationLayerParameters = new ApplicationLayerParameters()
            {

            };
            var udsTransmitter = new UdsTransmitter()
            {
                ApplicationLayerParameters = udsApplicationLayerParameters,
                SendContentAsync = doCanTransmitter.SendAsync,
                ReceiveContentAsync = doCanTransmitter.ReceiveAsync,
            };

            #endregion


            await connector.ConnectAsync();
            var response = await udsTransmitter.SendRequestAsync([0x22, 0xF1, 0x8C]);
            Console.WriteLine(response.ToHexString());
            await connector.DisconnectAsync();
        }
    }
}

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
.NET Framework net462 is compatible.  net463 was computed.  net47 is compatible.  net471 is compatible.  net472 is compatible.  net48 is compatible.  net481 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 29 2/20/2025
1.0.0-rc.3 19 2/18/2025

发布正式版,实现 DoCAN 协议的诊断通信。