“\r”在 Cygwin 的 TCL Expect 中被忽略

"\r" is ignored in TCL Expect in Cygwin

我有一个 tcl 脚本可以通过 Cygwin 中的 Expect 访问串口。我注意到 \r 被忽略导致串行控制台没有回复。

spawn ./plink.exe -serial COM$priuart -sercfg 115200,8,n,1,N
set id $spawn_id
set timeout 30
log_user 1 
exp_send -i $id  "\r"
expect -i $id -re ".*>" {exp_send -i $id  "sys rev\r"}
expect -i $id -re ".*>" {set temp $expect_out(buffer)

请注意,通过添加 -o igncr,Cygwin 中的类似问题已得到解决。但是调用tcl脚本问题依旧。

有什么想法吗?

您需要 exp_send 模拟按下 Return 键的确切内容可能会有所不同; \r(回车 return)对于经典 Unix 系统来说是正确的,但在您的情况下可能不正确,特别是因为您最终要与串行线路(可以添加它的自己的复杂层)。您完全有可能需要发送 \n(换行符)或 \r\n(carriage-return/newline 序列)。最简单的事情就是让你去试验看看什么有效。

改东西的时候别忘了把所有用到的地方都改一下

另请注意,Tcl 可以直接与串行线路通信,并告知 spawn 使用已打开的通道。这可能更适合你……

# The name *is* magical, especially for larger port numbers
set channel [open \\.\com$priuart r+]
fconfigure -mode 115200,n,8,1 -buffering none
spawn -open $channel