Search code examples
cygwintclexpect

"\r" is ignored in TCL Expect in Cygwin


I have a tcl script to access serial port through Expect in Cygwin. I noticed the \r is just ignored causing serial console no reply.

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)

Please note that the similar issue was resolved in Cygwin by adding -o igncr. However, calling tcl script the issue existed still.

Any thoughts?


Solution

  • The exact thing you need to exp_send to simulate the pressing of the Return key can vary; the \r (a carriage return) is the right thing for classic Unix systems, but might not be correct in your case, especially since you're ending up talking to a serial line (which can add its own layer of complexity). It's entirely possible that you'll need to send either \n (a newline character) or \r\n (carriage-return/newline sequence) instead. The simplest thing is for you to experiment and see what works.

    Don't forget when changing things to change it in all the places where it is used.

    Also be aware that Tcl can talk to serial lines directly, and spawn told to use an already opened channel. This might work better for you…

    # 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