gpt4 book ai didi

c - 通过 socketCAN 发送 EFF 29 位 ID 消息的问题

转载 作者:行者123 更新时间:2023-11-30 14:40:32 26 4
gpt4 key购买 nike

我正在尝试通过套接字发送 29 位 EFF CAN 消息,但由于某种原因,它使用 11 位标识符发送消息,从 ID 中删除 5 个字节。使用环回模式,通过 candump 我可以看到消息以 11 位形式接收。

我没有收到任何错误,什么也没有,这就是我感到困惑的原因

如果这有什么区别的话,我正在使用树莓派 3b+。和 Can-utils 库。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <net/if.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>

#include <linux/can.h>
#include <linux/can/raw.h>

#include </home/pi/can-utils/lib.h>

int main(int argc, char **argv)
{
int s,loop = 1;
int nbytes;
struct sockaddr_can addr;
struct can_frame frame;
struct ifreq ifr;

//can interface

if((s=socket(PF_CAN, SOCK_RAW, CAN_RAW))<0){
perror("socket");
return 1;
}


strcpy(ifr.ifr_name, "can0");
if(ioctl(s, SIOCGIFINDEX, &ifr) < 0){
perror("SIOCGIFINDEX");
return 1;
}


addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;


if(bind(s,(struct sockaddr *)&addr, sizeof(addr)) < 0){
perror("bind");
return 1;
}

//data send loop

while(loop){
int g;

for (int e = 0; e < 1020; e++){
if(e < 10) {g = 0;}
else if (e%100 == 0){ g = 1;}
else if (e%50 == 0){ g = 2;}
else if (e%20 == 0) {g = 3;}

if (g==0){
switch(e){

case 1: //TOTAL FUEL USED 5-8
frame.can_id = 0x0000FEE9;
printf("value %X",frame.can_id);
frame.can_dlc = 8;
frame.data[0] = 0x00;
frame.data[1] = 0x00;
frame.data[2] = 0x00;
frame.data[3] = 0x00;
frame.data[4] = 0xF5; //0.5L/BIT
frame.data[5] = 0x06; //0.5L/BIT
frame.data[6] = 0x07; //0.5L/BIT
frame.data[7] = 0x08; //0.5L/BIT
break;

case 2: //FUEL LEVEL 2
frame.can_id = 0x0000FEFC;
frame.can_dlc = 8;
frame.data[0] = 0x00;
frame.data[1] = 0x88; //0.4%/BIT
frame.data[2] = 0x00;
frame.data[3] = 0x00;
frame.data[4] = 0x00;
frame.data[5] = 0x00;
frame.data[6] = 0x00;
frame.data[7] = 0x00;
break;

case 3: //TOTAL ENGINE HOURS 1-4
frame.can_id = 0x0000FEE5;
frame.can_dlc = 8;
frame.data[0] = 0x11; //0.05h/BIT
frame.data[1] = 0x22; //0.05h/BIT
frame.data[2] = 0x33; //0.05h/BIT
frame.data[3] = 0x44; //0.05h/BIT
frame.data[4] = 0x00;
frame.data[5] = 0x00;
frame.data[6] = 0x00;
frame.data[7] = 0x00;
break;

case 4: //MILLEAGE 1-4
frame.can_id = 0x0000FEC1;
frame.can_dlc = 8;
frame.data[0] = 0x44; //5M/BIT
frame.data[1] = 0x33; //5M/BIT
frame.data[2] = 0x22; //5M/BIT
frame.data[3] = 0x11; //5M/BIT
frame.data[4] = 0x00;
frame.data[5] = 0x00;
frame.data[6] = 0x00;
frame.data[7] = 0x00;
break;

case 5:
frame.can_id = 0x0000FEEE; //ENGINE TEMPERATURE 1
frame.can_dlc = 8;
frame.data[0] = 0xCC; //1C/BIT -40C OFFSET
frame.data[1] = 0x00;
frame.data[2] = 0x00;
frame.data[3] = 0x00;
frame.data[4] = 0x00;
frame.data[5] = 0x00;
frame.data[6] = 0x00;
frame.data[7] = 0x00;
break;

case 6:
frame.can_id = 0x0000FEF5; //AMBIENT TEMPERATURE 4-5
frame.can_dlc = 8;
frame.data[0] = 0x00;
frame.data[1] = 0x00;
frame.data[2] = 0x00;
frame.data[3] = 0xE4; //0.03125C/BIT -273C OFFSET
frame.data[4] = 0xF2; //0.03125C/BIT -273C OFFSET
frame.data[5] = 0x00;
frame.data[6] = 0x00;
frame.data[7] = 0x00;
break;

case 7: //HIGH RES FUEL 5-8
frame.can_id = 0x0000FD09;
frame.can_dlc = 8;
frame.data[0] = 0x00;
frame.data[1] = 0x00;
frame.data[2] = 0x00;
frame.data[3] = 0x00;
frame.data[4] = 0x01; //0.001L/BIT
frame.data[5] = 0x02; //0.001L/BIT
frame.data[6] = 0x03; //0.001L/BIT
frame.data[7] = 0x04; //0.001L/BIT
break;

case 8: //VEHICLE WEIGHT 2-3
frame.can_id = 0x0000FEEA;
frame.can_dlc = 8;
frame.data[0] = 0x00;
frame.data[1] = 0x05; //0.5KG/BIT
frame.data[2] = 0xFF; //0.5KG/BIT
frame.data[3] = 0x00;
frame.data[4] = 0x00;
frame.data[5] = 0x00;
frame.data[6] = 0x00;
frame.data[7] = 0x00;
break;

case 9: //DISTANCE BEFORE SERVICE 2-3
frame.can_id = 0x0000FEC0;
frame.can_dlc = 8;
frame.data[0] = 0x00;
frame.data[1] = 0xEF; //5KM/BIT -160 635 KM OFFSET
frame.data[2] = 0xCD; //5KM/BIT -160 635 KM OFFSET
frame.data[3] = 0x00;
frame.data[4] = 0x00;
frame.data[5] = 0x00;
frame.data[6] = 0x00;
frame.data[7] = 0x00;
break;
default:
break;
}

for(int i = 0; i <1; i++){
if ((nbytes = write(s, &frame, sizeof(struct can_frame))) != sizeof(frame)){
}
}
}

if (g == 1){
for(int l = 1; l < 5; l++){
switch(l){
case 1: //EEC 3-5
frame.can_id = 0x000CF004;
frame.can_dlc = 8;
frame.data[0] = 0x00;
frame.data[1] = 0x00;
frame.data[2] = 0xEE; //TORQUE 1%/BIT -125% OFFSET
frame.data[3] = 0xEF; //RPM 0.125/BIT
frame.data[4] = 0x12; //RPM 0.125/BIT
frame.data[5] = 0x00;
frame.data[6] = 0x00;
frame.data[7] = 0x00;
break;

case 2: //FUEL ECONOMY 1-4
frame.can_id = 0x0000FEF2;
frame.can_dlc = 8;
frame.data[0] = 0xEC; //FUEL RATE (0.05L/h)/BIT
frame.data[1] = 0xFF; //FUEL RATE (0.05L/h)/BIT
frame.data[2] = 0x15; //CURRENT FUEL (1/512KM/L)/BIT
frame.data[3] = 0xE5; //CURRENT FUEL (1/512KM/L)/BIT
frame.data[4] = 0x00;
frame.data[5] = 0x00;
frame.data[6] = 0x00;
frame.data[7] = 0x00;
break;

case 3: //WHEEL SPEED 2-3
frame.can_id = 0x0000FEF1;
frame.can_dlc = 8;
frame.data[0] = 0x00;
frame.data[1] = 0x01; //(1/256KM/h)/BIT
frame.data[2] = 0x99; //(1/256KM/h)/BIT
frame.data[3] = 0x00;
frame.data[4] = 0x00;
frame.data[5] = 0x00;
frame.data[6] = 0x00;
frame.data[7] = 0x00;
break;

case 4: //EEC2 2-3
frame.can_id = 0x0000F003;
frame.can_dlc = 8;
frame.data[0] = 0x00;
frame.data[1] = 0x03; //0.4%/BIT CCELERATOR POSITION
frame.data[2] = 0x55; //1%/BIT ENGINE LOAD
frame.data[3] = 0x00;
frame.data[4] = 0x00;
frame.data[5] = 0x00;
frame.data[6] = 0x00;
frame.data[7] = 0x00;
break;
}
for(int i = 0; i <1; i++){
if ((nbytes = write(s, &frame, sizeof(struct can_frame))) != sizeof(frame)){
}
}
if(l<5) usleep(1000);
}

}
else if (g == 2){

frame.can_id = 0x0000F003;
frame.can_dlc = 8;
frame.data[0] = 0x00;
frame.data[1] = 0x03; //0.4%/BIT CCELERATOR POSITION
frame.data[2] = 0x55; //1%/BIT ENGINE LOAD
frame.data[3] = 0x00;
frame.data[4] = 0x00;
frame.data[5] = 0x00;
frame.data[6] = 0x00;
frame.data[7] = 0x00;

for(int i = 0; i <1; i++){
if ((nbytes = write(s, &frame, sizeof(struct can_frame))) != sizeof(frame)){
}
}
}
else if (g == 3){

frame.can_id = 0x0000F004;
frame.can_dlc = 8;
frame.data[0] = 0x00;
frame.data[1] = 0x00;
frame.data[2] = 0xEE; //TORQUE 1%/BIT -125% OFFSET
frame.data[3] = 0xEF; //RPM 0.125/BIT
frame.data[4] = 0x12; //RPM 0.125/BIT
frame.data[5] = 0x00;
frame.data[6] = 0x00;
frame.data[7] = 0x00;

for(int i = 0; i <1; i++){
if ((nbytes = write(s, &frame, sizeof(struct can_frame))) != sizeof(frame)){
}
}
}
usleep(10000);
g = 5;
if(e==900) e = 0;
}
}

close(s);
return 0;
}

最佳答案

设置can_id后需要添加正确的标志:

frame.can_id |= CAN_EFF_FLAG;

或者例如:而不是

frame.can_id = 0x0000FEE9

使用

frame.can_id = 0x0000FEE9U | CAN_EFF_FLAG ;

这里 U 表示您的整数文字是无符号的。另请参阅https://github.com/linux-can/can-utils/blob/master/include/linux/can.h

关于c - 通过 socketCAN 发送 EFF 29 位 ID 消息的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55499470/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com