使用屏幕访问串行端口时如何设置奇偶校验位

Tom*_*nen 6 linux serial-port gnu-screen

我通常使用screen (1) 通过以下命令打开串行端口:

sudo screen /dev/ttyUSB2 115200
Run Code Online (Sandbox Code Playgroud)

我已经尝试使用以下命令设置 parenb(以及其他一些东西):

sudo screen /dev/ttyUSB2 115200,cs8,parenb,-parodd,-cstopb
Run Code Online (Sandbox Code Playgroud)

但是,该系统似乎并不尊重这一点。以下是当我使用上述命令在屏幕中stty报告的设置:

$ sudo stty -F /dev/ttyUSB2 -a
speed 115200 baud; rows 0; columns 0; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^H; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S;
susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 100; time = 2;
-parenb -parodd cs8 -hupcl -cstopb cread clocal -crtscts
-ignbrk brkint ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl ixon -ixoff -iuclc -ixany -imaxbel -iutf8
-opost -olcuc -ocrnl -onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
-isig -icanon iexten -echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke
Run Code Online (Sandbox Code Playgroud)

请注意,-parenb尽管我在命令行启用了它。

如果我手动对 ttyUSB2 进行更改,则在运行screen之后(或同时)不会受到尊重。

设置括号:

$ sudo stty -F /dev/ttyUSB2 parenb
Run Code Online (Sandbox Code Playgroud)

检查其值:

$ sudo stty -F /dev/ttyUSB2 -a
speed 115200 baud; rows 0; columns 0; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^H; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S;
susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 100; time = 2;
parenb -parodd cs8 -hupcl -cstopb cread clocal -crtscts
-ignbrk brkint ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl ixon -ixoff -iuclc -ixany -imaxbel -iutf8
-opost -olcuc -ocrnl -onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
-isig -icanon iexten -echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke
Run Code Online (Sandbox Code Playgroud)

您可以看到 parenb 设置正确。

运行屏幕

$ sudo screen /dev/ttyUSB2 115200,cs8,parenb,-parodd,-cstopb
Run Code Online (Sandbox Code Playgroud)

再次检查parenb:

$ sudo stty -F /dev/ttyUSB2 -a
speed 115200 baud; rows 0; columns 0; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^H; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S;
susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 100; time = 2;
-parenb -parodd cs8 -hupcl -cstopb cread clocal -crtscts
-ignbrk brkint ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl ixon -ixoff -iuclc -ixany -imaxbel -iutf8
-opost -olcuc -ocrnl -onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
-isig -icanon iexten -echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke
Run Code Online (Sandbox Code Playgroud)

哎呀,又变回原样了-parenb

这是怎么回事?

我将如何修改此命令以使用偶校验(特别是 8e1 而不是默认的 8n1)?

操作系统是带有最新 HWE 的 Ubuntu 12.04.5 LTS。

Ƭᴇc*_*007 8

尝试 sudo screen /dev/ttyUSB2 115200,cs8,parenb,-parodd,-cstopb

手册页stty

  • csN - 将字符大小设置为 N 位,N in [5..8]
  • [-]parenb - 在输出中生成奇偶校验位并在输入中期望奇偶校验位
  • [-]parodd - 设置奇偶校验(即使有“-”)
  • [-]cstopb - 每个字符使用两个停止位(一个带有“-”)


Ken*_*ter 4

长话短说,它看起来好像screen不支持设置这些标志。另一种方法是stty在 screen 连接到端口时运行来设置标志,就像您所做的那样。或者,您可以在传统的 screen 命令行会话中运行kermit或其他终端仿真器程序,而不是让 screen 直接连接到串行端口。

源代码screen位于http://git.savannah.gnu.org/cgit/screen.git。看起来感兴趣的文件是tty.sh。这是在构建过程中运行以生成“tty.c”的 shell 脚本。tty.c 包含访问串行端口的代码。

该函数SttyMode()似乎是解析 tty 选项并设置 tty 模式的函数。在我看来,它处理一小部分固定的选项。“parenb”和“parodd”不在其中。

对于具有 C 经验的开发人员来说,添加对这些选项的支持似乎很简单(如果您可以选择)。