Код: Выделить всё
recvfrom(s, &lCANFrame.data, sizeof(lCANFrame.data), 0x0, (struct sockaddr*)&j1939_addr, &recv_data_len)
Код: Выделить всё
// J1939 CAN Socket
j1939_addr.can_family = AF_CAN;
j1939_addr.can_ifindex = ifr.ifr_ifindex;
j1939_addr.can_addr.j1939.name = J1939_NAME; //J1939 Name
j1939_addr.can_addr.j1939.addr = CAN_SRC_ADDR; //J1939: this is the SRC address of this device transmitting
//j1939_addr.can_addr.j1939.addr = J1939_IDLE_ADDR; //J1939: this is the SRC address of this device transmitting
j1939_addr.can_addr.j1939.pgn = J1939_NO_PGN; //This is a filter for receive function. This will only recieve PGN that is mentioned
Заботится ли ядро о запросе адреса?
А также, как мне отправить сообщение. Ниже не работает. Подскажите пожалуйста, что я делаю не так?
Код: Выделить всё
send_response = sendto(s, &lCANFrame.data[1], 1, 0, (const struct sockaddr*)&j1939_addr, sizeof(j1939_addr)); //Send OUT CAN data to the interface //Send one byte CAN message OUT
Код: Выделить всё
s = socket(PF_CAN, SOCK_DGRAM, CAN_J1939); //Open J1939 socket
strcpy(ifr.ifr_name, can_int_name); //can_int_name is the can interface name "vcan0", can0 etc.
ioctl(s, SIOCGIFINDEX, &ifr); //Retreive the index to the CAN interface
// J1939 CAN Socket
j1939_addr.can_family = AF_CAN;
j1939_addr.can_ifindex = ifr.ifr_ifindex;
j1939_addr.can_addr.j1939.name = J1939_NAME; //J1939 Name
j1939_addr.can_addr.j1939.addr = CAN_SRC_ADDR; //J1939: this is the SRC address of this device transmitting
j1939_addr.can_addr.j1939.pgn = J1939_NO_PGN; //This is a filter for receive function. This will only recieve PGN that is mentioned
if(bind(s, (struct sockaddr*)&j1939_addr, sizeof(j1939_addr))) // Check to make sure Socket Binding is successful
{
perror("Error in Binding Socket");
}
Подробнее здесь: https://stackoverflow.com/questions/787 ... linux-in-c