iha*_*ter 123 command-line terminal tty serial-port serial-console
有没有办法像使用 SSH 一样连接到串行终端?一定有比Minicom之类的工具更简单的方法,像这样
$ serial /dev/ttyS0
Run Code Online (Sandbox Code Playgroud)
我知道我可以cat输出,/dev/ttyS0但只有一种方式可以进行通信,从端口到控制台。和echo出端口是一样的,但周围的其他方式,来港。
如何在 Unix/Linux 上以最简单的方式实现与串口的双向通信?
Sha*_*off 89
我找到screen了最有用的串行通信程序,因为无论如何我都将它用于其他事情。它通常只是screen /dev/ttyS0 <speed>,尽管您的设备的默认设置可能会有所不同。它还允许您通过进入命令模式并执行exec !! <run some program that generates output>.
roz*_*acz 58
您需要任何程序(例如minicom通过串行端口进行通信)的主要原因是需要在启动连接之前设置端口。如果没有正确设置,cat和echo命令将无法满足您的预期。请注意,一旦您运行类似 的程序minicom,端口就会保留所使用的设置minicom。您可以使用如下stty程序查询通信设置:
stty < /dev/ttyS0
Run Code Online (Sandbox Code Playgroud)
如果你做得对;在启动计算机之后和运行任何其他程序(如minicom,通信设置)之前,将使用它们的默认设置。这些可能与您建立连接所需的不同。在这种情况下,向端口发送命令cat或echo将产生垃圾或根本不起作用。
使用后stty再次运行,您会注意到设置已设置为程序使用的内容。minicom
基本上,通过串口进行双向通信需要两件事:1)配置串口,2)打开伪tty读写。
我所知道的最基本的程序是picocom. 您还可以使用诸如setserial设置端口之类的工具,然后直接从 shell 与其交互。
iha*_*ter 31
我在这里找到了一种使用 shell 脚本的方法,它将cat作为后台进程和一个读取用户输入并将echo其输出到端口的 while 循环。我将它修改为更通用,它完全符合我的目的。
#!/bin/sh
# connect.sh
# Usage:
# $ connect.sh <device> <port speed>
# Example: connect.sh /dev/ttyS0 9600
# Set up device
stty -F $1 $2
# Let cat read the device $1 in the background
cat $1 &
# Capture PID of background process so it is possible to terminate it when done
bgPid=$!
# Read commands from user, send them to device $1
while read cmd
do
echo "$cmd"
done > $1
# Terminate background read process
kill $bgPid
Run Code Online (Sandbox Code Playgroud)
ktf*_*ktf 29
如果系统上安装了 UUCP,您可以使用命令cu,例如
$ cu -l /dev/ttyS0 -s 9600
Run Code Online (Sandbox Code Playgroud)
小智 23
“tio”是一个简单的 TTY 终端应用程序,它具有简单的命令行界面,可轻松连接到 TTY 设备以进行基本输入/输出。
典型用途是没有选项。例如:
tio /dev/ttyS0
Run Code Online (Sandbox Code Playgroud)
其中对应常用的选项:
tio --baudrate 115200 --databits 8 --flow none --stopbits 1 --parity none /dev/ttyS0
Run Code Online (Sandbox Code Playgroud)
它为所有选项提供完整的 shell 自动完成支持。
Fri*_*itz 19
此脚本基于另一个答案,但通过串行端口发送所有内容(除了Ctrl+Q),而不仅仅是单个命令后跟Enter. 这使您能够在远程主机上使用Ctrl+C或Ctrl+Z,并使用交互式“GUI”程序,如 aptitude 或 alsamixer。按 可以退出Ctrl+Q。
只需将以下代码放入文件/usr/local/bin/femtocom(或 中的任何其他位置$PATH)并执行chmod +x /usr/local/bin/femtocom.
从那里你可以连接到任何这样的串行端口,例如:
femtocom /dev/ttyUSB0 57600
Run Code Online (Sandbox Code Playgroud)
#!/bin/bash
if [[ $# -lt 1 ]]; then
echo "Usage:"
echo " femtocom <serial-port> [ <speed> [ <stty-options> ... ] ]"
echo " Example: $0 /dev/ttyS0 9600"
echo " Press Ctrl+Q to quit"
fi
set -e # Exit when any command fails
# Save settings of current terminal to restore later
original_settings="$(stty -g)"
# Kill background process and restore terminal when this shell exits
trap 'set +e; kill "$bgPid"; stty "$original_settings"' EXIT
# Remove serial port from parameter list, so only stty settings remain
port="$1"; shift
# Set up serial port, append all remaining parameters from command line
stty -F "$port" raw -echo "$@"
# Set current terminal to pass through everything except Ctrl+Q
# * "quit undef susp undef" will disable Ctrl+\ and Ctrl+Z handling
# * "isig intr ^Q" will make Ctrl+Q send SIGINT to this script
stty raw -echo isig intr ^Q quit undef susp undef
echo "Connecting to $port. Press Ctrl+Q to exit."
# Let cat read the serial port to the screen in the background
# Capture PID of background process so it is possible to terminate it
cat "$port" & bgPid=$!
cat >"$port" # Redirect all keyboard input to serial port
Run Code Online (Sandbox Code Playgroud)
小智 6
另一个可能发生的问题是您的用户帐户可能需要设置为“拨出”组才能访问串行端口。
sudo usermod -a -G dialout $USER
Run Code Online (Sandbox Code Playgroud)