给出打开串口函数

  1. int open_tty(char tty[])
  2. {
  3. int fd;
  4. char tty_path[32]={0};
  5. sprintf(tty_path,"/dev/%s",tty);
  6. fd=tty_open_port(tty_path);
  7. // PORT_SPEED是一个定义的宏,表示传输速率。数据位为8,无校验位,停止位为1
  8. tty_set_opt(fd,PORT_SPEED,8,'N',1);
  9. return fd;
  10. }

该函数接受一个参数,表示你要打开的串口名称,如“ttyS1”,返回串口操作描述符,在调用该函数后,可以根据返回值来判断是否设置成功,如果fd大于0,则返回成功。

该函数还依赖于两个函数,下面也给出(包括用到的头文件)。

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sys/types.h>
  4. #include <error.h>
  5. #include <sys/stat.h>
  6. #include <unistd.h>
  7. #include <termios.h>
  8. #include <fcntl.h>
  9. #include <stdlib.h>
  10. extern int tty_open_port(char *tty_num);
  11. extern int tty_set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop);
  12. int tty_open_port(char *tty_num)
  13. {
  14. int fd = open (tty_num, O_RDWR | O_NOCTTY | O_NDELAY );
  15. //阻塞 fcntl(fd,F_SETFL,0) ; //block
  16. //非阻塞 fcntl(fd,F_SETFL,FNDELAY) ;
  17. if (fd == -1)
  18. {
  19. printf ("Can't Open Serial Port %s !\n",tty_num);
  20. return -1;
  21. }
  22. else
  23. {
  24. //将串口设置成非阻塞的操作
  25. fcntl(fd,F_SETFL,FNDELAY);
  26. return fd;
  27. }
  28.  
  29. }
  30. /**************************************************************************************
  31. * 功 能:set serial port speed
  32. * 修改历史: 2011.6.29.
  33. **************************************************************************************/
  34. int tty_set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop)
  35. {
  36. struct termios newtio,oldtio;
  37. if ( tcgetattr( fd,&oldtio)!=0) {
  38. perror("SetupSerial 1");
  39. return -1;
  40. }
  41. bzero(&newtio, sizeof( newtio ));
  42. newtio.c_cflag |= CLOCAL | CREAD;
  43. newtio.c_cflag &= ~CSIZE; //mask the character size bits
  44.  
  45. switch( nBits )
  46. {
  47. case 7:
  48. newtio.c_cflag |= CS7; //data: 7bits
  49. break;
  50. case 8:
  51. newtio.c_cflag |= CS8; //data: 8bits
  52. break;
  53. }
  54.  
  55. switch( nEvent )
  56. {
  57. case 'O':
  58. newtio.c_cflag |= PARENB;
  59. newtio.c_cflag |= PARODD;
  60. newtio.c_iflag |= (INPCK | ISTRIP);
  61. break;
  62. case 'E':
  63. newtio.c_iflag |= (INPCK | ISTRIP);
  64. newtio.c_cflag |= PARENB;
  65. newtio.c_cflag &= ~PARODD;
  66. break;
  67. case 'N':
  68. newtio.c_cflag &= ~PARENB;
  69. break;
  70. }
  71. switch( nSpeed ) //set the bps
  72. {
  73. case 2400:
  74. cfsetispeed(&newtio, B2400);
  75. cfsetospeed(&newtio, B2400);
  76. break;
  77. case 4800:
  78. cfsetispeed(&newtio, B4800);
  79. cfsetospeed(&newtio, B4800);
  80. break;
  81. case 9600:
  82. cfsetispeed(&newtio, B9600);
  83. cfsetospeed(&newtio, B9600);
  84. break;
  85. case 19200:
  86. cfsetispeed(&newtio, B19200);
  87. cfsetospeed(&newtio, B19200);
  88. break;
  89. case 115200:
  90. cfsetispeed(&newtio, B115200);
  91. cfsetospeed(&newtio, B115200);
  92. break;
  93. case 460800:
  94. cfsetispeed(&newtio, B460800);
  95. cfsetospeed(&newtio, B460800);
  96. break;
  97. default:
  98. cfsetispeed(&newtio, B9600);
  99. cfsetospeed(&newtio, B9600);
  100. break;
  101. }
  102.  
  103. if( nStop == 1 ) //set the 1bit stop
  104. newtio.c_cflag &= ~CSTOPB;
  105. else if ( nStop == 2 ) //set the 2bit stop
  106. newtio.c_cflag |= CSTOPB;
  107. newtio.c_cc[VTIME] = 0;
  108. newtio.c_cc[VMIN] = 0;
  109. tcflush(fd,TCIFLUSH);
  110. if((tcsetattr(fd,TCSANOW,&newtio))!=0)
  111. {
  112. perror("com set error");
  113. return -1;
  114. }
  115. printf("Current serial speed is %d\n",nSpeed);
  116. return 0;
  117. }

代码中给出了可以设置成阻塞和非阻塞的操作。

