⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 can_send.c

📁 can4linux-3.5.3.gz can4 linux
💻 C
📖 第 1 页 / 共 3 页
字号:
    putchar('\n');}void usage(char *s){static char *usage_text  = "\ if optional id is not specified, 100 (0x64) is used\n\ and 8 data bytes are used to construct a mesage.\n\ Id and date can be given decimal (100) or hex 0x64\n\Options:\n\-r send message as rtr message.\n\-e send message in extended message format.\n\-l load try to reach this bus load, given in %\n\-s n - sleeptime between messages in ms, if not specified send single message\n\-d   - debug On\n\       schaltet zusaetzlich Debugging im Treiber an/aus\n\-b baudrate (Standard uses value of /proc/sys/Can/baud)\n\-D dev use /dev/dev/{can0,can1,can2,can3} (real nodes, std: can1)\n\""\-t type \n\   1 Stresstest f黵 Knoten, Sendet Bursts von kurzen rtr messages\n\   2 transmit bursts of 5 data frames, same ID\n\   3 transmit bursts of 5 data frames, different ID\n\     frame 5 contains counter variable\n\   4 as without this option, but increments CAN-ID with each message\n\   10 transmit bursts of 9 data frames, used for communication verification\n\   11 transmit -T number of frames as fast as possible, write(fd, buf, 1)\n\      if transmit buffer is full, sleep for -s ms time. \n\      if == 0: don't sleep, poll\n\      after every frame the message-id will be incremented\n\   12 same as 11\n\      but the message-id is constant and the databytes will be incremented\n\   30 transmit consecutive messages with the same id\n\      -T specifies the number of messages in this burst\n\   31 same as 12 but 8 data bytes\n\      -T specifies the muber of messages in this burst\n\   40 data for java applet example (sinus data)\n\-R   reset CAN Controller only, exit() program after reset\n\-T   number of bursts, time distance -s n (for -t<n>)\n\-V   print program version\n\\n\";    fprintf(stderr, "usage: %s [options] [id [ byte ..]]\n", s);    fprintf(stderr, usage_text);}/* test1:   - Data message 8 byte   - RTR  Message 0 byte   - RTR  Message 1 byte   - RTR  Message 2 byte*/void test1(void){long int test_count = 0;canmsg_t tm[4];int ret;    tm[0].id = 100;    tm[0].cob = 0;    tm[0].length = 8;    tm[0].flags = 0;    if (extd) {	tm[0].flags |= MSG_EXT;    }    tm[0].data[0] = 0x55;    tm[0].data[1] = 2;    tm[0].data[2] = 3;    tm[0].data[3] = 4;    tm[0].data[4] = 5;    tm[0].data[5] = 6;    tm[0].data[6] = 7;    tm[0].data[7] = 0xaa;    tm[1].id = message.id;    tm[1].cob = 0;    tm[1].length = 0;    tm[1].flags = MSG_RTR;    if (extd) {	tm[1].flags |= MSG_EXT;    }    tm[2].id = message.id;    tm[2].cob = 0;    tm[2].length = 1;    tm[2].flags = MSG_RTR;    if (extd) {	tm[2].flags |= MSG_EXT;    }    tm[3].id = message.id;    tm[3].cob = 0;    tm[3].length = 2;    tm[3].flags = MSG_RTR;    if (extd) {	tm[3].flags |= MSG_EXT;    }    do {	ret = write(can_fd, &tm[0], 4);	if (ret == -1) {	    perror("write error");	    usleep(errwaittime); 	    continue;	} else if (ret == 0) {	    printf("transmit timed out\n");	    usleep(errwaittime); 	    continue;	} else {	    if ( debug == TRUE ) {		printf("transmitted %d\n", ret);	    }	}		test_count++;		if(endless != 1) {	    if (test_count == test_count_soll) {		break;	    }	}	if (sleeptime > 0 )  {	    usleep(sleeptime);	}    }    while (1);    sleep(1);}/* test2:   - Data message 8 byte id= default or command line   - Data message 8 byte  "   - Data message 0 byte  "   - Data message 8 byte  "   - Data message 4 byte  "*/void test2(void){long int test_count = 0;canmsg_t tm[5];int ret;unsigned int cnt = 0;    tm[0].id = message.id;    tm[0].cob = 0;    tm[0].length = 8;    tm[0].flags = 0;    if (extd) {	tm[0].flags |= MSG_EXT;    }    tm[0].data[0] = 0x55;    tm[0].data[1] = 2;    tm[0].data[2] = 3;    tm[0].data[3] = 4;    tm[0].data[4] = 5;    tm[0].data[5] = 6;    tm[0].data[6] = 7;    tm[0].data[7] = 0xaa;    tm[1].id = message.id;    tm[1].cob = 0;    tm[1].length = 8;    tm[1].flags = 0;    if (extd) {	tm[1].flags |= MSG_EXT;    }    tm[1].data[0] = 0xaa;    tm[1].data[1] = 7;    tm[1].data[2] = 6;    tm[1].data[3] = 5;    tm[1].data[4] = 4;    tm[1].data[5] = 3;    tm[1].data[6] = 2;    tm[1].data[7] = 0x55;    tm[2].id = message.id;    tm[2].cob = 0;    tm[2].length = 0;    tm[2].flags = 0;    if (extd) {	tm[2].flags |= MSG_EXT;    }    tm[3].id = message.id;    tm[3].cob = 0;    tm[3].length = 8;    tm[3].flags = 0;    if (extd) {	tm[3].flags |= MSG_EXT;    }    tm[3].data[0] = 0x55;    tm[3].data[1] = 2;    tm[3].data[2] = 3;    tm[3].data[3] = 4;    tm[3].data[4] = 5;    tm[3].data[5] = 6;    tm[3].data[6] = 7;    tm[3].data[7] = 0xaa;    tm[4].id = message.id;    tm[4].cob = 0;    tm[4].length = 4;    tm[4].flags = 0;    /* currently data[] starts at offset 22 within canmsg_t     * Therfore a misalignment bus error is generated on some     * targets where a 4 byte value like cnt must be 4 byte aligned.     * The following code does not work in this machines     *     * *(unsigned int *)&tm[4].data[0] = cnt++     */         *(unsigned short *)&tm[4].data[0] = cnt & 0xffff;    *(unsigned short *)&tm[4].data[2] = (cnt & 0xfffff) >> 16;    cnt++;    if (extd) {	tm[4].flags |= MSG_EXT;    }    do {	ret = write(can_fd, &tm[0], 5);	if (ret == -1) {	    perror("write error");	    usleep(errwaittime); 	    continue;	} else if (ret == 0) {	    printf("transmit timed out\n");	    usleep(errwaittime); 	    continue;	} else {	    if ( debug == TRUE ) {		printf("transmitted %d\n", ret);	    }	}	/* *(unsigned int *)&tm[4].data[0] = cnt++; */	*(unsigned short *)&tm[4].data[0] = cnt & 0xffff;	*(unsigned short *)&tm[4].data[2] = (cnt & 0xfffff) >> 16;	cnt++;		test_count++;	if(endless != 1) {	    if (test_count == test_count_soll) {		break;	    }	}	if ( sleeptime > 0 ) {	    usleep(sleeptime);	}    }    while(1);    sleep(1);}/* test3:   - Data message 8 byte id= default or command line   - Data message 8 byte  "   - Data message 0 byte  "   - Data message 8 byte  "   - Data message 4 byte  "   - data with 4 byte counter*/void test3(void){long int test_count = 0;canmsg_t tm[5];int ret;unsigned int cnt = 0;struct timespec req;/* struct timespec rem; */#define ID_OFFSET 0x10 		/* used for the following messages */    tm[0].id = message.id;    tm[0].cob = 0;    tm[0].length = 8;    tm[0].flags = 0;    if (extd) {	tm[0].flags |= MSG_EXT;    }    tm[0].data[0] = 0x55;    tm[0].data[1] = 2;    tm[0].data[2] = 3;    tm[0].data[3] = 4;    tm[0].data[4] = 5;    tm[0].data[5] = 6;    tm[0].data[6] = 7;    tm[0].data[7] = 0xaa;    tm[1].id = tm[0].id + ID_OFFSET;    tm[1].cob = 0;    tm[1].length = 8;    tm[1].flags = 0;    if (extd) {	tm[1].flags |= MSG_EXT;    }    tm[1].data[0] = 0xaa;    tm[1].data[1] = 7;    tm[1].data[2] = 6;    tm[1].data[3] = 5;    tm[1].data[4] = 4;    tm[1].data[5] = 3;    tm[1].data[6] = 2;    tm[1].data[7] = 0x55;    tm[2].id = tm[1].id + ID_OFFSET;    tm[2].cob = 0;    tm[2].length = 0;    tm[2].flags = 0;    if (extd) {	tm[2].flags |= MSG_EXT;    }    tm[3].id = tm[2].id + ID_OFFSET;    tm[3].cob = 0;    tm[3].length = 8;    tm[3].flags = 0;    if (extd) {	tm[3].flags |= MSG_EXT;    }    tm[3].data[0] = 0x55;    tm[3].data[1] = 2;    tm[3].data[2] = 3;    tm[3].data[3] = 4;    tm[3].data[4] = 5;    tm[3].data[5] = 6;    tm[3].data[6] = 7;    tm[3].data[7] = 0xaa;    tm[4].id = tm[3].id + ID_OFFSET;    tm[4].cob = 0;    tm[4].length = 4;    tm[4].flags = 0;    /* *(unsigned int *)&tm[4].data[0] = cnt++; */    *(unsigned short *)&tm[4].data[0] = cnt & 0xffff;    *(unsigned short *)&tm[4].data[2] = (cnt & 0xfffff) >> 16;    cnt++;    if (extd) {	tm[4].flags |= MSG_EXT;    }#if 1	/*	If  the process is scheduled under a real-time policy like	SCHED_FIFO or SCHED_RR, then pauses of up to 2 ms will  be	performed as busy waits with microsecond precision.	*/	req.tv_sec = sleeptime / (1000*1000);	req.tv_nsec = (sleeptime % (1000*1000)) * 1000;	if ( debug == TRUE ) {	    printf("Sleep %ld.%09ld\n", req.tv_sec, req.tv_nsec);	}#endif    do {	ret = write(can_fd, &tm[0], 5);	if (ret == -1) {	    perror("write error");	    usleep(errwaittime); 	    continue;	} else if (ret == 0) {	    printf("transmit timed out\n");	    usleep(errwaittime); 	    continue;	} else {	    if ( debug == TRUE ) {		printf("transmitted %d\n", ret);	    }	}	*(unsigned short *)&tm[4].data[0] = cnt & 0xffff;	*(unsigned short *)&tm[4].data[2] = (cnt & 0xfffff) >> 16;	cnt++;		test_count++;		if(endless != 1) {	    if (test_count == test_count_soll) {		break;	    }	}	if ( debug == TRUE ) {	    showCANStat(can_fd);	}	/* now read the rx queue, it may contain	 * frames with id -1, which signals an error */	do {	    i = read(can_fd, &rx, 1);	    if(-1 == i) {		perror("read error");	    } else if((i > 0) && (CANDRIVERERROR == rx.id)) {		displayerror(&rx);	    }	}	while(i > 0);	if (sleeptime > 0) {	    usleep(sleeptime);    /* F黵 die Verwendung von nanosleep muss noch etwas getan werden.       Nanosleep benutzt bei Zeiten < 2 ms eine echte busy loop im kernel,       d.h. mit "-s 1" geht bei endlosschleifen dann gar nichts mehr       au遝r dem gro遝n Roten Knopf am Rechner.       Falls also implementieren, dann auf kurze Schleifen, max 10 ode       so begrenzen, damit kann es aber m鰃lich sein mal schnell        meherer telegramme hineterinender zu senden, dann wieder usleep()       nehmen ...     */	    /* nanosleep(&req, &rem); */	}    }    while(1);    sleep(1);}/* Test 10You can see the output in hexformat by calling             +- select test type             |      +-- send messages to             |      |        +- number of sequences a 9 messages ( 2 * 9)             |      |        |     +- time in ms between sequences             |      |        |     |$ can_send -t10 -D stdout -T 2 -s 100 | od -t x1 -w32                                         |   |     |                                         |   |     +-- 32 bytes per line ==                                          |   |         one message                                         |   +-- type hex, one byte                                         +- use "object dump" for display*/#define SEQN	9 /* number of messages for one sequence, one write call */void update_seq(canmsg_t *m){int i;    /* calculate next sequence */    /* first: new message id */				/* ((2^11)/9)*9 - 1   				 * ((2*29)/9)*9 - 1				 */    if (    ( extd && (m->id > (536870906 - SEQN))) 	 || (!extd && (m->id > (     2042 - SEQN)))  ) {	/* reset message id to 0 */	for (i = 0; i < SEQN; i++) {	    (m + i)->id = i;	}    } else {	if ( debug == TRUE ) {	    printf(" new id %ld\n", m->id + SEQN);	}	/* not wrapped, increment message id */	for (i = 0; i < SEQN; i++) {		(m + i)->id += SEQN;	}    }    /* now update data bytes with counter value */    for (i = 0; i < SEQN; i++) {        unsigned char *p;        p = &((m + i)->data[0]);#if defined(EMBED)	*(unsigned short *)p += 1;#else	*(unsigned long long *)p += 1;#endif    }}void test10(void){long int test_count = 0;int ret, i;/* unsigned int cnt = 0; */int fac = 1;canmsg_t tm[SEQN] =  {    /*  f, cob,  id, time,   l,   data[8]                     */    {  0 ,   0,   0, {0 , 0},  0, { 0, 0, 0, 0, 0, 0, 0, 0} },     {  0 ,   0,   1, {0 , 0},  1, { 0, 0, 0, 0, 0, 0, 0, 0} },     {  0 ,   0,   2, {0 , 0},  2, { 0, 0, 0, 0, 0, 0, 0, 0} },     {  0 ,   0,   3, {0 , 0},  3, { 0, 0, 0, 0, 0, 0, 0, 0} },     {  0 ,   0,   4, {0 , 0},  4, { 0, 0, 0, 0, 0, 0, 0, 0} },     {  0 ,   0,   5, {0 , 0},  5, { 0, 0, 0, 0, 0, 0, 0, 0} },     {  0 ,   0,   6, {0 , 0},  6, { 0, 0, 0, 0, 0, 0, 0, 0} },     {  0 ,   0,   7, {0 , 0},  7, { 0, 0, 0, 0, 0, 0, 0, 0} },     {  0 ,   0,   8, {0 , 0},  8, { 0, 0, 0, 0, 0, 0, 0, 0} }};    if (cstdout == TRUE) {	/* use stdout */	fac = sizeof(canmsg_t);    }    if (extd) {	/* set the extd flag in all messages */	for (i = 0; i < SEQN; i++) {		tm[i].flags |= MSG_EXT;	}    }    if ( debug == TRUE ) {	printf("using test10 with extd = %s\n", extd ? "TRUE" : "FALSE");    }    /* loop forever if sleeptime > 0 */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -