- #include<stdio.h>
- #include<sys/types.h>
- #include<sys/stat.h>
- #include<fcntl.h>
- #include<termios.h>
- #defineBAUDRATEB115200
- #defineMODEMDEVICE"/dev/ttyUSB1"
- #defineSTOP'@'
- intmain()
- {
- intfd,c=0,res;
- structtermiosoldtio,newtio;
- charch,s1[20];
- printf("start.../n");
- fd=open(MODEMDEVICE,O_RDWR|O_NOCTTY);
- if(fd<0)
- {
- perror(MODEMDEVICE);
- exit(1);
- }
- printf("open.../n");
- tcgetattr(fd,&oldtio);
- bzero(&newtio,sizeof(newtio));
- newtio.c_cflag=BAUDRATE|CS8|CLOCAL|CREAD;
- newtio.c_iflag=IGNPAR;
- newtio.c_oflag=0;
- newtio.c_lflag=ICANON;
- tcflush(fd,TCIFLUSH);
- tcsetattr(fd,TCSANOW,&newtio);
- printf("writing.../n");
- while(1)
- {
- while((ch=getchar())!='@')
- {
- s1[0]=ch;
- res=write(fd,s1,1);
- }
- s1[0]=ch;
- s1[1]='/n';
- res=write(fd,s1,2);
- break;
- }
- printf("close.../n");
- close(fd);
- tcsetattr(fd,TCSANOW,&oldtio);
- return0;
- }
linux 串口发送
最新推荐文章于 2023-07-20 22:36:27 发布