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 | ...
随机推荐
- python交互模式下tab键自动补全
import rlcompleter,readline readline.parse_and_bind('tab:complete')
- java 对象、集合的非空判断
自我总结,有什么不到位的地方,请各位纠正补充,感激不尽! 目的:使程序更严谨 ***对象验证是否不为空: if( null != obj ) ***List验证不为空:if( null != lis ...
- (转)Elasticsearch 5 Ik+pinyin分词配置详解
今天以这篇文章结束同城旅游网的面试,正好面试官也问到站内检索,可以尝试一下这篇文章介绍的方法.Elasticsearch 5 Ik+pinyin分词配置详解
- CF585E. Present for Vitalik the Philatelist [容斥原理 !]
CF585E. Present for Vitalik the Philatelist 题意:\(n \le 5*10^5\) 数列 \(2 \le a_i \le 10^7\),对于每个数\(a\) ...
- 在.NetCore中使用Myrmec检测文件真实格式
Myrmec 是什么? Myrmec 是一个用于检测文件格式的库,Myrmec不同于其它库或者手写检测代码,Myrmec不依赖文件扩展名(在实际使用中,你的用户很可能使用虚假的扩展名欺骗你的应用程序) ...
- VUE2.0 elemenui-ui 2.0.X 封装 省市区三级
1. 效果图 2. 版本依赖 vue 2.X , elementui 2.0.11 使用element ui <el-form>标签 3. 源码 components/CityL ...
- 应用负载均衡之LVS(二):VS_TUN和VS_DR的arp问题
*/ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...
- qt安装--this Qt version uses an unsupported makefile
解决办法: Run regedit. Hop to HKEY_CURRENT_USER\Software\Trolltech\Versions或HKEY_CURRENT_USER\Software\D ...
- "abc123 ,def456",反转字母,其他位置不变
"abc123 ,def456",反转字母,其他位置不变. 无意间看到个有意思的面试题,忽然来了兴趣想着来做一下. 操作字符串用正则的效率比较高,但第一反应还是用原生来操作.下面说 ...
- asp.net Global.asax 不运行解决
asp.net application的站点发布后 Global.asax 未运行,搞了好久终于解决, 解决方法如下: publish设置 该设置经测试在win server 2003 和2008 都 ...