⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 opc.tcl

📁 opc技术
💻 TCL
字号:
##########################################################
##     OPC Toolbox
##     OPC.tcl
##     Developed by Zhe.Peng & Longhua.Ma
##     Email:pengzhe1113@gmail.com
##     Zhejiang Univ. Hangzhou, China
##     Dec.2006
###########################################################


set DIRE
set wish_v "1"
set text_temp "dfsd"
set list_temp 0
set list_temp_name 0
set w .opc
set item_read_temp(0) 0
catch {destroy $w}
toplevel $w
wm title $w "OPC-TOOLBOX's DEMO"

canvas $w.c -width 800 -height 400 -bg blue

####################### Top frame ###########################

labelframe $w.top -width 524 -height 50 -text "Connect OPC Server"
pack $w.top -padx 0 -pady 0 
pack propagate $w.top false

label $w.top.label_opcname -text "OPC Server:" 
entry $w.top.entry_opcname -textvariable opcserver

button $w.top.opcconnect -width 15 -height 1 -text "Connect" -command {
ScilabEval "call('opc_connect',opc_server_temp,1,'c','out')"
ScilabEval "opc_server_temp='$opcserver'"
$w.top.opcconnect configure -state "disabled"
$w.mid1.addgroup configure -state "normal"
$w.top.opcdisconnect configure -state "normal"
}

button $w.top.opcdisconnect -width 15 -height 1 -text "DisConnect" -command {
ScilabEval "call('opc_disconnect','out')"
$w.top.opcconnect configure -state "normal"
$w.top.opcdisconnect -state "disabled"
}

pack $w.top.label_opcname $w.top.entry_opcname $w.top.opcconnect $w.top.opcdisconnect -side left

####################### Mid1 frame ###########################

labelframe $w.mid1 -width 524 -height 50 -text "Add OPC Group"
pack $w.mid1 -padx 0 -pady 0 
pack propagate $w.mid1 false

label $w.mid1.label_opcname -text "OPC Group:" 
entry $w.mid1.entry_opcname -textvariable opcgroup
button $w.mid1.addgroup -width 15 -height 1 -text "Add Group" -command {
ScilabEval "call('opc_add_group',opc_group_temp,1,'c','out')"
ScilabEval "opc_group_temp='$opcgroup'"
$w.mid3.additem configure -state "normal"
$w.mid1.addgroup configure -state "disabled"
}

pack $w.mid1.label_opcname $w.mid1.entry_opcname $w.mid1.addgroup -side left

####################### Mid2 frame ###########################

labelframe $w.mid2 -width 524 -height 50 -text "Add Items to Listbox "
pack $w.mid2 -padx 0 -pady 0 
pack propagate $w.mid2 false

label $w.mid2.label_opcname -text "OPC Item:" 
entry $w.mid2.entry_opcname -textvariable opcitem
button $w.mid2.additem -width 15 -height 1 -text "Add to Items List" -command {
$w.mid4.l.list insert end $opcitem
}
button $w.mid2.delitem -width 15 -height 1 -text "Del From Items List" -command {
$w.mid4.l.list delete [$w.mid4.l.list curselection]
}

pack $w.mid2.label_opcname $w.mid2.entry_opcname $w.mid2.additem $w.mid2.delitem -side left

####################### mid3 frame ###########################

labelframe $w.mid3 -width 524 -height 50 -text "Add Items in Listbox to OPC Server"
pack $w.mid3 -padx 0 -pady 0 
pack propagate $w.mid3 false

button $w.mid3.additem -width 25 -height 1 -text "Add Items List to OPC Server" -command {

#set item_value [$w.mid4.l.list index end]
set item_t [$w.mid4.l.list index end]
for {set list_num_temp 0} {$list_num_temp<[$w.mid4.l.list index end]} {incr list_num_temp 1} {
ScilabEval "item_$list_num_temp='[$w.mid4.l.list get $list_num_temp]'"
ScilabEval "item_num_$list_num_temp=$list_num_temp"
#ScilabEval "call('copy_item',item_num_$list_num_temp,1,'i',item_$list_num_temp,2,'c','out')"
}

for {set list_num_temp 0} {$list_num_temp<[$w.mid4.l.list index end]} {incr list_num_temp 1} {
ScilabEval "call('copy_item',item_num_$list_num_temp,1,'i',item_$list_num_temp,2,'c','out')"
}

ScilabEval "item_num=$item_t"
ScilabEval "call('opc_add_item',item_num,1,'i','out')"
 $w.mid4.m.b  configure -state "normal"
 $w.mid4.r1.b  configure -state "normal"
 }

pack  $w.mid3.additem -side left -padx 150

####################### mid4 frame ###########################

labelframe $w.mid4 -width 524 -height 200 -text "Read And Write Items"
pack $w.mid4 -padx 0 -pady 0 
pack propagate $w.mid4 false

####################### mid4.l frame ###########################

frame $w.mid4.l -width 140 -height 200 
pack $w.mid4.l -padx 0 -pady 0 
pack propagate $w.mid4.l false

