1
我有一個顯示我的CAN ID的問題。我通過CAN總線發送一條消息,ID爲0x18FF11F3。我的程序接收到這個消息,也是所有數據字段,只有ID不完全匹配can socket printf格式
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
如果read()返回零,你不應該打印任何東西。很難相信一點點神奇地設置在某個地方。你確定你發送正確嗎?爲什麼在這段代碼中沒有正確的錯誤檢查?這與'primtf()'有什麼關係? – EJP