`
lovnet
  • 浏览: 6725647 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

linux 串口发送

 
阅读更多

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics