DiagnosticTool.Protocols.Uds 1.0.0-rc.1

This is a prerelease version of DiagnosticTool.Protocols.Uds.
There is a newer version of this package available.
See the version list below for details.
The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package DiagnosticTool.Protocols.Uds --version 1.0.0-rc.1                
NuGet\Install-Package DiagnosticTool.Protocols.Uds -Version 1.0.0-rc.1                
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-rc.1" />                
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-rc.1                
#r "nuget: DiagnosticTool.Protocols.Uds, 1.0.0-rc.1"                
#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-rc.1&prerelease

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

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);

完整参考代码

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();
var doCanTransmitter = new DoCanTransmitter()
{
    TransportLayerParameters = new TransportLayerParameters()
    {
        RequestId = 0x77A,
        ResponseId = 0x7BA,
        UseFD = true,
        MinDlc = 8,
        MaxDlc = 8,
        BrsEnabled = false,
    },
    SendCanFrameAsync = SendAsync,
    ReceiveCanFrameAsync = ReceiveAsync,
};
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);
return;

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,
    };
}
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 0 2/20/2025
1.0.0-rc.3 19 2/18/2025

修复 DoCAN 发送连续帧过程存在的问题。