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

📄 can_send.c

📁 can4linux-3.5.3.gz can4 linux
💻 C
📖 第 1 页 / 共 3 页
字号:
    do {        int seq   = SEQN *fac;        int start = 0;;	while(seq) {	    if ( debug == TRUE ) {		 printf("send %d/%d, ", start, seq);	    }	    ret = write(can_fd, &tm[start], seq);	    if (ret == -1) {		perror("write error");		exit(2);	    } else if (ret == 0) {		printf("transmit timed out\n");		/* in case of polling (sleeptime==0), wait 0 or 1000us */		/* usleep(sleeptime);  */		usleep(errwaittime); 		continue;	    } else {		if ( debug == TRUE ) {		    printf("transmitted %d\n", ret);		}	    }	    if ( debug == TRUE ) {		showCANStat(can_fd);	    }	    seq -= ret;	    start += ret;	    if ( seq >= 0 && sleeptime) {	    	usleep(10000); 	    }	}		test_count++;	if (endless != 1) {	    /* is it enough ? than leave loop */	    if (test_count == test_count_soll) {		break;	    }	}        update_seq(&tm[0]);	/* if neccessary sleep */	if (sleeptime > 0) {	    usleep(sleeptime);	}    }    while(1);    /* before closing the driver, give it a chance to transmit    messages in tx buffer */    usleep(1000000);}/* using the global defined message, that can be changed via command line,the function tries to reach a specified bus-load- use 20 ms sleep time or larger for low bus loads*/void test20(void){int n, i, test_count;int run;int ret;    /* first assume we have only 11 bit and 8 data bytes */    /* 1 Message = 120 bits */    /* number of messages for 100 % */    /* time   / (bits/m   * time_one_bit) */    n = 20000 / (120 * (1000 / baud));      n = n * load / 100;    test_count = 0;    printf("send %ld messages, %d messages  every 20 ms cycle \n",    	test_count_soll, n);    /* printf("soll %ld \n", test_count_soll); */    sleeptime = 19000;    run = 0;    run = TRUE;    while(run) {	for(i = 0; i < n; i++) {	    ret = write(can_fd, &message, 1);	    if (ret == -1) {		perror("write error; ");	    } else if (ret == 0) {		printf("Node %d: transmit time out\n", node);	    } else {		if ( debug == TRUE ) {		     printf("Node %d: transmit message %ld\n", node, message.id );		 }	    }	    /* endless??? 	     *  2007_09_28 - ro	     */	    	    if (test_count_soll && (++test_count == test_count_soll)) {		run = FALSE;		break;	    }	    /* printf("  count %d \n", test_count); */	}	usleep(sleeptime);    }}void test11(void){canmsg_t message;long int test_count = 0;int ret;    /* our default 8 byte message */    message.id      = 0;    message.cob     = 0;    message.flags   = 0;    message.length  = 8;    message.data[0] = 0x55;    message.data[1] = 2;    message.data[2] = 3;    message.data[3] = 4;    message.data[4] = 5;    message.data[5] = 6;    message.data[6] = 7;    message.data[7] = 0xaa;    if (extd) {	message.flags |= MSG_EXT;    }    /* else */    /* the default can_send, simply send a message */    /* no special test, send the normal message, (old behaviouur) */    do {    again:        if ( debug == TRUE ) {	     printf(" transmit message %ld\n", message.id );	 }	ret = write(can_fd, &message, 1);	if (ret == -1) {	    /* int e = errno; */	    perror("write error");	    /* if ( e == ENOSPC) { */		if (sleeptime) { usleep(sleeptime);  }		/* continue; */		goto again;	    /* } */	} else if (ret != 1) {	    /* write buffer full? try it again */	    /* fprintf(stderr, "transmitted %d from 1\n", ret); */	    /* Only if buffer full, sleep a while if specified */		if (sleeptime) { usleep(sleeptime);  }		goto again;	} else {	}	if ( debug == TRUE ) {	     showCANStat(can_fd);	}	message.id++ ;		test_count++;	if (endless != 1) {	    /* is it enough ? than leave loop */	    if (test_count == test_count_soll) {		break;	    }	}    }    while(1);    usleep(1000000);}void test12(void){long int test_count = 0;int ret;    /* message.id      = 0; */ /* default message id */    message.cob     = 0;    message.flags   = 0;    if (extd) {	message.flags |= MSG_EXT;    }    message.length  = 4;    message.data[0] = 0;    message.data[1] = 0;    message.data[2] = 0;    message.data[3] = 0;    message.data[4] = 0;    message.data[5] = 0;    message.data[6] = 0;    message.data[7] = 0;    /* else */    /* the default can_send, simply send a message */    /* no special test, send the normal message, (old behaviouur) */    do {    	message.data[0] = test_count % 0x100;    	message.data[1] = (test_count >> 8) % 0x100;    	message.data[2] = (test_count >> 16) % 0x100;    	message.data[3] = (test_count >> 24) % 0x100;    again:        if ( debug == TRUE ) {	     printf(" %6ld: transmit message %ld\n",	     	test_count, message.id );	 }#if 0	gettimeofday(&tstamp, NULL);	printf("%12lu.%06lu >", tstamp.tv_sec, tstamp.tv_usec);	showCANStat(can_fd);#endif	ret = write(can_fd, &message, 1);#if 0	gettimeofday(&tstamp, NULL);	printf("%12lu.%06lu <", tstamp.tv_sec, tstamp.tv_usec);	showCANStat(can_fd);	fputs("\n", stdout);#endif	if (ret == -1) {	    /* int e = errno; */	    perror("write error");	    /* if ( e == ENOSPC) { */		if (sleeptime) { usleep(sleeptime);  }		/* continue; */		goto again;	    /* } */	} else if (ret != 1) {	    /* fprintf(stdout, "transmitted %d from 1\n", ret); */		if (sleeptime) { usleep(sleeptime);  }		goto again;	} else {	}	if ( debug == TRUE )	     showCANStat(can_fd);	if (sleeptime) { usleep(sleeptime);  }		test_count++;	if (endless != 1) {	    /* is it enough ? than leave loop */	    if (test_count == test_count_soll) {		break;	    }	}    }    while(1);    usleep(1000000);}void test30(void){/* long int test_count = 0; *//* unsigned int cnt = 0; */#define MAXTESTMSG 5canmsg_t tm[MAXTESTMSG];int ret; /* shold be malloced  here according to the variable 	    test_count_soll    and all messages initialized in a loop    */    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 = 8;    tm[2].flags = 0;    if (extd) {	tm[2].flags |= MSG_EXT;    }    tm[2].data[0] = 0xaa;    tm[2].data[1] = 7;    tm[2].data[2] = 6;    tm[2].data[3] = 5;    tm[2].data[4] = 4;    tm[2].data[5] = 3;    tm[2].data[6] = 2;    tm[2].data[7] = 0x55;    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] = 0xaa;    tm[3].data[1] = 7;    tm[3].data[2] = 6;    tm[3].data[3] = 5;    tm[3].data[4] = 4;    tm[3].data[5] = 3;    tm[3].data[6] = 2;    tm[3].data[7] = 0x55;    tm[4].id = message.id;    tm[4].cob = 0;    tm[4].length = 8;    tm[4].flags = 0;    if (extd) {	tm[4].flags |= MSG_EXT;    }    tm[4].data[0] = 0xaa;    tm[4].data[1] = 7;    tm[4].data[2] = 6;    tm[4].data[3] = 5;    tm[4].data[4] = 4;    tm[4].data[5] = 3;    tm[4].data[6] = 2;    tm[4].data[7] = 0x55;    if(test_count_soll > MAXTESTMSG)	    test_count_soll = MAXTESTMSG;    do {	ret = write(can_fd, &tm[0], test_count_soll);	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);	    }	}	if ( sleeptime > 0 ) {	    usleep(sleeptime);	}    }    while(1);    sleep(1);}void test31(void){long int test_count = 0;int ret;    /* message.id      = 0; */ /* default message id */    message.cob     = 0;    message.flags   = 0;    if (extd) {	message.flags |= MSG_EXT;    }    message.length  = 8;    message.data[0] = 1;    message.data[1] = 2;    message.data[2] = 3;    message.data[3] = 4;    message.data[4] = 5;    message.data[5] = 6;    message.data[6] = 7;    message.data[7] = 8;    /* else */    /* the default can_send, simply send a message */    /* no special test, send the normal message, (old behaviouur) */    do {    	message.data[0] = test_count % 0x100;    	message.data[1] = (test_count >> 8) % 0x100;    	message.data[2] = (test_count >> 16) % 0x100;    	message.data[3] = (test_count >> 24) % 0x100;    again:        if ( debug == TRUE ) {	     printf(" %6ld: transmit message %ld\n",	     	test_count, message.id );	 }#if 0	gettimeofday(&tstamp, NULL);	printf("%12lu.%06lu >", tstamp.tv_sec, tstamp.tv_usec);	showCANStat(can_fd);#endif	ret = write(can_fd, &message, 1);#if 0	gettimeofday(&tstamp, NULL);	printf("%12lu.%06lu <", tstamp.tv_sec, tstamp.tv_usec);	showCANStat(can_fd);	fputs("\n", stdout);#endif	if (ret == -1) {	    /* int e = errno; */	    perror("write error");	    /* if ( e == ENOSPC) { */		if (sleeptime) { usleep(sleeptime);  }		/* continue; */		goto again;	    /* } */	} else if (ret != 1) {	    /* fprintf(stdout, "transmitted %d from 1\n", ret); */		if (sleeptime) { usleep(sleeptime);  }		goto again;	} else {	}	if ( debug == TRUE )	     showCANStat(can_fd);	if (sleeptime) { usleep(sleeptime);  }		test_count++;	if (endless != 1) {	    /* is it enough ? than leave loop */	    if (test_count == test_count_soll) {		break;	    }	}    }    while(1);    usleep(1000000);}void test40(void){/* long int test_count = 0; *//* unsigned int cnt = 0; */#if defined(X)canmsg_t tm;int ret;float  inc = 0;    tm.id = message.id;    tm.cob = 0;    tm.length = 8;    tm.flags = 0;    if (extd) {	tm.flags |= MSG_EXT;    }    tm.data[0] = 0;    tm.data[1] = 0;    tm.data[2] = 0;    tm.data[3] = 0;    tm.data[4] = 0;    tm.data[5] = 0;    tm.data[6] = 0;    tm.data[7] = 0;    do {	/* calc data  */	tm.data[0]  = 128 * (sin(inc + 0 * (M_2_PI/2)) + 1); 	tm.data[1]  = 128 * (sin(inc + 1 * (M_2_PI/2)) + 1); 	tm.data[2]  = 128 * (sin(inc + 2 * (M_2_PI/2)) + 1); 	tm.data[3]  = 128 * (sin(inc + 3 * (M_2_PI/2)) + 1); 	tm.data[4]  = 128 * (sin(inc + 4 * (M_2_PI/2)) + 1); 	tm.data[5]  = 128 * (sin(inc + 5 * (M_2_PI/2)) + 1); 	tm.data[6]  = 128 * (sin(inc + 6 * (M_2_PI/2)) + 1); 	tm.data[7]  = 128 * (sin(inc + 7 * (M_2_PI/2)) + 1); 	ret = write(can_fd, &tm, 1);	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);	    }	}	inc += 0.1;	if ( sleeptime > 0 ) {	    usleep(sleeptime);	}    }    while(1);    sleep(1);#endif}

⌨️ 快捷键说明

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