Serial Communication Canonical Mode Non-Blocking N

2020-04-21 06:48发布

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.

2条回答
放我归山
2楼-- · 2020-04-21 07:20

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.

查看更多
手持菜刀,她持情操
3楼-- · 2020-04-21 07:41

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

查看更多
登录 后发表回答