ubuntu下串口编程备忘
弄了一下串口,一个小问题多折腾了下,备忘。
软件环境:
zl@zhanglong:~$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=12.04
DISTRIB_CODENAME=precise
DISTRIB_DESCRIPTION="Ubuntu 12.04.4 LTS"
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,有串口及读写串口的工具软件。两机通过交叉串口线相连
代码如下:
点击(此处)折叠或打开
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <termios.h>
- /**
- * 写/读数据
- **/
- int main(int argc, char* argv[])
- {
- int i;
- int fd; /* File descriptor for the port */
- int iRet;
- char buf[1024];
- fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
- if (fd < 0) { /** error **/
- printf("[%s-%d] open error!!!\n", __FILE__, __LINE__);
- goto err1;
- }
- //fcntl(fd, F_SETFL, FNDELAY);
- fcntl(fd, F_SETFL, 0);
- iRet = write(fd, "a1b2c3\r", 7);
- if (iRet < 0) {
- printf("[%s-%d] write error!!!\n", __FILE__, __LINE__);
- }
- iRet = read(fd, buf, 1024);
- for(i = 0; i < iRet; i++) {
- if((i & 0xf) == 0) {
- printf("\n");
- }
- printf("0x%02x ", buf[i]);
- fflush( fflush(stdout));
- }
- printf("\n");
- close(fd);
- err1:
- return 0;
- }
编译运行此代码后发现:
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,再读数据。
修改后的代码如下:
点击(此处)折叠或打开
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <termios.h>
- /**
- * 读数据
- **/
- int main(int argc, char* argv[])
- {
- int i;
- int fd; /* File descriptor for the port */
- int iRet;
- struct termios options_old, options;
- unsigned char buf[1024];
- fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
- if (fd < 0) { /** error **/
- printf("[%s-%d] open error!!!\n", __FILE__, __LINE__);
- goto err1;
- }
- //fcntl(fd, F_SETFL, FNDELAY);
- fcntl(fd, F_SETFL, 0);
- /*********************************************************/
- /** * Get the current options for the port... **/
- tcgetattr(fd, &options);
- options_old = options;
- /*** Set the baud rates to 9600... **/
- // cfsetispeed(&options, B9600);
- // cfsetospeed(&options, B9600);
- options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/
- //options.c_oflag |= OPOST; /** 加工过的输出 **/
- options.c_oflag &= ~OPOST; /** 选择原始输出 **/
- /*** Enable the receiver and set local mode... **/
- // options.c_cflag |= (CLOCAL | CREAD);
- /*** Set the new options for the port... **/
- tcsetattr(fd, TCSANOW, &options);
- /*********************************************************/
- iRet = write(fd, "a1b2c3\r", 7);
- if (iRet < 0) {
- printf("[%s-%d] write error!!!\n", __FILE__, __LINE__);
- }
- iRet = read(fd, buf, 1024);
- for(i = 0; i < iRet; i++) {
- if((i & 0xf) == 0) {
- printf("\n");
- }
- printf("0x%02x ", buf[i]);
- fflush(stdout);
- }
- printf("\n");
- tcsetattr(fd, TCSANOW, &options_old);
- close(fd);
- err1:
- return 0;
- }
简单测试,发现win7每次发出数据,ubuntu一端都可以收到。并且win7一端没有再收到自己发出的数据。
但win7一次发出很长一段数据(如456789abcdef)时,ubuntu只收到前面的数据(0x34 0x35 0x36 0x37 0x38 0x39 0x61 0x62)。估计是串口速度慢,read()系统调用等不了那么长时间,接收到一部分数据后就返回了。
测试循环执行read()系统调用,并打印收到的数据,确实可以收到更多数据。
至此,问题已经清晰:ubuntu下的/dev/ttyS0设备打开时,默认设置了终端相关的特性,会根据收到不同的数据做出不同的反应。如果用作纯粹的数据传输通道,需要进行设置,去除终端相关特性设定。
ubuntu下串口编程备忘的更多相关文章
- t420 win7 硬盘安装ubuntu 10.04 LTS 备忘
http://zhangwen.sinaapp.com/?p=5 t420 win7 硬盘安装ubuntu 10.04 LTS 备忘 发表于 2011 年 10 月 25 日 对ubuntu的新版没有 ...
- Windows下串口编程
造冰箱的大熊猫@cnblogs 2019/1/27 将Windows下串口编程相关信息进行下简单小结,以备后用. 1.打开串口 打开串口使用CreateFile()函数.以打开COM6为例: HAN ...
- ubuntu下C编程,编译基础( 转)
buntu下C编程,编译基础 C 编程中相关文件后缀 .a 静态库 (archive) .c C源代码(需要编译预处理) .h C源代码头文件 .i C源代码(不需编译预处理) .o 对象文件 ...
- Linux下串口编程【转】
本文转载自:http://blog.csdn.net/w282529350/article/details/7378388 /************声明:本人只是见到这篇文章对我帮助很大才转载的,但 ...
- Linux下串口编程入门
简介: Linux操作系统从一开始就对串行口提供了很好的支持,本文就Linux下的串行口通讯编程进行简单的介绍. 串口简介 串行口是计算机一种常用的接口,具有连接线少,通讯简单,得到广泛的使用.常用 ...
- ubuntu和raspberry下调试python_spi备忘
Ubuntu12.04 自安装python3.3中头文件Python.h路径:usr/local/python3.3/include/python3.3m Ubuntu12.04 自带的Python2 ...
- ubuntu 下串口调试工具 minicom安装与配置cutecom安装
安装minicom: $sudo apt-get install minicom 配置minicom: 如果您的系统的默认语言不是英文,请执行下面的命令: $LANG=EN ...
- Xeon Phi 编程备忘
▶ 闲鱼的 Xeon Phi 3120A 配办公室的新 Xeon 服务器,记录一下环境安装过程. ● 原本尝试搭 Ubuntu 服务器,参考[https://software.intel.com/en ...
- ubuntu 下串口调试工具 minicom安装与配置
检查系统是否支持USB转串口: lsmod | grep usbserial 如果有usbserial,说明系统支持USB转串口. 识别串口设备: 插上USB转串口,在终端输入命令: #dmesg | ...
随机推荐
- Docker几个基本常识
标签(linux): docker 此文来自本人学习以及网络整理而来. 笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 对于用户来说,可能一开始在不了解的情况下会 ...
- 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 ...
- 百度插件webuploader的坑!
前言: 自因为项目中需要使用上传插件,所以之前找了几款上传插件.但是呢,小的上传插件是不支持我们项目上传的(做虚拟机项目的,一个镜像可能好几个G),所以呢这个插件要支持分片上传,拓展性要高(肯定的啦, ...
- 【OH】Oracle软件安装需要的软件包(官方文档)
1 安装12c 1.1 Table 3 x86-64 Supported Linux 7 Operating System Requirements Item Requirements SSH R ...
- Windows实用命令
Windows实用命令 # 统计ESTABLISHED状态下的连接一共有多少个/c是统计行数,/i是忽略大小写 netstat -ano|find /i "established&qu ...
- SpringMVC源码情操陶冶-AbstractHandlerMethodMapping
承接前文SpringMVC源码情操陶冶-AbstractHandlerMapping,本文将介绍如何注册HandlerMethod对象作为handler 类结构瞧一瞧 public abstract ...
- GitLab简单使用
[权限] ①已经搭建了Gitlab服务器,并已开通 http://git.******.com 权限(每个公司的git地址不一致),登陆到git中去: ②已经开通了项目权限(有专人负责开通): 当开 ...
- C primer Plus_part6
第十章 数组和指针 1.const :保护变量不受改变,特别是在作为入参传入函数 对于变量:const 不能修改值 对于指针: const 可以修改值,但是不能修改指向对象 #include< ...
- [Swift]UIKit学习之警告框:UIAlertController和UIAlertView
Important: UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated.) T ...
- 【echarts3】--1 简单入门
echarts3 相信大家都了解吧,是百度研发的 ECharts 特性介绍 ECharts,一个纯 Javascript 的图表库,可以流畅的运行在 PC 和移动设备上,兼容当前绝大部分浏览器(IE8 ...