使用read(...)时在换行符上停止 [英] Stop on newline when using read(...)

查看:685
本文介绍了使用read(...)时在换行符上停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从通过UART连接的GPS读取NMEA语句.操作系统是Debian,语言必须是C ++.为此,我使用open(...)打开文件,并使用read(...)读取字符串.但是,这种方式我必须指定一个字符串长度,该长度会打乱句子.相反,我想读到NMEA句子的结尾.如何使用read(...)并在新行停止? read(...)是否可以选择?

I need to read the NMEA sentences from a GPS connected through UART. The OS is Debian, and the language must be C++. To do so I'm opening the file with open(...) and reading a string with read(...). However, this way I have to specify a string length, which breaks up the sentences. Instead I want to read until the end of the NMEA sentence. How may I use read(...) and stop on new line? Is there an option to read(...)?

推荐答案

我需要从通过UART连接的GPS读取NMEA语句.
...
如何使用read(...)并在新行停止?

I need to read the NMEA sentences from a GPS connected through UART.
...
How may I use read(...) and stop on new line?

如果打开终端设备(例如/dev/ttyUSB0 ),则可以使用终端的线路规程处理程序将接收到的文本解析为行. 必须以阻止模式打开终端(除非指定了非阻止,否则默认为默认模式),并且必须将终端配置为

If you opened a terminal device (e.g. /dev/ttyUSB0), then you can use the terminal's line discipline handler to parse the received text into lines.
The terminal must be opened in blocking mode (which is the default unless non-blocking is specified), and the terminal must be configured for canonical input (using the termios API).

是否可以读取(...)?

Is there an option to read(...)?

当终端设备配置为规范输入时, read()将返回一行文本(除非发生错误).请确保您的读取缓冲区(和count参数)足够大以容纳最长的预期行,以使 read()不会截断该行.

When the terminal device is configured for canonical input, then a read() will return a line of text (unless an error occurred). Be sure that your read buffer (and count argument) is large enough for the longest expected line, so that the read() will not truncate the line.

termios 手册页中:

   Canonical and noncanonical mode  

   The setting of the ICANON canon flag in c_lflag determines whether
   the terminal is operating in canonical mode (ICANON set) or
   noncanonical mode (ICANON unset).  By default, ICANON set.

   In canonical mode:

   * Input is made available line by line.  An input line is available
     when one of the line delimiters is typed (NL, EOL, EOL2; or EOF at
     the start of line).  Except in the case of EOF, the line delimiter
     is included in the buffer returned by read(2).

   * Line editing is enabled (ERASE, KILL; and if the IEXTEN flag is
     set: WERASE, REPRINT, LNEXT).  A read(2) returns at most one line
     of input; if the read(2) requested fewer bytes than are available
     in the current line of input, then only as many bytes as requested
     are read, and the remaining characters will be available for a
     future read(2).

   * The maximum line length is 4096 chars (including the terminating
     newline character); lines longer than 4096 chars are truncated.
     After 4095 characters, input processing (e.g., ISIG and ECHO*
     processing) continues, but any input data after 4095 characters up
     to (but not including) any terminating newline is discarded.  This
     ensures that the terminal can always receive more input until at
     least one line can be read.

使用stty命令或 tcgetattr() tcsetattr()来配置终端模式.
研究正确设置终端模式 POSIX操作系统串行编程指南.

Use the stty command, or tcgetattr() and tcsetattr() to configure the terminal mode.
Study Setting Terminal Modes Properly and Serial Programming Guide for POSIX Operating Systems.

请注意,读取缓冲区中返回的行不是字符串,并且不会以空字节终止.有关解决方案,请参见 Linux串行读取抛出错误

Note that the line returned in the read buffer is not a string, and will not be terminated with a null byte. For a solution see Linux Serial Read throws Error

这篇关于使用read(...)时在换行符上停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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