为什么字符串匹配在这个“期望”中失败 代码?

发布于 2024-07-19 09:18:30 字数 1380 浏览 3 评论 0原文

我创建了以下过程,期望与安捷伦基站模拟器一起使用:

proc get_bss_parameter_value {bss parameter_string} {
global bss_array

set bss_str "$parameter_string?"

puts "String 1"
set bss_str "oa;$bss_array(gpib):$bss_str\r"
send "$bss_str"
expect {nopattern^}

puts "String 2"
set bss_str "en;$bss_array(gpib)"
puts "Sending bss_str: $bss_str"
send "$bss_str\r"
expect .*

set receive_buffer $expect_out(buffer)
puts "receive_buffer START: $receive_buffer"
puts "END"

return $receive_buffer
}

==================================== ======================= 输出:

字符串 1

>

oa;05:SYST:APPL? 字符串2 发送 bss_str: en;05 “CDMA 2000 实验室应用程序 T”

无法读取“expect_out(buffer)”:没有这样的变量 执行时 “设置 receive_buffer $expect_out(buffer)” (过程“get_bss_parameter_value”第 20 行)

============================================== ============= 如果上面代码中的“.”被替换为“”,则输出为:

String 1

>

oa;05:SYST:APPL? 字符串2 发送 bss_str: en;05 接收缓冲区开始:

oa;05:SYST:APPL?

结束 当前应用程序是

oa;05:SYST:APPL?

问题:
1)我无法在expect_out(buffer)变量中获取值“CDMA 2000 Lab App T”,该变量应与Agilent设备的输出匹配(由于.*)。 代码有问题吗?
2) 在这两种情况下,命令“en;05”被发送但不显示在标准输出上。 尽管我们可以看到第一种情况的预期输出。

I have created the following procedure in expect to work with an Agilent Base Station Simulator:

proc get_bss_parameter_value {bss parameter_string} {
global bss_array

set bss_str "$parameter_string?"

puts "String 1"
set bss_str "oa;$bss_array(gpib):$bss_str\r"
send "$bss_str"
expect {nopattern^}

puts "String 2"
set bss_str "en;$bss_array(gpib)"
puts "Sending bss_str: $bss_str"
send "$bss_str\r"
expect .*

set receive_buffer $expect_out(buffer)
puts "receive_buffer START: $receive_buffer"
puts "END"

return $receive_buffer
}

========================================================
OUTPUT:

String 1

>

oa;05:SYST:APPL?
String 2
Sending bss_str: en;05
"CDMA 2000 Lab App T"

can't read "expect_out(buffer)": no such variable
while executing
"set receive_buffer $expect_out(buffer)"
(procedure "get_bss_parameter_value" line 20)

========================================================
If "." is replaced by "" in the above code, this is the OUTPUT:

String 1

>

oa;05:SYST:APPL?
String 2
Sending bss_str: en;05
receive_buffer START:

oa;05:SYST:APPL?

END
Current application is

oa;05:SYST:APPL?

Problem:
1) I am not able to get the value "CDMA 2000 Lab App T" in the expect_out(buffer) variable which should match (due to .*) the output from the Agilent device. Is there something wrong with the code?
2) In both the cases, the command "en;05" is send but not displayed on stdout. Though we can see the expected output in the first case.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

指尖微凉心微凉 2024-07-26 09:18:30

请注意,[expect] 命令默认情况下执行 glob-style< /a> 匹配,而不是 正则表达式样式。 所以,在你写的地方:

expect .*

你实际上是在寻找一个以文字点开头,后跟任意数量的字符的字符串。 由于这不匹配,expect_out 数组不会被填充。 正如您所提到的,

expect *

其行为如您所料,因为全局模式“*”匹配任何字符串。

如果您确实想要“.*”,那么您需要指定

expect -re .*
set receive_buffer $expect_out(buffer)

Note that the [expect] command by default does glob-style matching, not regexp-style. So, where you write:

expect .*

you are actually looking for a string beginning with a literal dot followed by any number of characters. As this fails to match, the expect_out array does not get populated. As you mention

expect *

behaves as you expect because the glob pattern "*" matches any string.

If you truly want ".*", then you need to specify

expect -re .*
set receive_buffer $expect_out(buffer)
时间海 2024-07-26 09:18:30

我的印象是expect_out变量是一个全局变量。 如果我是正确的,那么您需要以下内容:

global expect_out

或者,您可以通过使用名称空间限定符“::”将其显式引用为全局

set receive_buffer $::expect_out(buffer)

I was under the impression that the expect_out variable was a global. If I'm correct, then you'd need the following:

global expect_out

Or, you can refer to it as a global explicitly by using the namespace qualifier "::"

set receive_buffer $::expect_out(buffer)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文