我正在处理一个expect脚本,它将根据硬件返回的值执行一个条件。我想使用expect来自动化这个过程,但是,我不确定我是否要正确地这样做,如下面的代码所示。
#! /bin/expect
set timeout 5
puts "Spawning serial connection"
spawn -open [open /dev/ttys1 w+]
send --"\r"
expect "barebox: /"
send --"?\r"
expect "barebox: /"
#I2C communication to initiate test pattern test
send "i2c_write -b 1 -a 0x50 -r 0x105 -w 0x02 0xf0\r"
expect "barebox: /"
send "i2c_write -b 1 -a 0x50 -r 0x105 -w 0x01 0xf0\r"
expect "barebox: /"
send "i2c_read -b 1 -a 0x50 -r 0x108 -w 0x03 0xh0\r"
#checking to see if value returned matches any of the following conditions
expect{
while{"0x04 0x00}{
put "FPGA still busy/r"
send "i2c_read -b 1 -a 0x50 -r 0x108 -w 0x03 0xh0\r"
}
if{"0x00 0x00}{
put "Test Pattern test successful\r"
}
elseif{"0x08 0x00}{
put "Test Pattern test failed\r"
}
}
put "End of Test Pattern test\r"发布于 2022-06-06 20:57:05
您可以将循环和测试编写为:
expect {
"0x04 0x00" {
puts "FPGA still busy"
send "i2c_read -b 1 -a 0x50 -r 0x108 -w 0x03 0xh0\r"
exp_continue
}
"0x00 0x00" {
puts "Test Pattern test successful"
}
"0x08 0x00" {
puts "Test Pattern test failed"
}
}在循环中有一个延迟可能是明智的,这样就不会用连续的请求来敲碎远程系统。例如,通过在sleep 1之前添加行send,每次可以等待1秒。有关详细信息,请参阅https://www.tcl-lang.org/man/expect5.31/expect.1.html的文档
https://stackoverflow.com/questions/72523130
复制相似问题