GNU/Linux - tty设备介绍

在 Linux 和其他类 Unix 操作系统中,"TTY "代表 "电传打字机"。最初,TTY 指的是用于文本通信的物理电传打字机终端,但在现代用法中,它一般指在图形环境或基于文本的控制台中用于文本输入和输出的虚拟终端或终端模拟器。

每个 TTY 都提供一个用户界面,通过命令行界面(CLI)与操作系统交互。用户可以使用 TTY 运行命令、启动程序和执行各种任务。

在 Linux 中,TTY 设备在 /dev 目录中表示为特殊文件,如 /dev/tty1, /dev/tty2 等表示虚拟终端,使用文本操作模式。用户可以使用 Ctrl+Alt+Fn 组合键在虚拟终端之间切换,其中 Fn 指 F1 至 F6(或更高,取决于系统配置)功能键。比如我使用Ubuntu,F1键会新开一个图形界面登录窗口,F2键是当前界面,F3 ~ F6,会新开文本模式的tty登录界面。使用用户名和密码登录,登出使用”logout“命令,或者直接关闭终端窗口。每个终端都是独立的session,F1 ~ F6分别对应/dev/tty1 ~ /dev/tty6。

"/dev/pts/0" , "/dev/pts/1" 等表示终端模拟器使用的伪终端。它们用于图形环境或通过 SSH 或 telnet 远程访问系统。每个用户会话通常都有自己的伪终端,你多开一个控制台,就多一个伪终端。

“tty " 前缀是类 Unix 操作系统中用来表示终端设备的惯例。 "/dev/tty "及其变体("/dev/ttyS0"、"/dev/tty1 "等)代表不同类型的终端设备,每种设备都有特定的用途。

In Linux and other Unix-like operating systems, "TTY" stands for "teletypewriter." Originally, TTY referred to physical teletypewriter terminals used for text-based communication, but in modern usage, it generally refers to the virtual terminals or terminal emulators used for text input and output within a graphical environment or a text-based console.

Each TTY provides a user interface to interact with the operating system through a command-line interface (CLI). Users can run commands, launch programs, and perform various tasks using a TTY. 

In Linux, TTY devices are represented as special files in the /dev directory, such as "/dev/tty1",  "/dev/tty2", etc., for virtual terminals in text mode. Users can switch between virtual terminals using the Ctrl+Alt+Fn key combinations, where Fn refers to function keys from F1 to F6 (or higher, depending on the system configuration).

And "/dev/pts/0" ,  "/dev/pts/1" , etc., for pseudo-terminals used by terminal emulators. They are used in graphical environments or when accessing the system remotely via SSH or telnet. Each user session typically has its own pseudo-terminal.

This “tty” prefix is a convention used in Unix-like operating systems to denote terminal devices.   ”/dev/tty“ and its variants (”/dev/ttyS0“, ”/dev/tty1“, etc.) represent different types of terminal devices. Each of these devices serves a specific purpose.

Also there are some special suffixes in different environments. 

比如在x86_64 Ubuntu中,

$ ls /dev/tty*

显示的输出结果:

/dev/tty

/dev/tty0 ~ /dev/tty63

/dev/ttyprintk

/dev/ttyS0 ~ /dev/ttyS31

/dev/ttyUSB0



$ ls /dev/pts/

0  1  ptmx

$ ls /dev/ptmx

/dev/ptmx

tty0 ~ tty63,表示可用的虚拟终端。

ttyS0 ~ ttyS31,表示的是串口设备,serial port (COM0 ~ COM31),表示物理或虚拟串口(physical or virtual serial port)。这么多序号的设备节点都存在,表示的是虚拟串口。这些串口是用来和外部串口设备进行通讯的,比如modems,serial printers等。

ttyprintk是用于调试和监控(debugging and monitoring) kernel-level 事件和信息的(events and messages),将kernel log发送到缓冲区,然后使用dmesg来读取。

这里/dev/ttyUSB0是接了一个设备,serial-to-usb的串口转USB线,这个是真实存在的一个物理串口设备。

/dev/pts/0 ~ /dev/pts1 是伪终端,因为我就开了2个终端窗口,所以是数量是2。

/dev/pts/ptmx以及/dev/ptmx是master pseudo-terminal device,主伪终端设备,用来管理其他伪终端。

在i.MX93 ARM-Linux中:

# ls /dev/tty*

/dev/tty

/dev/tty0 ~ /dev/tty63

/dev/ttyGS0

/dev/ttyLP0 ~ /dev/ttyLP7   



# ls /dev/pt

ptmx  pts/



# ls /dev/ptmx

/dev/ptmx

/dev/ttyGS0 是 USB Gadget Serial device。

/dev/ttyLP0 ~ /dev/ttyLP7,是片上UART端口,DTS使能后出现设备节点。

在i.MX6UL(L) ARM-Linux中:

# ls /dev/pts

无

# ls /dev/ptmx

/dev/ptmx



# ls /dev/tty*

/dev/tty

/dev/tty0 ~ /dev/tty63

/dev/ttyGS0

/dev/ttymxc0 ~ /dev/ttymxc7 /* 片上UART,DTS使能后出现设备节点 */

/dev/ttymxc0 ~ /dev/ttymxc7, 是片上UART端口,DTS使能后出现设备节点 。

在嵌入式系统中,tty设备名会根据主芯片而产生不同。比如上面的/dev/ttymxc0 和/dev/ttyLP0。

“mxc” 表示是i.MX chip,而LP表示Low Power Uart。这是基于不同芯片系列,串行接口设备名称会不同,上面例子是i.MX family of processors的serial interface的terminal device。

除了上面介绍的,还有一些其他的tty类型设备。比如:

 ”/dev/ttyAMA*“ 表示的是树莓派(Raspberry Pi devices)设备的UART serial ports。

"/dev/ttyUSB*" 和 ”/dev/ttyACM*“ 表示USB serial ports和USB modems,和USB串口设备进行通讯。

要识别 TTY 设备,可以使用 tty 命令。它将显示与当前终端会话相关联的 TTY 设备的路径。Linux 中的 tty 命令是一个内置工具,用于显示连接到标准输入的终端的文件名。

To identify TTY devices, you can use the tty command. It will display the path of the TTY device associated with your current terminal session. The tty command in Linux is a built-in utility that displays the file name of the terminal connected to the standard input.

在用户空间的程序,就可以通过/dev/ttyXX的设备文件,虚拟的或真实的,来进行串行通讯。例如,要向串口设备写入数据,有两种方法,一种是通过shell命令,一种是使用代码编程实现。

Shell命令举例,最简单的是直接echo命令,还可以使用printf命令、文件重定向等。

$ echo "123123" > /dev/ttyS1

使用编程的方法,C、Python或Perl语言,需要先调用open函数打开文件,然后使用write函数写入数据。Python的写法:

with open('/dev/ttyX', 'w') as tty:

    tty.write("Hello, world!\n")

读取串口或其他tty设备的数据的话和写入数据类似:

$ cat /dev/ttyS1



# 文件重定向,File redirection

$ cat /dev/ttyS1 > output.txt

编程方法,Python:

with open('/dev/ttyX', 'r') as tty:

    data = tty.read()

    print(data)

C: Using low-level file operations

#include <stdio.h>

#include <fcntl.h>

#include <unistd.h>



int main() {

    int fd = open("/dev/ttyX", O_RDONLY);

    if (fd < 0) {

        perror("Error opening TTY device");

        return 1;

    }

    char buffer[1024];

    ssize_t bytes_read = read(fd, buffer, sizeof(buffer));

    if (bytes_read < 0) {

        perror("Error reading from TTY device");

        close(fd);

        return 1;

    }

    printf("Read %zd bytes: %s\n", bytes_read, buffer);

    close(fd);

    return 0;

}

请记住,从 TTY 设备读取数据通常需要等待用户输入或来自外部设备的数据,因此在数据可用之前,读取操作可能会阻塞。此外,确保拥有适当的权限来读取 TTY 设备文件。

Remember that reading from TTY devices often involves waiting for user input or data from external devices, so reading operations may block until data is available. Additionally, ensure that you have appropriate permissions to read from the TTY device file.

你可以使用系统调用(如 C 语言中的 ioctl())或命令行工具(如 stty)以编程方式控制 TTY 设备,以操作终端设置。请记住,在底层与 TTY 设备交互可能需要管理权限,直接操作 TTY 可能会破坏系统功能或造成意想不到的后果。在使用 TTY 设备等系统级组件时务必小心谨慎。

You can control TTY devices programmatically using system calls like ioctl() in C or by using command-line tools like stty to manipulate terminal settings. Keep in mind that interacting with TTY devices at a low level might require administrative privileges, and manipulating TTYs directly can potentially disrupt the functioning of your system or cause unintended consequences. Always proceed with caution when working with system-level components like TTY devices.

总的来说,Linux 中的 TTY 是指用户通过基于文本的输入和输出与操作系统交互的界面。另外,通过多种不同的 /dev/tty 设备,Linux 就能支持各种终端和串行通信方案,包括基于文本的控制台交互、串行设备通信、虚拟终端和远程访问。

Overall, TTY in Linux refers to the interface through which users interact with the operating system via text-based input and output.  And the presence of multiple ”/dev/tty“ devices allows Linux to support a wide range of terminal and serial communication scenarios, including text-based console interaction, serial device communication, virtual terminals, and remote access.

  • 14
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜流冰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值