Linux,串口,非缓冲模式 [英] Linux, serial port, non-buffering mode

查看:21
本文介绍了Linux,串口,非缓冲模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Linux 中使用串行端口组织 nob-blocking 读写功能.这是我的代码:http://pastebin.com/RSPw7HAi一切正常,但它是缓冲的.这意味着,如果我通过控制台 + CR 符号向串行输入,则选择检测新输入,否则,如果我通过简单的 python 脚本输入,它会缓冲所有符号并等待直到我发送回车符号.因此,有了这个输入(如下所示),它只是在某处缓冲符号.我必须通过 USB2Serial 转换器连接 PC

I am trying to organize nob-blocking read-write functionality with serial port in Linux. Here is the code I have: http://pastebin.com/RSPw7HAi It all works fine, but it is buffered. That means, that if I do input to serial via console + CR symbol, select detects new input, otherwise, if I do input via simple python script, it buffers all symbols and waits until I send it carriage return symbol. So with this input (given below) it simply buffers symbols somewhere. I have to PCs connected via USB2Serial converter

#!/usr/bin/env python3

import serial

cmd1_state = b'x3Ex01x00x01'

#Selecting serial port for commands to be sent --> /dev/ttyUSB0
serial_0 = serial.Serial('/dev/ttyUSB2');
print("Using serial port ", serial_0.portstr);
serial_0.write(cmd1_state)

# closing serial port
serial_0.close()

那么,谁能告诉我在这里做什么?我是否必须在我的 C 文件中的端口打开中更改某些内容,或者使用 python 脚本来完成?后来我使用了 flush() 方法,但它也没有帮助.顺便说一句,我已经用谷歌搜索了 F_NOCACHE arg 到 fcntl() 函数.但!这都是关于 BSD 和 Darwin OS 的,据我所知,Linux 中没有这样的东西(F_NOACHE arg 到 fcntl).

So, can anybody tell me what to do here? Do I have to change something within port opening in my C file or it's to be done with python script? I used flush() method in later, but it also did not help. BTW, I've googled out about F_NOCACHE arg to fcntl() function. BUT! It's all about BSD and Darwin OS's, there is no such thing (F_NOACHE arg to fcntl) in Linux, as far as I could see.

更新:看来我找到了解决办法.

UPD: Looks like I found out the solution.

 /* set input mode (non-canonical, no echo,...) */
newtio.c_lflag = 0;

newtio.c_cc[VTIME]    = 0;   /* inter-character timer unused */
newtio.c_cc[VMIN]     = 1;   /* blocking read until 1 char received */

tcflush(fd, TCIFLUSH);

取自:http://tldp.org/HOWTO/Serial-Programming-HOWTO/x115.html

推荐答案

看来我找到了解决方案.

Looks like I found out the solution.

/* set input mode (non-canonical, no echo,...) */
newtio.c_lflag = 0;

newtio.c_cc[VTIME]    = 0;   /* inter-character timer unused */
newtio.c_cc[VMIN]     = 1;   /* blocking read until 1 char received */

tcflush(fd, TCIFLUSH);
Taken from : http://tldp.org/HOWTO/Serial-Programming-HOWTO/x115.html

这篇关于Linux,串口,非缓冲模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