📄 irda-test.c
字号:
/************************************************ * serial communication irDA demo * by Zou jian guo <ah_zou@163.com> * 2005-01-26 * VERSION 0.10*************************************************/#include <termios.h>#include <stdio.h>#include <unistd.h>#include <fcntl.h>#include <sys/signal.h>#include <pthread.h>#define BAUDRATE B115200#define COM0 "/dev/ttyS0"#define COM1 "/dev/ttyS1"#define COM2 "/dev/ttyS2"#define COM3 "/dev/ttyS3"#define COM4 "/dev/ttyS4"#define ENDMINITERM 27 /* ESC to quit miniterm */#define FALSE 0#define TRUE 1#define IRDA_RX_DISABLE 1#define IRDA_RX_ENABLE 2#define IRDA_TX_DISABLE 3#define IRDA_TX_ENABLE 4static int STOP=FALSE;static int fd,fd_irda;void child_handler(int s){ printf("stop!!!\n"); STOP=TRUE;}/*******************************************************************/void* keyboard(void * data){ int c; for (;;){ c=getchar(); if( c== ENDMINITERM){ STOP=TRUE; break ; } } return NULL;}/*******************************************************************//* IRDA input handler */void* receive(void * data){ unsigned char c,buf[255]; int status; printf("read IRDA\n"); ioctl(fd_irda,IRDA_TX_DISABLE,NULL);//DISABLE TX ioctl(fd_irda,IRDA_RX_ENABLE,NULL);//ENABLE RX while (STOP==FALSE) { //read(fd_irda,buf,80); /* com port */ status=read(fd_irda,&c,1); /* com port */ //usleep(100); if(status&1){ write(1,&c,1); /* stdout */ } } printf("exit from reading IRDA\n"); return NULL; }/*******************************************************************/void* send(void * data){ int c='0'; printf("send data\n"); ioctl(fd_irda,IRDA_RX_DISABLE,NULL); ioctl(fd_irda,IRDA_TX_ENABLE,NULL); while (STOP==FALSE) /* modem input handler */ { c++; if(c>127){ c='\n'; } c %= 255; usleep(50000); write(fd,&c,1); /* stdout */ write(1,&c,1); //usleep(1000000); } return NULL; }/*******************************************************************/int main(int argc,char** argv){ struct termios oldtio,newtio,oldstdtio,newstdtio; struct sigaction sa; int ok,num; pthread_t th_a, th_b, th_c; void * retval;// printf("argc=%d\n",argc); if(argc>1 && strcmp(argv[1],"?")==0){ printf("Usage: %s w|r \n",argv[0]); printf(" w : write to ttyS\n"); printf(" r : read from ttyS \n"); printf(" no 'w|r' is both read and write\n"); return 0; } fd = open("/dev/ttyS0", O_RDWR ); if (fd <0) { perror("open ttyS0 fail!\n"); exit(-1); } tcgetattr(0,&oldstdtio); tcgetattr(fd,&oldtio); /* save current modem settings */ tcgetattr(fd,&newstdtio); /* get working stdtio */// newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;/*ctrol flag*/ newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;/*ctrol flag*/ newtio.c_iflag = IGNPAR; /*input flag*/ newtio.c_oflag = 0; /*output flag*/ newtio.c_lflag = 0; /*echo flag*/ newtio.c_cc[VMIN]=1; newtio.c_cc[VTIME]=0; /* now clean the modem line and activate the settings for modem */ tcflush(fd, TCIFLUSH); tcsetattr(fd,TCSANOW,&newtio);/*set attrib */ sa.sa_handler = child_handler; sa.sa_flags = 0; sigaction(SIGCHLD,&sa,NULL); /* handle dying child */ pthread_create(&th_a, NULL, keyboard, 0); fd_irda = open("/dev/irda/0", O_RDWR ); if (fd_irda <0) { perror("open /dev/irda/0 fail!\n"); // exit(-1); } if(argc>1){ if(strcmp(argv[1],"r")==0){ pthread_create(&th_b, NULL, receive, 0); } if(strcmp(argv[1],"w")==0){ pthread_create(&th_c, NULL, send, 0); } } else{ pthread_create(&th_b, NULL, receive, 0); pthread_create(&th_c, NULL, send, 0); } pthread_join(th_a, &retval); pthread_join(th_b, &retval); pthread_join(th_c, &retval); tcsetattr(fd,TCSANOW,&oldtio); /* restore old modem setings */ tcsetattr(0,TCSANOW,&oldstdtio); /* restore old tty setings */ close(fd); exit(0); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -