弄了一下串口,一个小问题多折腾了下,备忘。
软件环境:
zl@zhanglong:~$ cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=12.04
DISTRIB_CODENAME=precise
DISTRIB_DESCRIPTION="Ubuntu 12.04.4 LTS"

zl@zhanglong:~$uname -a
Linux zhanglong 3.2.0-58-generic #88-Ubuntu SMP Tue Dec 3 17:37:58 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
硬件环境:
    另有一台win7,有串口及读写串口的工具软件。两机通过交叉串口线相连

代码如下:

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <errno.h>
  6. #include <termios.h>
  7. /**
  8. * 写/读数据
  9. **/
  10. int main(int argc, char* argv[])
  11. {
  12. int i;
  13. int fd; /* File descriptor for the port */
  14. int iRet;
  15. char buf[1024];
  16. fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
  17. if (fd < 0) { /** error **/
  18. printf("[%s-%d] open error!!!\n", __FILE__, __LINE__);
  19. goto err1;
  20. }
  21. //fcntl(fd, F_SETFL, FNDELAY);
  22. fcntl(fd, F_SETFL, 0);
  23. iRet = write(fd, "a1b2c3\r", 7);
  24. if (iRet < 0) {
  25. printf("[%s-%d] write error!!!\n", __FILE__, __LINE__);
  26. }
  27. iRet = read(fd, buf, 1024);
  28. for(i = 0; i < iRet; i++) {
  29. if((i & 0xf) == 0) {
  30. printf("\n");
  31. }
  32. printf("0x%02x ", buf[i]);
  33. fflush( fflush(stdout));
  34. }
  35. printf("\n");
  36. close(fd);
  37. err1:
  38. return 0;
  39. }

编译运行此代码后发现:
      win7能够收到ubuntu发出的数据,但win7发出的数据,自己意外收到了,而ubuntu却收不到。或有时能收到数据(如win7以十六进制发4567890a时,ubuntu能收到数据0x45 0x67 0xffffff89 0x0a)。
  将char buf[1024]数组改成unsigned char buf[1024]后,0xffffff89变成了0x89。
  win7机器上单独发以十六进制,先发"7890a",ubuntu无打印输出,再发"457890a",ubuntu无打印输出,然后发"4567890a"时,ubuntu上的程序打印收到的数据,并且前面几次发出的都收到了。
  网上发现一说法,如果不是终端,使用Raw Mode方式来通讯。偿试将串口设置成Raw Mode,再读数据。

    修改后的代码如下:

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <errno.h>
  6. #include <termios.h>
  7. /**
  8. * 读数据
  9. **/
  10. int main(int argc, char* argv[])
  11. {
  12. int i;
  13. int fd; /* File descriptor for the port */
  14. int iRet;
  15. struct termios options_old, options;
  16. unsigned char buf[1024];
  17. fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
  18. if (fd < 0) { /** error **/
  19. printf("[%s-%d] open error!!!\n", __FILE__, __LINE__);
  20. goto err1;
  21. }
  22. //fcntl(fd, F_SETFL, FNDELAY);
  23. fcntl(fd, F_SETFL, 0);
  24. /*********************************************************/
  25. /** * Get the current options for the port... **/
  26. tcgetattr(fd, &options);
  27. options_old = options;
  28. /*** Set the baud rates to 9600... **/
  29. // cfsetispeed(&options, B9600);
  30. // cfsetospeed(&options, B9600);
  31. options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/
  32. //options.c_oflag |= OPOST; /** 加工过的输出 **/
  33. options.c_oflag &= ~OPOST; /** 选择原始输出 **/
  34. /*** Enable the receiver and set local mode... **/
  35. // options.c_cflag |= (CLOCAL | CREAD);
  36. /*** Set the new options for the port... **/
  37. tcsetattr(fd, TCSANOW, &options);
  38. /*********************************************************/
  39. iRet = write(fd, "a1b2c3\r", 7);
  40. if (iRet < 0) {
  41. printf("[%s-%d] write error!!!\n", __FILE__, __LINE__);
  42. }
  43. iRet = read(fd, buf, 1024);
  44. for(i = 0; i < iRet; i++) {
  45. if((i & 0xf) == 0) {
  46. printf("\n");
  47. }
  48. printf("0x%02x ", buf[i]);
  49. fflush(stdout);
  50. }
  51. printf("\n");
  52. tcsetattr(fd, TCSANOW, &options_old);
  53. close(fd);
  54. err1:
  55. return 0;
  56. }

简单测试,发现win7每次发出数据,ubuntu一端都可以收到。并且win7一端没有再收到自己发出的数据。
     但win7一次发出很长一段数据(如456789abcdef)时,ubuntu只收到前面的数据(0x34 0x35 0x36 0x37 0x38 0x39 0x61 0x62)。估计是串口速度慢,read()系统调用等不了那么长时间,接收到一部分数据后就返回了。
               测试循环执行read()系统调用,并打印收到的数据,确实可以收到更多数据。

