I want to check if the socketcan is bind with roscpp

18 Views Asked by At

I have been checking the vitality of CAN sockets using the bind function without any issues until I started incorporating ros/ros.h and adding ros::NodeHandle. Prior to using ROS, I could inspect each CAN socket individually, and even with multiple sockets, the bind function would correctly return zero/non-zero if the socket was alive/dead. However, after introducing ros/ros.h and adding ros::NodeHandle, the bind function for all CAN sockets started consistently returning 0 (True). Is there anyone with relevant solutions or good ideas regarding this issue?

ros::init(argc, argv, "Test");
ros::NodeHandle nh;              // The issue arises depending on the declaration of the NodeHandle.

int sock;
struct sockaddr_can addr;
struct ifreq ifr;
struct can_frame rxframe;
struct can_frame txframe;

strcpy(ifr.ifr_name, "vcan0");
ioctl(sock, SIOCGIFINDEX, &ifr);

memset(&addr, 0, sizeof(addr));
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;

if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
    std::cerr << "Bind" << std::endl;
    return 1;
}
    return 0;
0

There are 0 best solutions below