我正在使用socket CAN,我实现了以下代码:
int main(int argc, char **argv)
{
char *sendSoc = NULL;
char *receiveSoc = NULL;
char specifiedType;
struct can_frame sendFrame;
struct can_frame receiveFrame;
int sendSocket=-1, receiveSocket=-1;
int can_id;
int type=-1;
int dat,k=0,iter=0, sendCount, interval;
struct can_filter sendSocket_filter;
struct can_filter receiveSocket_filter;
unsigned long long value = 0;
if (argc < 8)
{
fprintf(stderr, "Usage: %s [<canSendInterface>] [<canReceiveInterface>] <id> <type> <interval(µs)> <sendCount> <can-msg>\n"
"<id> identifier=ID CAN Identifier\n"
"<type> type is 's' for standard and 'e' for extended frame\n"
"<interval(us)> send message repetition rate\n"
"<sendCount> send message COUNT times\n"
"<can-msg> can consist of up to 8 bytes given as a space separated list - data must be in hexadecimal\n"
" <data0> <data1> <data2> <data3> <data4> <data5> <data6> <data7>",
argv[0]);
return 1;
}
sendSoc = argv[1];
receiveSoc = argv[2];
sscanf(argv[3], "%x", (unsigned int *) & sendFrame.can_id);
sendFrame.can_dlc = 8;
sendSocket_filter.can_id = sendFrame.can_id;
receiveSocket_filter.can_id = sendFrame.can_id;
sscanf(argv[4], "%c", &specifiedType);
if( specifiedType == 's')
{
type = STANDARD;
sendSocket_filter.can_mask = 0x000007ff;
receiveSocket_filter.can_mask = 0x000007ff;
}
else if( specifiedType == 'e')
{
type = EXTENDED;
sendSocket_filter.can_mask = 0x1fffffff;
receiveSocket_filter.can_mask = 0x1fffffff;
}
else
{
printf("Bad Type Entered \n");
}
interval = atoi(argv[5]);
sendCount = atoi(argv[6]);
if( (type == STANDARD) && (sendFrame.can_id > 0x7ff) )
{
printf("Error: CAN-ID too big for standard frame \n");
exit(-1);
}
if( (type == EXTENDED) && (sendFrame.can_id > 0x1fffffff) )
{
printf("Error: CAN-ID too big for extended frame \n");
exit(-1);
}
printf("here\n");
for(k=0; k<9; k++)
{
sscanf(argv[7+k], "%x", &dat);
sendFrame.data[k] = (unsigned char)(dat & 0xff);
printf("sendFrame.data[%d] vaut %d\n",k,sendFrame.data[k]);
}
printf("test\n");
return 0;
}
我系统地获得了分段错误。
基本上,我用脚本调用脚本 ./scriptname can0 can1 0x1ffff e 10 5 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x1E
结果是:
LA
sendFrame.data[0] vaut 1
sendFrame.data[1] vaut 2
sendFrame.data[2] vaut 3
sendFrame.data[3] vaut 4
sendFrame.data[4] vaut 5
sendFrame.data[5] vaut 6
sendFrame.data[6] vaut 7
sendFrame.data[7] vaut 30
Segmentation fault
在can.h中,结构是:
52 * struct can_frame - basic CAN frame structure
53 * @can_id: the CAN ID of the frame and CAN_*_FLAG flags, see above.
54 * @can_dlc: the data length field of the CAN frame
55 * @data: the CAN frame payload.
56 */
57 struct can_frame {
58 canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */
59 __u8 can_dlc; /* data length code: 0 .. 8 */
60 __u8 data[8] __attribute__((aligned(8)));
61 };
目前我不明白为什么会发生此分段错误。目的是通过专用插座在CAN上发送这些值。
提前感谢
答案 0 :(得分:2)
使用for循环:
for(k=0; k<9; k++)
{
sscanf(argv[7+k], "%x", &dat);
sendFrame.data[k] = (unsigned char)(dat & 0xff);
printf("sendFrame.data[%d] vaut %d\n",k,sendFrame.data[k]);
}
你到达argv[7+8] = argv[15]
。但是您的命令中只有14个输入参数:
./scriptname can0 can1 0x1ffff e 10 5 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x1E
[1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14]
所以argv数组中的最后一个元素是argv[14]
。
细分错误来自对argv[15]
的访问权限,argv
是sendFrame.data
数组中不存在的元素。
您与for(k=0; k<9; k++)
有同样的错误。这个数组是8个元素长度定义和for循环
sendFrame.data
您正在浏览{{1}}数组的9个元素。这也是分段错误的原因