主要函数intopenport(char*Dev)//打开串口intsetport(intfd,intbaud,intdatabits,intstopbits,intparity)//设置串口,波特率,数据位,停止位,校验intreadport(intfd,char*buf,intlen,intmaxwaittime)//读数据,参数为串口,BUF,长度,超时时间intwriteport(intfd,char*buf,intlen)//发送数据voidclearport(intfd)//如果出现数据与规约不符合,可以调用这个函数来刷新串口读写数据如果有BUG,请大家及时回复给我,EMAIL:41063473@QQ.COM。#includestdio.h#includestring.h#includeunistd.h#includefcntl.h#includeerrno.h#includetermios.h#includesys/time.hintopenport(char*Dev){intfd=open(Dev,O_RDWR|O_NOCTTY|O_NDELAY);if(-1==fd){perror(Can''tOpenSerialPort);return-1;}elsereturnfd;}intsetport(intfd,intbaud,intdatabits,intstopbits,intparity){intbaudrate;structtermiosnewtio;switch(baud){case300:baudrate=B300;break;case600:baudrate=B600;break;case1200:baudrate=B1200;break;case2400:baudrate=B2400;break;case4800:baudrate=B4800;break;case9600:baudrate=B9600;break;case19200:baudrate=B19200;break;case38400:baudrate=B38400;break;default:baudrate=B9600;break;}tcgetattr(fd,&newtio);bzero(&newtio,sizeof(newtio));//settingc_cflagnewtio.c_cflag&=~CSIZE;switch(databits)/*设置数据位数*/{case7:newtio.c_cflag|=CS7;//7位数据位break;case8:newtio.c_cflag|=CS8;//8位数据位break;default:newtio.c_cflag|=CS8;break;}switch(parity)//设置校验{case'n':case'N':newtio.c_cflag&=~PARENB;/*Clearparityenable*/newtio.c_iflag&=~INPCK;/*Enableparitychecking*/break;case'o':case'O':newtio.c_cflag|=(PARODD|PARENB);/*设置为奇效验*/newtio.c_iflag|=INPCK;/*Disnableparitychecking*/break;case'e':case'E':newtio.c_cflag|=PARENB;/*Enableparity*/newtio.c_cflag&=~PARODD;/*转换为偶效验*/newtio.c_iflag|=INPCK;/*Disnableparitychecking*/break;case'S':case's':/*asnoparity*/newtio.c_cflag&=~PARENB;newtio.c_cflag&=~CSTOPB;break;default:newtio.c_cflag&=~PARENB;/*Clearparityenable*/newtio.c_iflag&=~INPCK;/*Enableparitychecking*/break;}switch(stopbits)//设置停止位{case1:newtio.c_cflag&=~CSTOPB;//1break;case2:newtio.c_cflag|=CSTOPB;//2break;default:newtio.c_cflag&=~CSTOPB;break;}newtio.c_cc[VTIME]=0;newtio.c_cc[VMIN]=0;newtio.c_cflag|=(CLOCAL|CREAD);newtio.c_oflag|=OPOST;newtio.c_iflag&=~(IXON|IXOFF|IXANY);cfsetispeed(&newtio,baudrate);cfsetospeed(&newtio,baudrate);tcflush(fd,TCIFLUSH);if(tcsetattr(fd,TCSANOW,&newtio)!=0){perror(SetupSerial3);return-1;}return0;}intreadport(intfd,char*buf,intlen,intmaxwaittime)//读数据,参数为串口,BUF,长度,超时时间{intno=0;intrc;intrcnum=len;structtimevaltv;fd_setreadfd;tv.tv_sec=maxwaittime/1000;//SECONDtv.tv_usec=maxwaittime%1000*1000;//USECONDFD_ZERO(&readfd);FD_SET(fd,&readfd);rc=select(fd+1,&readfd,NULL,NULL,&tv);if(rc0){while(len){rc=read(fd,&buf[no],1);if(rc0)no=no+1;len=len-1;}if(no!=rcnum)return-1;//如果收到的长度与期望长度不一样,返回-1returnrcnum;//收到长度与期望长度一样,返回长度}else{return-1;}return-1;}intwriteport(intfd,char*buf,intlen)//发送数据{write(fd,buf,len);}voidclearport(intfd)//如果出现数据与规约不符合,可以调用这个函数来刷新串口读写数据{tcflush(fd,TCIOFLUSH);}main(){intfd,rc,i,ret;unsignedcharrbuf[256];unsignedcharwbuf[256]=;for(i=0;i256;i++)wbuf[i]=i;char*dev=/dev/ttyS0;//串口号/dev/ttyS0对应于串口1fd=openport(dev);//打开串口if(fd0){ret=setport(fd,4800,8,1,'o');//设置串口,波特率,数据位,停止位,校验if(ret0){printf(Can'tSetSerialPort!/n);exit(0);}}else{printf(Can'tOpenSerialPort!/n);exit(0);}while(1){rc=readport(fd,rbuf,5,500);//读取5个字节,超时时间为500毫秒if(rc0){writeport(fd,wbuf,rc);printf(recv:%d/n,rc);for(i=0;irc;i++)printf(%02x,rbuf[i]);printf(/n);}elseprintf(recvnone/n);}close(fd);}