Serial Communication Canonical Mode Non-Blocking NL Detection

喜夏-厌秋 提交于 2019-12-02 07:54:13

问题


I am sending commands over a serial port from a Linux embedded device to some serial enabled firmware. For easy debugging and simplicity we are using ascii human-readable commands terminated by newlines. Is it appropriate to use canonical mode here, or is canonical mode usually reserved for interactive terminals? The examples I find online all use raw mode.

In particular, in canonical mode, how do I check without blocking if an entire line is available for reading.


回答1:


according to Linux Serial Programming documentation :

This is the normal processing mode for terminals, but can also be useful for communicating with other dl input is processed in units of lines, which means that a read will only return a full line of input. A line is by default terminated by a NL (ASCII LF), an end of file, or an end of line character. A CR (the DOS/Windows default end-of-line) will not terminate a line with the default settings.

Canonical input processing can also handle the erase, delete word, and reprint characters, translate CR to NL, etc..

First

using canonical mode for serial communications is the best option, because we have Linux kernel support on data transmission and system handlers that will help to better reading serial text

Second

if you want to use canonical mode , make sure that you are using the right character for end of line in your device that sending data , other way you cannot use canonical feature




回答2:


Is it appropriate to use canonical mode here, or is canonical mode usually reserved for interactive terminals?

Yes, you can use canonical mode, but you will need to configure the termios interface for your situation.
The default termios configuration is for an interactive terminal, so features such as echoing the input should be disabled.
Since your device is unlikely to send the backspace and delete characters, such features can be ignored.


The examples I find online all use raw mode.

Seems like there are some "experts" that are not aware that canonical mode for terminals exists.
See the comments to reading from serial port in c breaks up lines .

For an example of (blocking) canonical mode, see this answer (note that there's another "expert comment" telling the OP that he cannot read lines).


In particular, in canonical mode, how do I check without blocking if an entire line is available for reading.

You could use select().
The man page implies that a canonical read of a terminal device is supported:

The file descriptors listed in readfds will be watched to see if characters become available for reading (more precisely, to see if a read will not block ...)

When both fields of the timeval structure are zero, then select() will not block and returns immediately.



来源:https://stackoverflow.com/questions/53728798/serial-communication-canonical-mode-non-blocking-nl-detection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!