想要干净整洁的桌面,避免各种线缆杂乱无章,ser2net 是一个解决方案。它运行在一台 Linux 机器上,可以通过配置,将串口转发到网络(如 RAW TCP,Telnet 等)。同时还能解决多人调试同一板卡,需要来回搬移板卡的问题。
前提条件
一台 Linux 机器。不限制 CPU 架构。只要求能够内建串口,或支持 USB 串口。同时拥有网络。
目录
- 安装 ser2net
- 最小配置 ser2net
- 启动 ser2net
- 添加管理端口,并使用管理命令
本文使用 ARMv7 的全志 H3 开发版(OrangePi Zero)完成。
开发版运行 Armbian Bookworm(Debian 12)操作系统。
安装 ser2net
ser2net 可以直接通过 APT 安装:
catboy@orangepizero:~$ sudo apt install -y ser2net
最小配置 ser2net
ser2net 的配置文件是 /etc/ser2net.yaml
。修改该文件:
%YAML 1.1
---
# This is a ser2net configuration file, tailored to be rather
# simple.
#
# Find detailed documentation in ser2net.yaml(5)
# A fully featured configuration file is in
# /usr/share/doc/ser2net/examples/ser2net.yaml.gz
#
# If you find your configuration more useful than this very simple
# one, please submit it as a bugreport
connection: &con0001
accepter: telnet,3001
connector: serialdev,/dev/ttyUSB0,115200N81
options:
kickolduser: true
配置的解释如下:
connection
定义了一个连接。后面的con001
是可以自己更改的连接名称。accepter
定义了接受的传入连接类型,即通过什么途径连接到串口。本实例中,配置的是通过 Telnet 协议,3001 端口连接到串口。connector
定义了一个连接器,即通过传入的连接,连接到什么地方。本实例中,连接到串口,使用串口设备/dev/ttyUSB0
。串口使用 115200,8,N,1 配置。options
声明了连接属性。本实例中,属性kickolduser
作用是当新的 Telnet 连接时,踢掉现有的会话。
启动 ser2net
ser2net 服务通过 systemd 托管。因此需要通过 systemd 启用服务并启动:
catboy@orangepizero:~$ sudo systemctl enable --now ser2net.service
服务启动后,便可以通过 Telnet 连接 Linux 机器的 3001 端口来访问串口。
添加管理端口,并使用管理命令
ser2net 提供了一个管理模式,在管理模式中,可以使用管理命令来查看与设置 ser2net。要添加管理模式,需要修改配置文件。在配置文件中添加如下内容:
admin: &admin
accepter: telnet,3000
完整实例如下:
%YAML 1.1
---
# This is a ser2net configuration file, tailored to be rather
# simple.
#
# Find detailed documentation in ser2net.yaml(5)
# A fully featured configuration file is in
# /usr/share/doc/ser2net/examples/ser2net.yaml.gz
#
# If you find your configuration more useful than this very simple
# one, please submit it as a bugreport
admin: &admin
accepter: telnet,3000
connection: &con0001
accepter: telnet,3001
connector: serialdev,/dev/ttyUSB0,115200N81
options:
kickolduser: true
配置修改后,需要通过 systemd 重新启动 ser2net 服务:
catboy@orangepizero:~$ sudo systemctl restart ser2net.service
重新启动后,便可以通过 Telnet 连接到 Linux 主机的 3000 端口,访问 ser2net 的管理命令行:
catboy@ubuntu:~$ telnet orangepizero.local 3000
Trying 240e:b59:a01:d700:84bc:fd80:9d55:70...
Connected to orangepizero.local.
Escape character is '^]'.
-> help
exit - leave the program.
help - display this help.
version - display the version of this program.
yaml - Go into yaml output mode. In this mode there is no echo or
line processing is done. Some commands are disabled. Output
is yaml, beginning with --- and ending with ... for each
response to a command.
monitor <type> <tcp port> - display all the input for a given port on
the calling control port. Only one direction may be monitored
at a time. The type field may be 'tcp' or 'term' and specifies
whether to monitor data from the net port or from the serial port
Note that data monitoring is best effort, if the controller port
cannot keep up the data will be silently dropped. A controller
may only monitor one thing and a port may only be monitored by
one controller.
monitor stop - stop the current monitor.
disconnect <tcp port> - disconnect the tcp connection on the port.
showport [<tcp port>] - Show information about a port. If no port is
given, all ports are displayed.
showshortport [<tcp port>] - Show information about a port in a one-line
format. If no port is given, all ports are displayed.
setporttimeout <tcp port> <timeout> - Set the amount of time in seconds
before the port connection will be shut down if no activity
has been seen on the port.
setportcontrol <tcp port> <controls>
Dynamically modify the characteristics of the port. These are
immediate and won't live between connections. Valid controls are
DTRHI, DTRLO, RTSHI, and RTSLO.
setportenable <tcp port> <enable state> - Sets the port operation state.
Valid states are:
off - The port is shut down
on - The port is up and all I/O is transferred
reload - Reload the configuration file.
-> showport
port: con0001
accepter: telnet,3001
enable state: on
timeout: 0
device: serialdev,/dev/ttyUSB0,115200N81
device config: ?
device controls: ?
tcp to device state: unconnected
device to tcp state: unconnected
bytes read from device: 0
bytes written to device: 0
->
有关 ser2net 的详细用法,可以参见 man 8 ser2net
手册的描述。其 YAML 配置文件的说明,可以参见 man 5 ser2net.yaml
手册的描述。
学到了