Tty device to read and write transfer

Transfer from https://feng-qi.github.io/2017/05/04/how-to-read-write-to-tty-device/

    <p>这是 StackExchange 上的一个问答,在这里翻译一下原文地址为:<br><a href="https://unix.stackexchange.com/questions/138342/how-to-read-write-to-tty-device" target="_blank" rel="noopener">How to read/write to tty* device?</a></p>

Problem Description

I have a device to transfer information via USB to my computer. Arch Linux through the /dev/next set up a
named ttyUSB0file to set up the equipment. I've been using GTKtermto receive and store information in a display
on analog terminal window.


My question is: GTKtermhow specific read / write ttyUSB0the file, from where I can learn to achieve similar
technical capabilities? That is, from the simplest case, I how to write a character to ttyUSB0, or receive it from a
byte written to the file and go?


Michael Homer's answer

You may like to use other file using TTYs file. You can use a general method that you use to open the file language to fight
to open them and read and write. They are compared to other "normal" files, there are some special behavior, but basically it is the same. I
will talk some special cases at the end of the text, but still look at some of the experiments it.


You can do in an ordinary terminal interesting thing is to run ttyit and it will print a line similar to the following output:


1
/dev/pts/2

This is your terminal is running relies TTY device, you can write something to that end:


1
2
3
$ echo Hello > /dev/pts/2
Hello
\(</span><br></pre></td></tr></tbody></table></figure> <p>你甚至可以从它读取信息:</p> <figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">\) read X < /dev/pts/2
hello
$ echo \(X</span><br><span class="line">hello</span><br><span class="line">\)

( read XIt is shused to "read a line from the standard input and saved to the variable X" command; <indication /dev/pts/2
standard input as the read command; first "hello" is a type I, a second output terminal).


If you use screenor xtermopen another shell, you can run the shell in the newly opened
echo spooky > /dev/pts/2, the text will be displayed in your original shell, to other command
says is the same.




The following is a simple C program that can do what you want to do, to /dev/pts/3write a character, and then from
reading a byte it there:


1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int main() {
char byte;
int fd = open("/dev/pts/3", O_RDWR);
write(fd, "X", 1);
ssize_t size = read(fd, &byte, 1);
printf("Read byte %c\n", byte);
return 0;
}

A shell or terminal emulator is bound to the true TTY device will produce some interesting behavior, but you should be able to get
some feedback (get something back).




I want to access a terminal you need to have the appropriate permissions. These are just standard file permissions, and you use it ls -lto see
there is a chmodset of the same: You need to read the permissions to open the file and read it, and write permissions to write it. In the
back of your TTY terminal will belong to you, but not others, and corresponding to the USB device may also belong to TTY may
be or may not belong to you, depending on your configuration. You can use the same method to change the usual permissions.


As long as the program can be written with normal interaction, you do not need to do any special setup. You can see in the example that you
do not need to let the other side to read the data you write and every close the file: TTY file behaves like a pipeline, as long as the
data came just passed from both ends. When I wrote the text to the TTY can immediately show up, so when I later read from
the time taken has no data waiting for me. This is not like writing an ordinary file data is saved to disk -
it will be directly transferred to the other end, or saved to memory until it is read.


You might want to use selectthis function, it allows you to do other things in wait for incoming data devices, of course, if
you just want to wait for the arrival of data that you can use to read blocking the way, and let the operating system do the relevant scheduling.


One thing need to keep in mind is the kernel buffer size is limited, too much data if you write once may
cause you do not want to clog occurs. If this is a problem, you can use as a non-blocking IO
open("/dev/...",O_RDWR|O_NONBLOCK). Either way the principles are the same.

</div>

Guess you like

Origin www.cnblogs.com/sdu20112013/p/11234365.html