linux 串口接收

Code:
  1. #include<stdio.h>
  2. #include<sys/types.h>
  3. #include<fcntl.h>
  4. #include<termios.h>
  5. #defineBAUDRATEB115200
  6. #defineMODEMDEVICE"/dev/ttyUSB1"
  7. intmain()
  8. {
  9. intfd,c=0,res;
  10. structtermiosoldtio,newtio;
  11. charbuf[256];
  12. printf("start.../n");
  13. fd=open(MODEMDEVICE,O_RDWR|O_NOCTTY);
  14. if(fd<0)
  15. {
  16. perror(MODEMDEVICE);
  17. exit(1);
  18. }
  19. printf("open.../n");
  20. tcgetattr(fd,&oldtio);
  21. bzero(&newtio,sizeof(newtio));
  22. newtio.c_cflag=BAUDRATE|CS8|CLOCAL|CREAD;
  23. newtio.c_iflag=IGNPAR;
  24. newtio.c_oflag=0;
  25. newtio.c_lflag=ICANON;
  26. tcflush(fd,TCIFLUSH);
  27. tcsetattr(fd,TCSANOW,&newtio);
  28. printf("reading.../n");
  29. while(1)
  30. {
  31. res=read(fd,buf,255);
  32. //buf[res]=='/0';
  33. printf("res=%dvuf=%s/n",res,buf);
  34. //if(buf[0]=='@')break;
  35. }
  36. printf("close.../n");
  37. close(fd);
  38. tcsetattr(fd,TCSANOW,&oldtio);
  39. return0;
  40. }

你可能感兴趣的:(linux)