📄 can_test.c
字号:
/* can_test.c * test program for can200 driver */#include <stdio.h>#include <unistd.h> /* read(), write(), ... */#include <fcntl.h> /* fcntl() */#include <asm/param.h> /* HZ */#include "can200.h"static int can_read( int can, can_msg_t *msg ){ int err; fcntl( can, F_SETFL, O_NONBLOCK ); err = read( can, msg, CAN_MSG_LEN ); fcntl( can, F_SETFL, 0 ); return err;}static int can_write( int can, can_msg_t *msg ){ int err; err = write( can, msg, CAN_MSG_LEN ); if ( err < 0 ) { perror( "can_write()" ); exit( err ); } return err;}void print_msg( can_msg_t *msg ){ static unsigned long starttime = 0; int len=0, iii; if ( 0 == starttime ) starttime = msg->timestamp; //printf( "%3d ", msg->pad ); printf( "%.2fs ", (msg->timestamp - starttime)/(float) HZ ); if ( msg->info & CAN_INFO_EFF ) /* extended frame format */ printf( "%02x %02x%02x%02x%02x ", msg->info, msg->id0, msg->id1, msg->id2, msg->id3 ); else /* standard frame format */ printf( "%02x %02x%02x ", msg->info, msg->id0, msg->id1 ); if ( msg->info & CAN_INFO_RTR ) len = 0; else len = msg->info & CAN_INFO_LEN_MASK; if ( len > 8 ) len = 8; for ( iii = 0; iii < len; iii++ ) printf( "%02x", msg->data[iii] ); printf( "\n" );}int main( int argc, char **argv ){ int can; can_msg_t msg; can = open( "can20k", O_RDWR ); if ( can < 0 ) { perror( "can_test open()" ); return can; } while( 1 ) { usleep( 100000 ); while ( can_read( can, &msg ) > 0 ) { print_msg( &msg ); } } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -