/dev/tty /dev/ttyS0

本文详细解释了Linux系统中/dev/tty、/dev/ttyS0及/dev/tty0等终端的区别,包括串行端口终端、控制终端与控制台终端的概念,并介绍了如何通过命令查看当前进程的控制终端。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/dev/tty /dev/ttyS0 /dev/tty0区别  2012-05-14 22:41:49

分类: LINUX

1、串行端口终端(/dev/ttySn)   串行端口终端(Serial Port Terminal)是使用计算机串行端口连接的终端设备。计算机把每个串行端口都看作是一个字符设备。有段时间这些串行端口设备通常被称为终端设备,因为 那时它的最大用途就是用来连接终端。这些串行端口所对应的设备名称是/dev/tts/0(或/dev/ttyS0), /dev/tts/1(或/dev/ttyS1)等,设备号分别是(4,0), (4,1)等,分别对应于DOS系统下的COM1、COM2等。若要向一个端口发送数据,可以在命令行上把标准输出重定向到这些特殊文件名上即可。例如, 在命令行提示符下键入:echo test > /dev/ttyS1会把单词”test”发送到连接在ttyS1(COM2)端口的设备上。
3、控制终端(/dev/tty)   如果当前进程有控制终端(Controlling Terminal)的话,那么/dev/tty就是当前进程的控制终端的设备特殊文件。可以使用命令”ps –ax”来查看进程与哪个控制终端相连。对于你登录的shell,/dev/tty就是你使用的终端,设备号是(5,0)。使用命令”tty”可以查看它 具体对应哪个实际终端设备。/dev/tty有些类似于到实际所使用终端设备的一个联接。   
4、控制台终端(/dev/ttyn, /dev/console)   在Linux 系统中,计算机显示器通常被称为控制台终端 (Console)。它仿真了类型为Linux的一种终端(TERM=Linux),并且有一些设备特殊文件与之相关联:tty0、tty1、tty2 等。当你在控制台上登录时,使用的是tty1。使用Alt+[F1—F6]组合键时,我们就可以切换到tty2、tty3等上面去。tty1–tty6等 称为 虚拟终端,而tty0则是当前所使用虚拟终端的一个别名,系统所产生的信息会发送到该终端上(这时也叫控制台终端)。因此不管当前正在使用哪个虚拟终端, 系统信息都会发送到控制台终端上。
/dev/console即控制台,是与操作系统交互的设备,系统将一些信息直接输出到控制台上。目前只有在单用户模式下,才允许用户登录控制台。 

tty:
如果一个进程有控制终端的话,/dev/tty 就是它的控制终端,这个东西不是固定的,不同的程序打开这个    设备文件可能指向的终端不同。
    #echo "test" > /dev/tty
    test
tty0:
    tty1 –tty6等称为虚拟终端,而tty0则是当前所使用虚拟终端的一个别名,系统所产生的信息会发送到
    该终端上。因此不管当前正在使用哪个虚拟终端,系统信息都会发送到控制台终端上。
    #echo "test" > /dev/tty0
    test
    (注意:好像要在文本模式下才可以)

/dev/tty主要是针对进程来说的,而/dev/tty0是针对整个系统来说的
就是说同是/dev/tty文件,对不同的进程来说,其具体指向是不同的。
但不管对那个进程来说/dev/tty0指向的都是当前的虚拟终端.


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 38.5 The ps Command Chapter 38
Starting, Stopping, and Killing Processes
Next: 38.7 Why ps Prints Some Commands in Parentheses
 

38.6 The Controlling Terminal

In article 38.5 , we pointed out that the ps command needs special options ( -x for BSD and -e for System V) to list processes without a controlling terminal.

But just what is a controlling terminal? Just what it sounds like: the terminal from which the process was started. In the ps listing, this is usually given as a tty , or terminal id. That ps entry usually corresponds to a serial port, or a pty ( 41.8 ) . A pty or "pseudo-terminal" is a construct that makes a window or network login ( 1.33 ) look to the operating system just like a terminal.

In the ps listing, a tty might appear as t1 for /dev/tty1 , p3 for /dev/ttyp3 , or as some other designation, such as co for/dev/console , the full screen display of a workstation before any window system is started. Processes without a controlling terminal show a question mark ( ? ).

How does a process "lose" its controlling terminal? Easy. Some processes, such as system "daemons" ( 1.14 ) never had one - they were started by system scripts that weren't started from any terminal, or they disconnected themselves from their controlling terminals. But it's also possible that you started a process running in the background, logged out, and logged back later or on another terminal to find it still running without a controlling terminal.

The tty command can be used to report which "terminal" you're currently connected to. For example:

% 

tty


/dev/ttyp2

Running tty without a controlling terminal gives the message not a tty .




Explaining The Terminal Welcome Screen3

Terminal

Share
Explaining The Terminal Welcome Screen

Hey

A couple of days ago I received an email asking me to explain Terminal’s welcome screen. For new users this can be quite confusing and many wonder what each parts mean. This post is going to be a quick introduction on what each line means and way it is there. I will also have a couple of links to posts explaining each part in more detail. If you have any questions please leave a comment below.

When you start Terminal you should see something like this.

Terminal Screen Shot

or

Last login: Sat Jan 16 18:34:23 on ttys000
Welcome James
dyn240089:~ James$

The first line:

Last login: Sat Jan 16 18:34:23 on ttys000

is all about when you last logged into your Terminal, this can be semi useful if you use remote login or similar to see when the last log in was. Most of the time I ignore it since I can never remember when I last used Terminal.

The final part ttys000 is the Terminal you have logged into. It comes from when computers were a lot slower and older. You can find more information over on this Computer Terminal wiki page. The ttys, starts at zero and increases as you open more Terminal windows.

The second line:

Welcome James

is you message of the day. I can’t remember the original how ever you can change it by following thispost. You could add something witty, or reduce the amount of space on screen by deleting any of the text.

The final line:

dyn240089:~ James$

is made up of two parts. The first is your computer. This can be named from System Preferences > Sharing, text box at the top of the page. Most of the time this can be changed by you. However sometimes the network you connect to (as is the case with mine) will change it. It can vary sometimes local host is displayed. Keep an eye on it to make your you are using the right Terminal, especially if you have ssh’d or are using remote log in.

The final part, after the colon, is the short name of the current user. This should be who  you are logged in with. If you are using ssh, this will change.

There isn’t a lot too the three lines in Terminal, however for new people this may be quite difficult to understand. If you have any questions please leave a comment. Alternatively you can find more information in some books. I recommend Mac OS X Unix Toolbox and The Mac OS X Command Linefrom Amazon.


### 配置和使用 `/dev/ttyS0` 映射 Linux 设备文件串行端口 #### 1. 查找并确认串行端口的存在 为了确保 `/dev/ttyS0` 存在于系统中,可以使用 `ls` 命令来列出设备节点: ```bash ls -l /dev/ttyS* ``` 这将显示所有可用的 TTY 串行端口。如果未找到 `/dev/ttyS0`,可能是因为硬件连接问题或内核模块加载失败。 #### 2. 使用 Docker 进行串口映射 对于需要在 Docker 容器内部访问主机上的物理串行端口的情况,可以通过 `-device` 参数实现直接映射。具体命令如下所示[^1]: ```bash docker run -it --rm \ --device=/dev/ttyS0:/dev/ttyS0 \ my_image ``` 此操作会把宿主机上真实的 `/dev/ttyS0` 装载到容器内的相同路径下,从而允许应用程序像平常一样读写该设备。 #### 3. 修改 udev 规则以保持一致性 有时重启之后原来的设备名可能会改变(比如从 `/dev/ttyUSB0` 变成其他编号),为了避免这种情况影响程序正常工作,建议创建自定义 UDEV 规则固定分配特定名称给指定 USB-Serial Adapter: 编辑新规则文件 `/etc/udev/rules.d/99-myserial.rules` 并加入下面内容(假设 VID 和 PID 是已知): ```plaintext SUBSYSTEM=="tty", ATTRS{idVendor}=="xxxx", ATTRS{idProduct}=="yyyy", SYMLINK+="my_serial" ``` 替换其中 xxxx yyyy 为实际产品的 Vendor ID 和 Product ID, 然后重新加载规则使更改生效: ```bash sudo udevadm control --reload-rules && sudo udevadm trigger ``` 现在无论何时插入相同的适配器都会自动链接至 `/dev/my_serial`. #### 4. 测试通信功能 完成上述设置后,可利用简单的回送测试工具如 minicom 或者 screen 来验证是否能够成功打开并发送接收数据包. 例如通过 Screen 工具连接到目标串口: ```bash screen /dev/ttyS0 115200 ``` 这里假定波特率为 115200 bps; 如果不确定速率,则需查阅文档获取确切数值.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值