0

我在 Linux 平台上使用 dotnet core,并希望与 CAN 设备建立通信。Linux平台上可以使用SocketCan,但是有没有办法使用dotnet core中的UnixDomainSocketEndPoint和Socket类直接访问SocketCan呢?

在 Linux (Debian9) 环境中尝试使用此测试程序创建 Socket 实例时:

using System;
using System.Net.Sockets;

namespace SocketScan
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Scan for available ProtocolType values");

            ScanForProtocolTypes(SocketType.Stream);
            ScanForProtocolTypes(SocketType.Raw);
        }

        private static void ScanForProtocolTypes(SocketType socketType)
        {
            for (var i = 0; i < 1000000; i++)
            {
                try
                {
                    var socket = new Socket(AddressFamily.Unix, socketType, (ProtocolType)i);
                    Console.WriteLine($"Succeeded creating socket with SocketType={socketType} ProtocolType={((ProtocolType)i)}");
                }
                catch (SocketException)
                { }
            }
        }
    }
}

输出是:

$ dotnet run SocketTest.csproj 
Scan for available ProtocolType values
Succeeded creating socket with SocketType=Stream ProtocolType=Unspecified
Succeeded creating socket with SocketType=Stream ProtocolType=Icmp
Succeeded creating socket with SocketType=Raw ProtocolType=Unspecified
Succeeded creating socket with SocketType=Raw ProtocolType=Icmp

枚举 AdressFamily、SocketType 和 ProtocolType 的哪些值将匹配 SocketCan 套接字?

4

1 回答 1

0

在 .NET Core 3.0+ 上有AddressFamily.ControllerAreaNetworkand ProtocolFamily.ControllerAreaNetwork,并且 SocketCAN 需要 aSocketType.Raw才能工作。请参阅https://github.com/dotnet/corefx/pull/37315上的相关拉取请求

于 2020-02-17T08:10:35.733 回答