label $w.mid4.l.label -text "Items List"
listbox  $w.mid4.l.list 
$w.mid4.l.list insert 0 {ABB.sawtoothreal8} {ABB.tag123} {ABB.tag234}
pack $w.mid4.l.label $w.mid4.l.list -side top -pady 7

####################### mid4.m frame ###########################

frame $w.mid4.m -width 140 -height 200 
pack $w.mid4.m -padx 0 -pady 0 
pack propagate $w.mid4.m false

button $w.mid4.m.b -text "Read Items" -command {
set item_tt [$w.mid4.l.list index end]
$w.mid4.m.list delete 0 [$w.mid4.m.list index end]
ScilabEval {item_read_result=call('opc_read',item_num,1,'i','out',[1,item_num],2,'r')}

for {set list_num_temp 0} {$list_num_temp<[$w.mid4.l.list index end]} {incr list_num_temp 1} {
ScilabEval "item_read_i=$list_num_temp+1"
ScilabEval "TCL_SetVar('item_read_temp($list_num_temp)',item_read_result(item_read_i))"
$w.mid4.m.list insert end $item_read_temp($list_num_temp)
}
}

listbox  $w.mid4.m.list 
$w.mid4.m.list insert 0 0 0 0

pack $w.mid4.m.b $w.mid4.m.list -side top -pady 5


####################### mid4.r1 frame###########################

frame $w.mid4.r1 -width 80 -height 200
pack $w.mid4.r1 -padx 0 -pady 0 
pack propagate $w.mid4.r1 false

entry $w.mid4.r1.e1 -textvariable item_index
entry $w.mid4.r1.e -textvariable item
label $w.mid4.r1.l -text "Values"
entry $w.mid4.r1.v -textvariable item_value
button $w.mid4.r1.b -text "write" -command {
ScilabEval "item_write_i=$item_index"
ScilabEval "item_write_v=$item_value"
ScilabEval "call('opc_write',item_write_i,1,'i',item_write_v,2,'r','out')"
} 
button $w.mid4.r1.b2 -text "Auto" -command {
time_loop
}

pack  $w.mid4.r1.e1 $w.mid4.r1.e $w.mid4.r1.l $w.mid4.r1.v  $w.mid4.r1.b $w.mid4.r1.b2 -side top -pady 2

####################### mid4.r2 frame###########################

frame $w.mid4.r2 -width 200 -height 200
pack $w.mid4.r2 -padx 0 -pady 0 
pack propagate $w.mid4.r2 false

####################### mid4.r2.top frame###########################

frame $w.mid4.r2.top -width 80 -height 82
pack $w.mid4.r2.top -padx 0 -pady 0 
pack propagate $w.mid4.r2.top false

####################### mid4.r2.bottom frame###########################

frame $w.mid4.r2.bottom -width 200 -height 200
pack $w.mid4.r2.bottom -padx 0 -pady 0 
pack propagate $w.mid4.r2.bottom false

####################### mid4.r2.bottom.l frame###########################

frame $w.mid4.r2.bottom.l -width 85 -height 200
pack $w.mid4.r2.bottom.l -padx 0 -pady 0 
pack propagate $w.mid4.r2.bottom.l false

####################### mid4.r2.bottom.r frame###########################

frame $w.mid4.r2.bottom.r -width 100 -height 200
pack $w.mid4.r2.bottom.r -padx 0 -pady 0 
pack propagate $w.mid4.r2.bottom.r false


pack $w.mid4.r2.top $w.mid4.r2.bottom -side top
pack $w.mid4.r2.bottom.l $w.mid4.r2.bottom.r -side left

####################### packframe ###########################
pack $w.top $w.mid1 $w.mid2 $w.mid3 $w.mid4 -side top
pack  $w.mid4.l $w.mid4.m $w.mid4.r1 $w.mid4.r2 -side left


####################### bind mouse ###########################

bind $w.mid4.l.list <ButtonRelease-1> {
set item_index [$w.mid4.l.list curselection]
set item [$w.mid4.l.list get [$w.mid4.l.list curselection] ]
}

bind $w.mid4.m.list <ButtonRelease-1> {
set item [$w.mid4.l.list get [$w.mid4.m.list curselection] ]
}


#######################initial###########################
set opcserver {Matrikon.OPC.Simulation.1}
set opcgroup group1
$w.mid1.addgroup configure -state "disabled"
$w.mid4.m.b configure -state "disabled"
$w.mid4.r1.b configure -state "disabled"
$w.mid3.additem configure -state "disabled" 
$w.top.opcdisconnect configure -state "disabled" 



proc time_loop {} {
global w
global list_num_temp
global item_read_temp

set item_tt [$w.mid4.l.list index end]
$w.mid4.m.list delete 0 [$w.mid4.m.list index end]
ScilabEval {item_read_result=call('opc_read',item_num,1,'i','out',[1,item_num],2,'i')}

for {set list_num_temp 0} {$list_num_temp<[$w.mid4.l.list index end]} {incr list_num_temp 1} {
ScilabEval "item_read_i=$list_num_temp+1"
ScilabEval "TCL_SetVar('item_read_temp($list_num_temp)',item_read_result(item_read_i))"
$w.mid4.m.list insert end $item_read_temp($list_num_temp)
}
after 1000 time_loop
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -