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

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

我通常使用屏幕(1)打开串口,命令如下:

sudo screen /dev/ttyUSB2 115200

我尝试使用以下命令设置 parenb (以及许多其他东西):

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

然而,系统似乎并不尊重这一点。以下是报告的设置斯特蒂当我在屏幕使用上述命令:

$ 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

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

如果我手动将更改改为 ttyUSB2,则在运行后(或运行过程中)不会生效屏幕

设置括号:

$ sudo stty -F /dev/ttyUSB2 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

您可以看到 parenb 已正确设置。

跑步屏幕

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

再次检查 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

哎呀,又恢复原样了-parenb

这是怎么回事?

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

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

答案1

长话短说,它似乎screen不支持设置这些标志。另一种方法是运行stty以在屏幕连接到端口时设置标志,就像您所做的那样。或者,您可以运行科米特或传统屏幕命令行会话中的另一个终端仿真程序,而不是让屏幕直接连接到串行端口。

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

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

如果您愿意的话,对于有 C 语言经验的开发人员来说,添加对这些选项的支持似乎很简单。

答案2

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

手册页stty

  • csN - 将字符大小设置为 N 位,N 介于 [5..8] 之间
  • [-]parenb - 在输出中生成奇偶校验位并期望在输入中出现奇偶校验位
  • [-]parodd – 设置奇校验(偶数为‘-’)
  • [-]cstopb - 每个字符使用两个停止位(一个带有 '-')

相关内容