// <![CDATA[ // ]]

true

Linux下打开串口设置的更多相关文章

  1. 详解linux下的串口通讯开发

    串行口是计算机一种常用的接口,具有连接线少,通讯简单,得到广泛的使用.常用的串口是RS-232-C接口(又称EIA RS-232-C)它是在1970年由美国电子工业协会(EIA)联合贝尔系统.调制解调 ...

  2. 具体解释linux下的串口通讯开发

    串行口是计算机一种经常使用的接口,具有连接线少.通讯简单,得到广泛的使用.经常使用的串口是RS-232-C接口(又称EIA RS-232-C)它是在1970年由美国电子工业协会(EIA)联合贝尔系统. ...

  3. 【转载】详解linux下的串口通讯开发

    来源:https://www.cnblogs.com/sunyubo/archive/2010/09/26/2282116.html 串行口是计算机一种常用的接口,具有连接线少,通讯简单,得到广泛的使 ...

  4. Linux下的串口调试工具——Xgcom

    Linux下的串口调试工具——Xgcom xgcom的下载网址:https://code.google.com/archive/p/xgcom/downloads (1)安装必须的库 apt-get ...

  5. [转载]linux下core文件设置与查看

    转自:https://blog.csdn.net/dingqinghui/article/details/77855330?locationNum=9&fps=1 linux下core文件设置 ...

  6. Linux下环境变量设置 (转)

    Linux下环境变量设置 1.在Windows 系统下,很多软件安装都需要配置环境变量,比如 安装 jdk ,如果不配置环境变量,在非软件安装的目录下运行javac 命令,将会报告找不到文件,类似的错 ...

  7. linux下查看串口信息

    rs232串口通信接口:当通信距离较近时(<12m),可以使用电缆线直接连接,若距离较远,需附加调制解调器. 9个脚针的定义: CDC数据载波检测,RXD接收数据,TXD发送数据,DTR数据中断 ...

  8. linux下打开chm文件的方法

    windows中,通常情况下,chm文件可以使用系统自带的程序打开,但是linux就没有那么幸运了,那么,如何在linux下打开chm 文件呢?有小编来为您介绍介绍,本篇,小编以ubuntu环境为例 ...

  9. MongoDB在Linux下常用优化设置

    MongoDB在Linux下常用优化设置 以下是一些MongoDB推荐的常用优化设置.在生产环境下选取合适的参数值,例如预读值和默认文件描述符数目等,会对系统性能有很大的影响. 1.关闭数据库文件的 ...

随机推荐

  1. Entity Framework 5.0基础系列

    1.Entity Framework简介 http://www.cnblogs.com/aehyok/p/3315991.html 2.Entity Framework DBFirst尝试http:/ ...

  2. 漫谈Puppet4

    激动人心的改进 速度,速度,还是速度 稳定性和鲁棒性的提升 全新的Parser “不变"的agent 不兼容的改动 包管理方式的变化 配置文件/目录的路径变化 其他路径变化 Director ...

  3. 如何判断平台工具集去做条件编译(VC++目录、预处理器定义、$(PlatformToolsetVersion))

    作者:zyl910 从VS2010开始,提供了一个平台工作集(Platform ToolSet)选项用于配制vc编译版本.到了VS2012,更是因为默认平台工具集不支持WindowsXP,导致经常需要 ...

  4. java/.net-常用工具下载地址&常用学习网址&快捷键

    HTML5 HTML5:http://www.html5cn.org/ php常用网址 thinkphp框架:http://www.thinkphp.cn/ wampserver开发服务器:http: ...

  5. c++中typename和class的区别介绍

    "typename"是一个C++程序设计语言中的关键字.相当用于泛型编程时是另一术语"class"的同义词.这个关键字用于指出模板声明(或定义)中的非独立名称( ...

  6. git的一些相关知识

    1.配置多个git远程仓库的ssh-Key切换(转自) 目前的git仓库如github都是通过使用SSH与客户端连接,如果只是固定使用单个git仓库的单个用户 (first),生成生成密钥对后,将公钥 ...

  7. css blur 的兼容写法

    出自:小tip: 使用CSS将图片转换成模糊(毛玻璃)效果 .blur { filter: url(blur.svg#blur); /* IE10, IE11 */ -webkit-filter: b ...

  8. 使用ueditor小结

    1. 导入 js: ueditor.config.js ueditor.all.js css/images/plugin: themes lang dialog(可选) third-party(可选) ...

  9. ffmpeg安装的问题

    php语音转换需要安装ffmpeg文件 参考地址: http://thierry-xing.iteye.com/blog/2017864 http://diogomelo.net/blog/11/en ...

  10. Kafka - SQL 代码实现

    1.概述 上次给大家分享了关于 Kafka SQL 的实现思路,这次给大家分享如何实现 Kafka SQL.要实现 Kafka SQL,在上一篇<Kafka - SQL 引擎分享>中分享了 ...