linux基础复习(7)串口应用开发
B115200); cfsetospeed(newtio, B115200); break; case 460800: cfsetispeed(newtio, B460800); cfsetospeed(newtio, B460800); break; default: cfsetispeed(newtio, B9600); cfsetospeed(newtio, B9600); break; } /*设置停止位*/ if( nStop == 1 ) newtio.c_cflag = ~CSTOPB; else if ( nStop == 2 ) newtio.c_cflag |= CSTOPB; /*设置等待时间和最小接收字符*/ newtio.c_cc[VTIME] = 0; newtio.c_cc[VMIN] = 0; /*处理未接收字符*/ tcflush(fd,TCIFLUSH); /*激活新配置*/ if((tcsetattr(fd,TCSANOW,newtio))!=0) { perror(com set error); return -1; } printf(set done!\n); return 0; } [b]串口使用详解[/b] 在配置完串口的相关属性后,就可对串口进行打开,读写操作了。其使用方式与文件操作一样,区别在于串口是一个终端设备。 [b]打开串口[/b] fd = open( /dev/ttyS0, O_RDWR|O_NOCTTY|O_NDELAY); Open函数中除普通参数外,另有两个参数O_NOCTTY和O_NDELAY。 O_NOCTTY: 通知linix系统,这个程序不会成为这个端口的控制终端。 O_NDELAY: 通知linux系统不关心DCD信号线所处的状态(端口的另一端是否激活或者停止)。 然后,恢复串口的状态为阻塞状态,用于等待串口数据的读入。用fcntl函数: fcntl(fd, F_SETFL, 0); 接着,测试打开的文件描述府是否引用一个终端设备,以进一步确认串口是否正确打开。 isatty(STDIN_FILENO); 串口的读写与普通文件一样,使用read,write函数。 read(fd,buff,8); write(fd,buff,8); 实例 #i nclude stdio.h> #i nclude string.h> #i nclude sys/types.h> #i nclude errno.h> #i nclude sys/stat.h> #i nclude fcntl.h> #i nclude unistd.h> #i nclude termios.h> #i nclude stdlib.h> int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop) { struct termios newtio,oldtio; if ( tcgetattr( fd,oldtio) != 0) { perror(SetupSerial 1); return -1; } bzero( newtio, sizeof( newtio ) ); newtio.c_cflag |= CLOCAL | CREAD; newtio.c_cflag = ~CSIZE; switch( nBits ) { case 7: newtio.c_cflag |= CS7; break; case 8: newtio.c_cflag |= CS8; break; } switch( nEvent ) { case 'O': newtio.c_cflag |= PARENB; newtio.c_cflag |= PARODD; newtio.c_iflag |= (INPCK | ISTRIP); break; case 'E': newtio.c_iflag |= (INPCK | ISTRIP); newtio.c_cflag |= PARENB; newtio.c_cflag = ~PARODD; break; case 'N': newtio.c_cflag = ~PARENB; break; } switch( nSpeed ) { case 2400: cfsetispeed(newtio, B2400); cfsetospeed(newtio, B2400); break; case 4800: cfsetispeed(newtio, B4800); cfsetospeed(newtio, B4800); break; case 9600: cfsetispeed(newtio, B9600); cfsetospeed(newtio, B9600); break; case 115200: cfsetispeed(newtio, B115200); cfsetospeed(newtio, B115200); break; default: cfsetispeed(newtio, B9600); cfsetospeed(newtio, B9600); break; } if( nStop == 1 ) newtio.c_cflag = ~CSTOPB; else if ( nStop == 2 ) newtio.c_cflag |= CSTOPB; newtio.c_cc[VTIME] = 0; newtio.c_cc[VMIN] = 0; tcflush(fd,TCIFLUSH); if((tcsetattr(fd,TCSANOW,newtio))!=0) { perror(com set error); return -1; } printf(set done!\n); return 0; } int open_port(int fd,int comport) { char *dev[]={/dev/ttyS0,/dev/ttyS1,/dev/ttyS2}; long vdisable; if (comport==1) { fd = open( /dev/ttyS0, O_RDWR|O_NOCTTY|O_NDELAY); if (-1 == fd){ perror(Can't Open Serial Port); return(-1); } else printf(open ttyS0 .....\n); } else if(comport==2) { fd = open( /dev/ttyS1, O_RDWR|O_NOCTTY|O_NDELAY); if (-1 == fd){ perror(Can't Open Serial Port); return(-1); } else printf(open ttyS1 .....\n); } else if (comport==3) { fd = open( /dev/ttyS2, O_RDWR|O_NOCTTY|O_NDELAY); if (-1 == fd){ perror(Can't Open Serial Port); return(-1); } else printf(open ttyS2 .....\n); } if(fcntl(fd, F_SETFL, 0)0) printf(fcntl failed!\n); else printf(fcntl=%d\n,fcntl(fd, F_SETFL,0)); if(isatty(STDIN_FILENO)==0) printf(standard input is not a terminal device\n); else printf(isatty success!\n); printf(fd-open=%d\n,fd); return fd; } int main(void) { int fd; int nread,i; char buff[]=Hello\n; if((fd=open_port(fd,1))0){ perror(open_port error); return; } if((i=set_opt(fd,1
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)