问题描述
限时送ChatGPT账号..我想保存通过串行端口传入的 7 个变量.传输从一个空行开始,然后是 7 行,每行由一个变量组成.没有空格,但在每一行末尾都有一个回车.每个变量也可以由空格组成.这是反复进行的.如果空行会引起问题,在我的外部设备中可以省略它.
#!/usr/bin/env 希望控制台显示设置 Term(Port) com5设置术语(模式)9600,n,8,1"设置结果 [列表]设置数据{}proc 接收者 {chan} {设置数据 [获取 $chan]concat {*}[拆分 $data \n]set ::result [拆分 $data "\n"]#puts $data#puts $::result#foreach 元素 $::result {#puts $Element}#puts "元素 0 [lindex $::result 0]"#puts "元素 1 [lindex $::result 1]"返回}set chan [打开 $Term(Port) r+]fconfigure $chan -mode $Term(Mode) -translation binary -buffering none -blocking 0fileevent $chan 可读 [列出接收者 $chan]
puts $data
显示如下:
开始ChME3562264乐3乐4乐6
所有 7 个变量都可见,但中间有空行.Lok4"和Lok6"之间的空行似乎没问题,因为这是一个由空格组成的变量.
我试图用 set ::result [split $data "\n"]
创建一个列表.但这不能正常工作.使用 foreach Element $::result {puts $Element}
控制台显示 7 个变量:
开始ChME3562264乐3乐4.乐6
我在块引用中手动插入了 Lok4 和 Lok6 之间的点,仅用于显示目的.实际上,它是一个仅由空格组成的变量.
尽管它看起来像一个列表,但如果我尝试 puts "元素 0 [lindex $::result 0]"puts "元素 1 [lindex $::result 1]"
显示
元素 0 开始元素 1元素 0 ChME3元素 1元素 0 562264
等等.元素 1 保持为空,元素 0 连续分配给每个变量.所以它显然不是一个列表.但我想知道,为什么 foreach Element $::result {puts $Element}
似乎有效?我需要更改哪些内容才能获得真实列表?
但是我无法检索它.还是我必须创建一个自己的新列表?
使用 gets
检索结果 并使用 split代码>在这一步:
[split [gets $chan] {}]
要隐藏此列表,请将列表值分配给范围超出周围 proc 的变量,例如全局或命名空间变量:
set ::result [split [gets $chan] {}]
上下文:
proc 接收者 {chan} {设置数据 [获取 $chan]set ::result [concat {*}[split $data \n]]# set ::result [split [gets $chan] {}]# 把 $::result;# 调试打印输出返回}
GUI 集成
<块引用>我已经创建了这样一个 GUI,我想在其中放置这些变量进入标签
将您的标签小部件连接到全局变量 ::result
,这样标签就会在 proc 接收器
中的变量发生变化时更新.
label .l -textvar ::result
I want to save 7 variables incoming over a serial port. The transmission starts with an empty line, followed by 7 lines, each consisting of a single variable. No blanks but a carriage return at every line end. Each variable can also consists of blanks. This is carried out repeatedly. If the empty line would cause a problem, it coud be omitted in my external device.
#!/ usr /bin/env wish
console show
set Term(Port) com5
set Term(Mode) "9600,n,8,1"
set result [list]
set data {}
proc receiver {chan} {
set data [gets $chan]
concat {*}[split $data \n]
set ::result [split $data "\n"]
#puts $data
#puts $::result
#foreach Element $::result {
#puts $Element}
#puts "Element 0 [lindex $::result 0]"
#puts "Element 1 [lindex $::result 1]"
return
}
set chan [open $Term(Port) r+]
fconfigure $chan -mode $Term(Mode) -translation binary -buffering none -blocking 0
fileevent $chan readable [list receiver $chan]
puts $data
shows the following:
START
ChME3
562264
Lok3
Lok4
Lok6
All the 7 variables are visible but with empty lines inbetween. The empty line between "Lok4" and "Lok6" seems to be ok, since this is a variable consisting of blanks.
I tried to create a list with set ::result [split $data "\n"]
. But that isn't working properly. With foreach Element $::result {puts $Element}
the console shows the 7 variables:
START
ChME3
562264
Lok3
Lok4
.
Lok6
I have inserted the point between Lok4 and Lok6 manually here in the blockquote just for display purposes. In reality it's a variable consisting of only blanks.
Despite it looks like a list, if I try
puts "Element 0 [lindex $::result 0]"
puts "Element 1 [lindex $::result 1]"
it shows
Element 0 START
Element 1
Element 0 ChME3
Element 1
Element 0 562264
and so on.
Element 1 remains empty and Element 0 is consecutively assigned with each variable.
So it is clearly not a list. But I wonder, why foreach Element $::result {puts $Element}
seems to work? What do I have to change to get a real list?
but I'm unable to retrieve it. Or do I have to create an own new list?
The result is retrieved using gets
and turned into a list using split
in this one step:
[split [gets $chan] {}]
To stash this list away, assign the list value to a variable that is scoped beyond the surrounding proc, e.g., a global or namespace variable:
set ::result [split [gets $chan] {}]
In context:
proc receiver {chan} {
set data [gets $chan]
set ::result [concat {*}[split $data \n]]
# set ::result [split [gets $chan] {}]
# puts $::result; # debug print-out
return
}
GUI integration
I have already created such a GUI where I want to put these variables into labels
Connect your label widget to the global variable ::result
, so the label becomes updated upon changes to the variable in proc receiver
.
label .l -textvar ::result
这篇关于如何将来自串行端口的传入变量保存到列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论