提高:: ASIO超过SocketCAN [英] boost::asio over SocketCAN

查看:228
本文介绍了提高:: ASIO超过SocketCAN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想利用升压短耳插槽CAN 的读取数据。
没有什么花哨的 linux的事情/ can.h 和设备应
行为类似于环回接口,并与原料插座一起使用。

I was thinking of making use of Boost Asio to read data from a Socket CAN. There's nothing fancy going on in linux/can.h , and the device should behave like the loopback interface, and be used with a raw socket.

综观 basic_raw_socket 接口,看来我可以利用
basic_raw_socket ::分配分配与创建本地插座

Looking at the basic_raw_socket interface it seems that I can make use of basic_raw_socket::assign to assign the native socket created with

socket( PF_CAN, SOCK_RAW, CAN_RAW );

这是我迄今为止

namespace can {
       class CanSocket {
       public:
               typedef boost::asio::ip::basic_endpoint<CanSocket> endpoint;
               typedef boost::asio::ip::basic_resolver_query<CanSocket> resolver_query;
               typedef boost::asio::ip::basic_resolver_iterator<CanSocket> resolver_iterator;
               typedef boost::asio::basic_raw_socket<CanSocket> socket;
               typedef boost::asio::ip::basic_resolver<CanSocket> resolver;

               CanSocket()
                       : _protocol( CAN_RAW )
                       , _family( PF_CAN )
               {
               }

               static CanSocket v4()
               {
                       return CanSocket();
               }
               static CanSocket v6();
               int type() const;
               int protocol() const;
               int family() const;

               friend bool operator==(const CanSocket& p1, const CanSocket& p2)
               {
                       return p1._protocol != p2._protocol || p1._family != p2._family;
               }
               friend bool operator!=(const CanSocket& p1, const CanSocket& p2)
               {
                       return p1._protocol == p2._protocol || p1._family == p2._family;
               }

       private:
               int _protocol;
               int _family;
};
}

这是我如何使用它在我的应用

And this is how I use it in my application

   boost::asio::io_service ioserv;

   CanSocket::socket s( ioserv );

   int sock = socket( PF_CAN, SOCK_RAW, CAN_RAW );

   s.assign(CanSocket::v4(), sock);

   struct ifreq ifr;
   strcpy(ifr.ifr_name, "vcan0");
   ioctl(sock, SIOCGIFINDEX, &ifr); /* ifr.ifr_ifindex gets filled
                                 * with that device's index */

   /* Select that CAN interface, and bind the socket to it. */

   /* this should be the endpoint */
   struct sockaddr_can addr;
   addr.can_family = AF_CAN;
   addr.can_ifindex = ifr.ifr_ifindex;

   /* s.bind (....) */
   bind( sock, (struct sockaddr*)&addr, sizeof(addr) );

我不明白对方是怎么做的我的绑定 取值来本地终结点?有没有涉及IP地址或端口。

What I don't quite get is how do I bind s to the local endpoint? There are no IPs or ports involved.

还有什么应该除了端点实施得到了?

Is there anything else that should be implemented besides the endpoint to get it going?

推荐答案

该解决方案是使用 POSIX :: stream_descriptor

只要打开本机插座,绑定,然后使用 POSIX :: basic_stream_descriptor ::分配

Just open the native socket, bind and then use posix::basic_stream_descriptor::assign.

这篇关于提高:: ASIO超过SocketCAN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