linux 串口 read -1,linux的串口编程。read()读不出回车键

这是一个使用GCC编译的Linux串口通信程序,它实现了标准输入输出、错误处理和速度配置等功能。程序允许用户通过命令行参数指定设备、波特率等,并支持7位或8位数据位、十六进制模式和颜色输出。用户可以通过按三次ESC键退出程序。
摘要由CSDN通过智能技术生成

这有个友善的串口例程,参考下吧,用gcc编译可以在linux下用#include<stdio.h>#include<stdlib.h>#include<termio.h>#include<unistd.h>#include<fcntl.h>#include<getopt.h>#include<time.h>#include<errno.h>#include<string.h>intCommFd,TtyFd;staticvoidError(constchar*Msg){fprintf(stderr,"%s\n",Msg);fprintf(stderr,"strerror()is%s\n",strerror(errno));exit(1);}staticvoidWarning(constchar*Msg){fprintf(stderr,"Warning:%s\n",Msg);}staticintSerialSpeed(constchar*SpeedString){intSpeedNumber=atoi(SpeedString);#defineTestSpeed(Speed)if(SpeedNumber==Speed)returnB##SpeedTestSpeed(1200);TestSpeed(2400);TestSpeed(4800);TestSpeed(9600);TestSpeed(19200);TestSpeed(38400);TestSpeed(57600);TestSpeed(115200);TestSpeed(230400);Error("Badspeed");return-1;}staticvoidPrintUsage(void){fprintf(stderr,"comtest-interactiveprogramofcommport\n");fprintf(stderr,"press[ESC]3timestoquit\n\n");fprintf(stderr,"Usage:comtest[-ddevice][-ttty][-sspeed][-7][-c][-x][-o][-h]\n");fprintf(stderr,"-77bit\n");fprintf(stderr,"-xhexmode\n");fprintf(stderr,"-ooutputtostdouttoo\n");fprintf(stderr,"-cstdoutoutputusecolor\n");fprintf(stderr,"-hprintthishelp\n");exit(-1);}staticinlinevoidWaitFdWriteable(intFd){fd_setWriteSetFD;FD_ZERO(&WriteSetFD);FD_SET(Fd,&WriteSetFD);if(select(Fd+1,NULL,&WriteSetFD,NULL,NULL)<0){Error(strerror(errno));}}intsendUart(charc){WaitFdWriteable(CommFd);returnwrite(CommFd,&c,1);}charrecUart(){charc='\0';if(FD_ISSET(CommFd,&ReadSetFD)){if(read(CommFd,&c,1)==1)returnc;elseprintf("Nodatatoreceive.\n");}}intmain(intargc,char**argv){structtermiosTtyAttr;structtermiosBackupTtyAttr;intDeviceSpeed=B115200;intTtySpeed=B115200;intByteBits=CS8;constchar*DeviceName="/dev/ttyS0";constchar*TtyName="/dev/tty";intOutputHex=0;intOutputToStdout=0;intUseColor=0;printf("Nowwestart.\n");opterr=0;for(;;){intc=getopt(argc,argv,"d:s:t:7xoch");if(c==-1)break;switch(c){case'd':DeviceName=optarg;break;case't':TtyName=optarg;break;case's':if(optarg[0]=='d'){DeviceSpeed=SerialSpeed(optarg+1);}elseif(optarg[0]=='t'){TtySpeed=SerialSpeed(optarg+1);}elseTtySpeed=DeviceSpeed=SerialSpeed(optarg);break;case'o':OutputToStdout=1;break;case'7':ByteBits=CS7;break;case'x':OutputHex=1;break;case'c':UseColor=1;break;case'?':case'h':default:PrintUsage();}}if(optind!=argc)PrintUsage();CommFd=open(DeviceName,O_RDWR,0);if(CommFd<0)Error("Unabletoopendevice");if(fcntl(CommFd,F_SETFL,O_NONBLOCK)<0)Error("UnablesettoNONBLOCKmode");memset(&TtyAttr,0,sizeof(structtermios));TtyAttr.c_iflag=IGNPAR;TtyAttr.c_cflag=DeviceSpeed|HUPCL|ByteBits|CREAD|CLOCAL;TtyAttr.c_cc[VMIN]=1;if(tcsetattr(CommFd,TCSANOW,&TtyAttr)<0)Warning("Unabletosetcommport");TtyFd=open(TtyName,O_RDWR|O_NDELAY,0);if(TtyFd<0)Error("Unabletoopentty");TtyAttr.c_cflag=TtySpeed|HUPCL|ByteBits|CREAD|CLOCAL;if(tcgetattr(TtyFd,&BackupTtyAttr)<0)Error("Unabletogettty");if(tcsetattr(TtyFd,TCSANOW,&TtyAttr)<0)Error("Unabletosettty");for(;;){unsignedcharChar=0;fd_setReadSetFD;voidOutputStdChar(FILE*File){charBuffer[10];intLen=sprintf(Buffer,OutputHex?"%.2X":"%c",Char);fwrite(Buffer,1,Len,File);}FD_ZERO(&ReadSetFD);FD_SET(CommFd,&ReadSetFD);FD_SET(TtyFd,&ReadSetFD);#definemax(x,y)(((x)>=(y))?(x):(y))if(select(max(CommFd,TtyFd)+1,&a

mp;amp;ReadSetFD,NULL,NULL,NULL)<0){Error(strerror(errno));}#undefmaxif(FD_ISSET(CommFd,&ReadSetFD)){while(read(CommFd,&Char,1)==1){WaitFdWriteable(TtyFd);if(write(TtyFd,&Char,1)<0){Error(strerror(errno));}if(OutputToStdout){if(UseColor)fwrite("\x1b[01;34m",1,8,stdout);OutputStdChar(stdout);if(UseColor)fwrite("\x1b[00m",1,8,stdout);fflush(stdout);}}}if(FD_ISSET(TtyFd,&ReadSetFD)){while(read(TtyFd,&Char,1)==1){staticintEscKeyCount=0;WaitFdWriteable(CommFd);if(write(CommFd,&Char,1)<0){Error(strerror(errno));}if(OutputToStdout){if(UseColor)fwrite("\x1b[01;31m",1,8,stderr);OutputStdChar(stderr);if(UseColor)fwrite("\x1b[00m",1,8,stderr);fflush(stderr);}if(Char=='\x1b'){EscKeyCount++;if(EscKeyCount>=3)gotoExitLabel;}elseEscKeyCount=0;}}}ExitLabel:if(tcsetattr(TtyFd,TCSANOW,&BackupTtyAttr)<0)Error("Unabletosettty");return0;}

阅读全文 >

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值