0

I'm using dotnet core on a Linux platform and would like to set up communication with a CAN device. SocketCan is available on the Linux platform, but is there a way to use the UnixDomainSocketEndPoint and Socket classes in dotnet core to access SocketCan directly?

When trying to create an instance of Socket with this test program on Linux (Debian9) environment:

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)
                { }
            }
        }
    }
}

The output is:

$ 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

What values for the enums AdressFamily, SocketType and ProtocolType will match a SocketCan socket?

MagnusK
  • 101
  • 3
  • I did find this implementation of SocketCan in dotnet core: https://github.com/jormenjanssen/netcore-can-example/ However, there is a lot of Reflexion stuff there that I'd rather not use. If possible... – MagnusK Aug 08 '19 at 14:23

1 Answers1

0

On .NET Core 3.0+ there are AddressFamily.ControllerAreaNetwork and ProtocolFamily.ControllerAreaNetwork, and SocketCAN needs a SocketType.Raw to work. See the associated Pull Request at https://github.com/dotnet/corefx/pull/37315

Astrinus
  • 412
  • 9
  • 19