【问题标题】:expected boolean value error in tcl and arraystcl 和数组中的预期布尔值错误
【发布时间】:2018-08-15 08:55:09
【问题描述】:

我的串口 tcl 应用程序进展缓慢,但遇到了另一面墙。

我想创建一个布尔值数组以在 for 循环中进行迭代。

在 for 循环中,DTR 将发送一个串行输出。

下面我有以下代码:

set rs232 [open COM3: r]
fconfigure $rs232 -ttycontrol {DTR 0}

array set values {
0   0
1   1
}

set n [array size values]

set x 0
for {set a 0} {$a <=15} {incr a} {
fconfigure $rs232 -ttycontrol {DTR $values(0)}
wait 1000
fconfigure $rs232 -ttycontrol {DTR $values(1)}
wait 1000
}

我运行它并得到错误:

    expected boolean value but got "$values(0)"

谁能告诉我这是为什么,我该如何解决?

【问题讨论】:

  • 这似乎是一种笨拙的方式来编码“真”和“假”。有效的 Tcl 布尔值在 Tcl_GetBoolean 手册页上进行了说明——false 是 0、“false”、“no”或“off”中的任何一个; true 是 1、“true”、“yes”或“on”中的任何一个。
  • 好的,Glenn 干杯,我查看了 TCL_GetBoolean 页面,但它没有显示示例,我不知道 interp、src、boolPtr 参数是如何输入的。你没有例子吗?
  • set value "false"; if {$value} then {puts OK} else {puts No} -- 我的第一条评论主要是$values(0)是一种非常冗长的写0的方式

标签: serial-port tcl dts


【解决方案1】:

此调用:

fconfigure $rs232 -ttycontrol {DTR $values(0)}

-ttycontrol 的值“DTR $values(0)”传递给fconfigure。调用

fconfigure $rs232 -ttycontrol [list DTR $values(0)]

通过“DTR 0”。

大括号阻止了变量的替换,但list 的调用会强制执行它。

或者,您可以使用其中之一

fconfigure $rs232 -ttycontrol "DTR $values(0)"
fconfigure $rs232 -ttycontrol [subst {DTR $values(0)}]

【讨论】:

  • 感谢彼得非常感谢。我试过添加列表,现在可以了。
  • 我现在在 for 循环中尝试这个并用 i 替换索引值,但它返回 can't read "SHCP(i)": no such element in array。代码是: [code] for {set i 0} {i
  • 你需要学习 Tcl 语法的基础知识——看看tcl.tk/man/tcl8.5/tutorial/Tcl10.html
  • @KennyBarber: $SHCP(i)SHCP 数组的“i”成员的值。 $SHCP($i) 是由“i”命名的数组的成员。请看一下@ColinMacLeod 提到的教程。
  • 科林,我做了for循环,循环i。我现在有另一个问题,所以我会提出一个新问题
猜你喜欢
  • 2020-12-22
  • 2018-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多