问题
I want to convert some C/C++ code to C# so that I can use it on Linux with MONO instead of using a DLLImport with a *.so.
It seems that System.Net.Sockets doesn't support CAN-bus communication but I'm not sure if that means we cannot solve it anyway!?
// Create a socket with the CAN-protocol.
int _canSocket = socket(PF_CAN, SOCK_RAW, CAN_RAW);
// Get current flags for the socket and add non-blocking to it.
int flags = fcntl(_canSocket, F_GETFL, 0);
fcntl(_canSocket, F_SETFL, flags | O_NONBLOCK);
// Name the interface structure "can0" so that we can get the interface index for the interface named "can0".
struct ifreq ifr;
strcpy(ifr.ifr_name, "can0");
ioctl(_canSocket, SIOCGIFINDEX, &ifr);
// Create a local address for the socket.
struct sockaddr_can addr;
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
// Bind the socket to the address and we're ready to receive / send information on the CAN-bus.
bind(_canSocket, (struct sockaddr *)&addr, sizeof(addr));
I can get the interface using this code
NetworkInterface can = NetworkInterface.GetAllNetworkInterfaces().First(i => i.Name == "can0");
But how do I setup a socket, that speaks "CAN" if .NET doesn't support it. I'm thinking since the CAN is in fact speaking RAW in this case, it's just a matter of pushing bytes in the correct order!?
Or have I misunderstood everything?
Could someone please shed some light over this.
来源:https://stackoverflow.com/questions/27714576/can-bus-communication-with-system-net-sockets