至此,问题已经清晰:ubuntu下的/dev/ttyS0设备打开时,默认设置了终端相关的特性,会根据收到不同的数据做出不同的反应。如果用作纯粹的数据传输通道,需要进行设置,去除终端相关特性设定。

ubuntu下串口编程备忘的更多相关文章

  1. t420 win7 硬盘安装ubuntu 10.04 LTS 备忘

    http://zhangwen.sinaapp.com/?p=5 t420 win7 硬盘安装ubuntu 10.04 LTS 备忘 发表于 2011 年 10 月 25 日 对ubuntu的新版没有 ...

  2. Windows下串口编程

     造冰箱的大熊猫@cnblogs 2019/1/27 将Windows下串口编程相关信息进行下简单小结,以备后用. 1.打开串口 打开串口使用CreateFile()函数.以打开COM6为例: HAN ...

  3. ubuntu下C编程,编译基础( 转)

    buntu下C编程,编译基础     C 编程中相关文件后缀 .a 静态库 (archive) .c C源代码(需要编译预处理) .h C源代码头文件 .i C源代码(不需编译预处理) .o 对象文件 ...

  4. Linux下串口编程【转】

    本文转载自:http://blog.csdn.net/w282529350/article/details/7378388 /************声明:本人只是见到这篇文章对我帮助很大才转载的,但 ...

  5. Linux下串口编程入门

    简介: Linux操作系统从一开始就对串行口提供了很好的支持,本文就Linux下的串行口通讯编程进行简单的介绍. 串口简介  串行口是计算机一种常用的接口,具有连接线少,通讯简单,得到广泛的使用.常用 ...

  6. ubuntu和raspberry下调试python_spi备忘

    Ubuntu12.04 自安装python3.3中头文件Python.h路径:usr/local/python3.3/include/python3.3m Ubuntu12.04 自带的Python2 ...

  7. ubuntu 下串口调试工具 minicom安装与配置cutecom安装

    安装minicom:     $sudo apt-get install minicom 配置minicom:    如果您的系统的默认语言不是英文,请执行下面的命令:     $LANG=EN    ...

  8. Xeon Phi 编程备忘

    ▶ 闲鱼的 Xeon Phi 3120A 配办公室的新 Xeon 服务器,记录一下环境安装过程. ● 原本尝试搭 Ubuntu 服务器,参考[https://software.intel.com/en ...

  9. ubuntu 下串口调试工具 minicom安装与配置

    检查系统是否支持USB转串口: lsmod | grep usbserial 如果有usbserial,说明系统支持USB转串口. 识别串口设备: 插上USB转串口,在终端输入命令: #dmesg | ...

随机推荐

  1. Docker几个基本常识

    标签(linux): docker 此文来自本人学习以及网络整理而来. 笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 对于用户来说,可能一开始在不了解的情况下会 ...

  2. GitHub For Beginners: Don’t Get Scared, Get Started

    It's 2013, and there's no way around it: you need to learn how to use GitHub.2 Why? Because it's a s ...

  3. 百度插件webuploader的坑!

    前言: 自因为项目中需要使用上传插件,所以之前找了几款上传插件.但是呢,小的上传插件是不支持我们项目上传的(做虚拟机项目的,一个镜像可能好几个G),所以呢这个插件要支持分片上传,拓展性要高(肯定的啦, ...

  4. 【OH】Oracle软件安装需要的软件包(官方文档)

    1  安装12c 1.1  Table 3 x86-64 Supported Linux 7 Operating System Requirements Item Requirements SSH R ...

  5. Windows实用命令

     Windows实用命令   # 统计ESTABLISHED状态下的连接一共有多少个/c是统计行数,/i是忽略大小写 netstat -ano|find /i "established&qu ...

  6. SpringMVC源码情操陶冶-AbstractHandlerMethodMapping

    承接前文SpringMVC源码情操陶冶-AbstractHandlerMapping,本文将介绍如何注册HandlerMethod对象作为handler 类结构瞧一瞧 public abstract ...

  7. GitLab简单使用

    [权限] ①已经搭建了Gitlab服务器,并已开通  http://git.******.com 权限(每个公司的git地址不一致),登陆到git中去: ②已经开通了项目权限(有专人负责开通): 当开 ...

  8. C primer Plus_part6

    第十章  数组和指针 1.const :保护变量不受改变,特别是在作为入参传入函数 对于变量:const 不能修改值 对于指针: const 可以修改值,但是不能修改指向对象 #include< ...

  9. [Swift]UIKit学习之警告框:UIAlertController和UIAlertView

    Important: UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated.) T ...

  10. 【echarts3】--1 简单入门

    echarts3 相信大家都了解吧,是百度研发的 ECharts 特性介绍 ECharts,一个纯 Javascript 的图表库,可以流畅的运行在 PC 和移动设备上,兼容当前绝大部分浏览器(IE8 ...