可以套接字printf格式

时间:2015-10-09 09:02:15

标签: c sockets can-bus

显示我的CAN ID时出现问题。我通过CAN总线发送一条消息,ID为0x18FF11F3。我的程序收到此消息以及所有数据字段,只有ID不完全匹配

    void set_can_listener(uint16 *s16_Socket, struct can_frame *Frame) {
    /* Create the socket */
    *s16_Socket = socket(PF_CAN, SOCK_RAW, CAN_RAW);

    /* Locate the interface you wish to use */
    struct ifreq t_Ifr;
    strcpy(t_Ifr.ifr_name, "can0");
    ioctl(*s16_Socket, SIOCGIFINDEX, &t_Ifr); /* Ifr.ifr_ifindex gets filled with that device's index*/

    /* Select that CAN interface, and bind the socket to it.*/
    struct sockaddr_can t_Addr;
    t_Addr.can_family = AF_CAN;
    t_Addr.can_ifindex = t_Ifr.ifr_ifindex;
    bind(*s16_Socket, (struct sockaddr*) &t_Addr, sizeof(t_Addr));
}


void can_listener(uint16 *s16_Socket, struct can_frame *Frame) {
    /* Read message from CAN */
    unsigned int i;
    uint16 s16_BytesRead = read(*s16_Socket, Frame, sizeof(*Frame));
    if (s16_BytesRead >= 0) {
        printf("\nMessage received!\nID: 0x%X\nDLC: %X\n", Frame->can_id,
                Frame->can_dlc);
    }
}

我的输出如下:

Message received! 
ID: 0x98FF11F3 
DLC: 8

1 个答案:

答案 0 :(得分:1)

您正在使用CAN_EFF_FLAG过滤器集读取ID,定义为

#define CAN_EFF_FLAG 0x80000000U

HERE

代码正常,收到的消息也没问题。发件人在套接字上启用了EFF过滤器。

THIS链接可以解释您关于SocketCAN

的所有信息