####################################################################################################
##     SciSCADA Toolbox
##     Application_acquision.tcl
##     Developed by Zhe.Peng & Longhua.Ma
##     Email:pengzhe1113@gmail.com
##     Hangzhou, China
##     Sep.2006
####################################################################################################

source combobox.tcl
package require combobox 2.3
catch {namespace import combobox::*}

set DIRE
set status "status"
set sql_result "result"

set local_time "00:00:00"


set proc_temp main

set w .application_main
catch {destroy $w}
toplevel $w
wm title $w "SciSCADA's application in water treatment:Data Acquision"
wm state $w zoom 


canvas $w.c -width 1020 -height 758 -bg blue

####################### menu ###########################

		menu $w.menu -tearoff 0

	set mfile $w.menu.file
	menu $mfile -tearoff 0
	$w.menu add cascade -label "File" -menu $mfile -underline 0
	$mfile add command -label "Open" 
	$mfile add separator
	$mfile add command -label "Quit" -command exit

	set minterface $w.menu.interface
	menu $minterface -tearoff 0 
	$w.menu add cascade -label "Interface" -menu $minterface -underline 0
	
	$minterface add command -label "Main" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/main.tcl')" 
	}
 
	$minterface add command -label "OPC" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/opc.tcl')" 
	}
 
	$minterface add command -label "Modbus" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/modbus.tcl')" 
	}

	$minterface add command -label "Ethernet" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/ethernet.tcl')" 
	}

	$minterface add command -label "Mysql" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/mysql.tcl')" 
	}

	$minterface add command -label "Serial" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/serial.tcl')" 
	}


	set mapplication $w.menu.application
	menu $mapplication -tearoff 0 
	$w.menu add cascade -label "Application" -menu $mapplication -underline 0
	$mapplication add command -label "Intro to Tong Lu's water supply"  -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/application_about.tcl')" 
	}

	$mapplication add command -label "Main" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/Application_main.tcl')" 
	}

	$mapplication add command -label "Networks" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/application_net.tcl')" 
	}

	$mapplication add command -label "Data Acquisition" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/application_acquision.tcl')" 
	}

	$mapplication add command -label "Data Monitor" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/application_monitor.tcl')" 
	}

	

	set mhelp $w.menu.mhelp
	menu $mhelp -tearoff 0
	$w.menu add cascade -label "Help" -menu $mhelp -underline 0
	
	$mhelp add cascade -label "Source codes" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/source.tcl')" 
	}
	

	
	$mhelp add command -label "Help" -command {
        set tmp [file join $DIRE index.htm]
        regsub -all "/" $tmp "\\" tmp
        exec $env(COMSPEC) /c $tmp &
	}
	
	
	$mhelp add command -label "About" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/about.tcl')" 
	}
	 
	$w configure -menu $w.menu 
#######################top frame###########################

frame $w.top -width 1024 -height 640
pack $w.top -padx 0 -pady 0
pack propagate $w.top false 


#######################top.left frame###########################

frame $w.top.left -width 512 -height 640
pack $w.top.left -padx 0 -pady 0
pack propagate $w.top.left false 



#######################top.left.top frame###########################

labelframe $w.top.left.top -width 512 -height 400 -text "OPC"
pack $w.top.left.top -padx 0 -pady 0
pack propagate $w.top.left.top false 



#######################top.left.top.middle1 -frame###########################

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

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

button $w.top.left.top.middle1.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.left.top.middle1.opcconnect configure -state "disabled"
$w.top.left.top.middle2.addgroup configure -state "normal"
$w.top.left.top.middle1.opcdisconnect configure -state "normal"
}

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

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

#######################top.left.top.middle2 frame###########################

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

label $w.top.left.top.middle2.label_opcname -text "OPC Group:" 
entry $w.top.left.top.middle2.entry_opcname -textvariable opcgroup
button $w.top.left.top.middle2.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.top.left.top.middle4.additem configure -state "normal"
$w.top.left.top.middle2.addgroup configure -state "disabled"
}

pack $w.top.left.top.middle2.label_opcname $w.top.left.top.middle2.entry_opcname $w.top.left.top.middle2.addgroup -side left

#######################ÉÏtop.left.top.middle3 frame###########################

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

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

pack $w.top.left.top.middle3.label_opcname $w.top.left.top.middle3.entry_opcname $w.top.left.top.middle3.additem $w.top.left.top.middle3.delitem -side left

#######################top.left.top.middle4 frame###########################

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

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

#set item_value [$w.top.left.top.middle5.l.list index end]
set item_t [$w.top.left.top.middle5.l.list index end]
for {set list_num_temp 0} {$list_num_temp<[$w.top.left.top.middle5.l.list index end]} {incr list_num_temp 1} {
ScilabEval "item_$list_num_temp='[$w.top.left.top.middle5.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.top.left.top.middle5.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.top.left.top.middle5.m.b  configure -state "normal"
 $w.top.left.top.middle5.r.b  configure -state "normal"
 }

pack  $w.top.left.top.middle4.additem -side left -padx 150

#######################top.left.top.middle5 frame###########################

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


#######################top.left.top.middle5.l frame###########################

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

label $w.top.left.top.middle5.l.label -text "Items List"
listbox  $w.top.left.top.middle5.l.list 
$w.top.left.top.middle5.l.list insert 0 UNIT1.TRAIN1.NODE1.TAG1.SP UNIT1.TRAIN1.NODE1.TAG2.SP

pack $w.top.left.top.middle5.l.label $w.top.left.top.middle5.l.list -side top -pady 7

#######################top.left.top.middle5.m -frame###########################

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

button $w.top.left.top.middle5.m.b -text "Read Items" -command {
set item_tt [$w.top.left.top.middle5.l.list index end]
$w.top.left.top.middle5.m.list delete 0 [$w.top.left.top.middle5.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.top.left.top.middle5.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.top.left.top.middle5.m.list insert end $item_read_temp($list_num_temp)
}
}

listbox  $w.top.left.top.middle5.m.list 
$w.top.left.top.middle5.m.list insert 0 1 2 3

pack $w.top.left.top.middle5.m.b $w.top.left.top.middle5.m.list -side top -pady 0



#######################top.left.top.middle5.r -frame###########################

frame $w.top.left.top.middle5.r -width 140 -height 200 -pady 10
pack $w.top.left.top.middle5.r -padx 0 -pady 0 
pack propagate $w.top.left.top.middle5.r false

entry $w.top.left.top.middle5.r.e1 -textvariable item_index
entry $w.top.left.top.middle5.r.e -textvariable item
label $w.top.left.top.middle5.r.l -text "Values"
entry $w.top.left.top.middle5.r.v -textvariable item_value
button $w.top.left.top.middle5.r.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')"

} 

pack  $w.top.left.top.middle5.r.e1 $w.top.left.top.middle5.r.e $w.top.left.top.middle5.r.l $w.top.left.top.middle5.r.v  $w.top.left.top.middle5.r.b -side top -pady 0







#######################top.left.buttom frame###########################

labelframe $w.top.left.buttom -width 512 -height 250 -text "Mysql"
pack $w.top.left.buttom -padx 0 -pady 0
pack propagate $w.top.left.buttom false 




#######################top.left.buttom.middle1 frame###########################

labelframe $w.top.left.buttom.middle1 -width 524 -height 100 -text "Connect"
pack $w.top.left.buttom.middle1 -padx 0 -pady 1 
pack propagate $w.top.left.buttom.middle1 false

#######################top.left.buttom.middle1.left frame###########################

frame $w.top.left.buttom.middle1.left -width 150 -height 200
pack $w.top.left.buttom.middle1.left -padx 0 -pady 1 
pack propagate $w.top.left.buttom.middle1.left false

label $w.top.left.buttom.middle1.left.l1 -text "Host:"
label $w.top.left.buttom.middle1.left.l2 -text "Username:"
label $w.top.left.buttom.middle1.left.l3 -text "Password:"
label $w.top.left.buttom.middle1.left.l4 -text "Database:"

#######################.top.left.buttom.middle1.middle frame###########################

frame $w.top.left.buttom.middle1.middle -width 150 -height 200
pack $w.top.left.buttom.middle1.middle -padx 0 -pady 1 
pack propagate $w.top.left.buttom.middle1.middle false

entry $w.top.left.buttom.middle1.middle.e1 -textvariable host
entry $w.top.left.buttom.middle1.middle.e2 -textvariable user
entry $w.top.left.buttom.middle1.middle.e3 -textvariable pwd
entry $w.top.left.buttom.middle1.middle.e4 -textvariable db

#######################top.left.buttom.middle1.right frame###########################

frame $w.top.left.buttom.middle1.right -width 150 -height 200
pack $w.top.left.buttom.middle1.right -padx 0 -pady 1 
pack propagate $w.top.left.buttom.middle1.right false

button $w.top.left.buttom.middle1.right.button -text "Connect" -command {
ScilabEval "sql_host='$host'" 
ScilabEval "sql_user='$user'" 
ScilabEval "sql_pwd='$pwd'" 
ScilabEval "sql_db='$db'" 
ScilabEval {sql_temp_flag=call('sql_connect',sql_host,1,'c',sql_user,2,'c',sql_pwd,3,'c',sql_db,4,'c','out',[1,1],5,'i')}
}

#######################top.left.buttom.middle2 frame###########################

labelframe $w.top.left.buttom.middle2 -width 524 -height 40 -text "Status"
pack $w.top.left.buttom.middle2 -padx 0 -pady 1 
pack propagate $w.top.left.buttom.middle2 false

label $w.top.left.buttom.middle2.label -text $status

#######################top.left.buttom.buttom frame###########################

labelframe $w.top.left.buttom.buttom -width 524 -height 200 -text "Operation"
pack $w.top.left.buttom.buttom -padx 0 -pady 1 
pack propagate $w.top.left.buttom.buttom false

entry $w.top.left.buttom.buttom.entry -textvariable entrysql
button $w.top.left.buttom.buttom.button -text "Execute" -command {
ScilabEval {sql_result=call('sql_query',sql_exp,1,'c','out',[10,100],2,'c')}
ScilabEval "sql_exp='$entrysql'"
ScilabEval "TCL_SetVar('sql_result',sql_result)"
}

label $w.top.left.buttom.buttom.label -text $sql_result






#######################top.right frame###########################

frame $w.top.right -width 512 -height 640
pack $w.top.right -padx 0 -pady 0
pack propagate $w.top.right false 




#######################top.right.top frame###########################

labelframe $w.top.right.top -width 512 -height 360 -text "Modbus ASCII"
pack $w.top.right.top -padx 0 -pady 0
pack propagate $w.top.right.top false 



#######################top.right.top.m1 frame###########################

labelframe $w.top.right.top.m1 -width 524 -height 120 -text "Serial Configure"
pack $w.top.right.top.m1 -padx 0 -pady 0 
pack propagate $w.top.right.top.m1 false

#######################top.right.top.m1.top frame###########################

frame $w.top.right.top.m1.top -width 524 -height 40
pack $w.top.right.top.m1.top -padx 0 -pady 0 
pack propagate $w.top.right.top.m1.top false

label $w.top.right.top.m1.top.l1 -text "Port:"
label $w.top.right.top.m1.top.l2 -text "Baurdate:"
combobox $w.top.right.top.m1.top.c1 -textvariable port
combobox $w.top.right.top.m1.top.c2 -textvariable baurd

$w.top.right.top.m1.top.c1 list insert end "1" 
$w.top.right.top.m1.top.c1 list insert end "2" 
$w.top.right.top.m1.top.c1 list insert end "3" 
$w.top.right.top.m1.top.c1 list insert end "4" 

$w.top.right.top.m1.top.c2 list insert end "4800" 
$w.top.right.top.m1.top.c2 list insert end "9600" 
$w.top.right.top.m1.top.c2 list insert end "19200" 

#######################top.right.top.m1.buttom frame###########################

frame $w.top.right.top.m1.buttom -width 524 -height 40
pack $w.top.right.top.m1.buttom -padx 0 -pady 0 
pack propagate $w.top.right.top.m1.buttom false

label $w.top.right.top.m1.buttom.l1 -text "Nbit"
label $w.top.right.top.m1.buttom.l2 -text "Stop"
label $w.top.right.top.m1.buttom.l3 -text "Parity"

combobox $w.top.right.top.m1.buttom.c1 -textvariable nbit
combobox $w.top.right.top.m1.buttom.c2 -textvariable stop
combobox $w.top.right.top.m1.buttom.c3 -textvariable parity

$w.top.right.top.m1.buttom.c1 list insert end "8"
$w.top.right.top.m1.buttom.c1 list insert end "7"

$w.top.right.top.m1.buttom.c2 list insert end "1"
$w.top.right.top.m1.buttom.c2 list insert end "0"

$w.top.right.top.m1.buttom.c3 list insert end "0"
$w.top.right.top.m1.buttom.c3 list insert end "1"
$w.top.right.top.m1.buttom.c3 list insert end "2"

#######################top.right.top.m1.buttom2 frame###########################

frame $w.top.right.top.m1.buttom2 -width 524 -height 40
pack $w.top.right.top.m1.buttom2 -padx 0 -pady 0 
pack propagate $w.top.right.top.m1.buttom2 false

button $w.top.right.top.m1.buttom2.button -text "Connect" -command {
#ScilabEval "handle_serial = -1"
ScilabEval "handle_serial = opencom('Port',$port,'Baudrate',$baurd,'Nbits',$nbit,'Parity',$parity,'Stopbits',$stop,'Protocol',0,'Timeout',1000)"
$w.top.right.top.m3.c configure -state "normal"
$w.top.right.top.m1.buttom2.button configure -state "disabled"
$w.top.right.top.m1.buttom2.button2 configure -state "normal"
}

#after(1)
#sleep 1
#continue
#showMessageBox1 $w

#
#ScilabEval "TCL_H_temp=handle_serial"
#ScilabEval "TCL_SetVar('open_result',handle_serial)"
#ScilabEval "TCL_SetVar('open_result',opencom('Port',$port,'Baudrate',$baurd,'Nbits',$nbit,'Parity',$parity,'Stopbits',$stop,'Protocol',0,'Timeout',1000))"
#if {$open_result > 0} {
#showMessageBox1 $w
#} else {
#showMessageBox2 $w
#}

button $w.top.right.top.m1.buttom2.button2 -text "Disconnect" -command { 
ScilabEval "closecom(getcomhandles())"
$w.top.right.top.m3.c configure -state "disabled"
$w.top.right.top.m1.buttom2.button2 configure -state "disabled"
$w.top.right.top.m1.buttom2.button configure -state "normal"
}

pack $w.top.right.top.m1.buttom2.button $w.top.right.top.m1.buttom2.button2 -side left -padx 100

#######################top.right.top.m2 frame###########################

labelframe $w.top.right.top.m2 -width 524 -height 80 -text "Modbus Configure"
pack $w.top.right.top.m2 -padx 0 -pady 0 
pack propagate $w.top.right.top.m2 false


#######################top.right.top.m2.top frame###########################

frame $w.top.right.top.m2.top -width 524 -height 40
pack $w.top.right.top.m2.top -padx 0 -pady 0 
pack propagate $w.top.right.top.m2.top false

label $w.top.right.top.m2.top.l1 -text "Station"
label $w.top.right.top.m2.top.l2 -text "Function"
entry $w.top.right.top.m2.top.e1 -textvariable station
combobox $w.top.right.top.m2.top.e2 -textvariable function

$w.top.right.top.m2.top.e2 list insert end "send_ascii_1"
$w.top.right.top.m2.top.e2 list insert end "send_ascii_2"
$w.top.right.top.m2.top.e2 list insert end "send_ascii_3"
$w.top.right.top.m2.top.e2 list insert end "send_ascii_4"
$w.top.right.top.m2.top.e2 list insert end "send_ascii_5"
$w.top.right.top.m2.top.e2 list insert end "send_ascii_6"

#######################top.right.top.m2.buttom frame###########################

frame $w.top.right.top.m2.buttom -width 524 -height 40
pack $w.top.right.top.m2.buttom -padx 0 -pady 0 
pack propagate $w.top.right.top.m2.buttom false

label $w.top.right.top.m2.buttom.l1 -text "Address"
label $w.top.right.top.m2.buttom.l2 -text "Length(Value)"
entry $w.top.right.top.m2.buttom.e1 -textvariable address
entry $w.top.right.top.m2.buttom.e2 -textvariable length


#######################top.right.top.m3 frame###########################

labelframe $w.top.right.top.m3 -width 524 -height 50
pack $w.top.right.top.m3 -padx 0 -pady 0 
pack propagate $w.top.right.top.m3 false

button $w.top.right.top.m3.c -text "Send " -command {
ScilabEval "tcl_temp1=$station" 
ScilabEval "tcl_temp2=$address" 
ScilabEval "tcl_temp3=$length" 
ScilabEval "tcl_temp4='$function'" 
ScilabEval {tcl_temp0=call(tcl_temp4,tcl_temp1,1,'i',tcl_temp2,2,'i',tcl_temp3,3,'i','out',[1,17],4,'c')}
ScilabEval "TCL_SetVar('modbus_text',tcl_temp0)" 
ScilabEval {[inbytes,ok,nbytes] = readbincom(handle_serial,30)} 
ScilabEval {ok = writecom(handle_serial,tcl_temp0)}
ScilabEval {[inbytes,ok,nbytes] = readbincom(handle_serial,16)}
ScilabEval {ascii_inbytes=ascii(inbytes)} 
ScilabEval "TCL_SetVar('result_16',inbytes)" 
ScilabEval "TCL_SetVar('result_ascii',ascii_inbytes)" 

#ScilabEval "xselect()"
}
 
entry  $w.top.right.top.m3.e -textvariable modbus_text

pack $w.top.right.top.m3.c $w.top.right.top.m3.e -side left -padx 80

#######################top.right.top.m4 frame###########################

labelframe $w.top.right.top.m4 -width 524 -height 90 -text "Result"
pack $w.top.right.top.m4 -padx 0 -pady 0 
pack propagate $w.top.right.top.m4 false

#######################top.right.top.m4.top frame###########################

frame $w.top.right.top.m4.top -width 524 -height 40 
pack $w.top.right.top.m4.top -padx 0 -pady 0 
pack propagate $w.top.right.top.m4.top false

label $w.top.right.top.m4.top.l -text "ASCII"
entry $w.top.right.top.m4.top.e -textvariable result_ascii

#######################top.right.top.m4.buttom frame###########################

frame $w.top.right.top.m4.buttom -width 524 -height 50
pack $w.top.right.top.m4.buttom -padx 0 -pady 0 
pack propagate $w.top.right.top.m4.buttom false

button $w.top.right.top.m4.buttom.l -text "Analying Data"  -command {
ScilabEval {tcl_temp6=call('ascii_anlys','ascii_inbytes',1,'c','out',[1,10],4,'i')}
ScilabEval "TCL_SetVar('result_int',strcat(string(tcl_temp6)))" 
ScilabEval {tcl_temp6=call('ascii_anlys','ascii_inbytes',1,'c','out',[1,10],4,'i')}

}
 
entry $w.top.right.top.m4.buttom.e -textvariable result_int













#######################top.right.buttom frame###########################

labelframe $w.top.right.buttom -width 512 -height 330 -text "Ethernet"
pack $w.top.right.buttom -padx 0 -pady 0
pack propagate $w.top.right.buttom false 



#######################top.right.buttom.m1 frame###########################

labelframe $w.top.right.buttom.m1 -width 256 -height 300 -text "Configure"
pack $w.top.right.buttom.m1 -padx 0 -pady 0 
pack propagate $w.top.right.buttom.m1 false

#######################top.right.buttom.m1.t1 frame###########################

frame $w.top.right.buttom.m1.t1 -width 524 -height 50
pack $w.top.right.buttom.m1.t1 -padx 0 -pady 0 
pack propagate $w.top.right.buttom.m1.t1 false

label $w.top.right.buttom.m1.t1.l -text "Host:"
entry $w.top.right.buttom.m1.t1.e -textvariable e_host

pack $w.top.right.buttom.m1.t1.l $w.top.right.buttom.m1.t1.e -side left


#######################top.right.buttom.m1.t0 frame###########################

frame $w.top.right.buttom.m1.t0 -width 524 -height 50
pack $w.top.right.buttom.m1.t0 -padx 0 -pady 0 
pack propagate $w.top.right.buttom.m1.t0 false

label $w.top.right.buttom.m1.t0.l -text "Port:"
entry $w.top.right.buttom.m1.t0.e -textvariable e_port

pack $w.top.right.buttom.m1.t0.l $w.top.right.buttom.m1.t0.e -side left

#######################top.right.buttom.m1.t2 frame###########################

frame $w.top.right.buttom.m1.t2 -width 524 -height 50
pack $w.top.right.buttom.m1.t2 -padx 0 -pady 0 
pack propagate $w.top.right.buttom.m1.t2 false

label $w.top.right.buttom.m1.t2.l -text "Function:"
entry $w.top.right.buttom.m1.t2.e -textvariable e_function

pack $w.top.right.buttom.m1.t2.l $w.top.right.buttom.m1.t2.e -side left

#######################top.right.buttom.m1.t3 frame###########################

frame $w.top.right.buttom.m1.t3 -width 524 -height 50
pack $w.top.right.buttom.m1.t3 -padx 0 -pady 0 
pack propagate $w.top.right.buttom.m1.t3 false

label $w.top.right.buttom.m1.t3.l -text "Address:"
entry $w.top.right.buttom.m1.t3.e -textvariable e_address

pack $w.top.right.buttom.m1.t3.l $w.top.right.buttom.m1.t3.e -side left

#######################top.right.buttom.m1.t4 frame###########################

frame $w.top.right.buttom.m1.t4 -width 524 -height 50
pack $w.top.right.buttom.m1.t4 -padx 0 -pady 0 
pack propagate $w.top.right.buttom.m1.t4 false

label $w.top.right.buttom.m1.t4.l -text "Length:"
entry $w.top.right.buttom.m1.t4.e -textvariable e_length

pack $w.top.right.buttom.m1.t4.l $w.top.right.buttom.m1.t4.e -side left



#######################top.right.buttom.m2 frame###########################

frame $w.top.right.buttom.m2 -width 256 -height 300
pack $w.top.right.buttom.m2 -padx 0 -pady 0 
pack propagate $w.top.right.buttom.m2 false


#######################top.right.buttom.m2.m2 frame###########################

labelframe $w.top.right.buttom.m2.m2 -width 524 -height 120 -text "Construct Code"
pack $w.top.right.buttom.m2.m2 -padx 0 -pady 0 
pack propagate $w.top.right.buttom.m2.m2 false



button $w.top.right.buttom.m2.m2.b -text "Construct Code" -command {
ScilabEval "e_function=$e_function" 
ScilabEval "e_address=$e_address" 
ScilabEval "e_length=$e_length" 
ScilabEval "e_host='$e_host'" 
ScilabEval "e_port=$e_port" 
ScilabEval {modbus_char=call('modbus_construct',e_function,1,'i',e_address,2,'i',e_length,3,'i','out',[1,12],4,'i')}
ScilabEval "TCL_SetVar('send_code',strcat(string(modbus_char)))" 
ScilabEval {modbus_char=call('modbus_construct',e_function,1,'i',e_address,2,'i',e_length,3,'i','out',[1,12],4,'i')}
ScilabEval "e_function=$e_function" 
ScilabEval "e_address=$e_address" 
ScilabEval "e_length=$e_length" 
ScilabEval "e_host='$e_host'" 
ScilabEval "e_port=$e_port" 
}

entry $w.top.right.buttom.m2.m2.c -textvariable send_code 

pack $w.top.right.buttom.m2.m2.b $w.top.right.buttom.m2.m2.c -side top

#######################top.right.buttom.m2.m3 frame###########################

labelframe $w.top.right.buttom.m2.m3 -width 524 -height 120 -text "Send Code"
pack $w.top.right.buttom.m2.m3 -padx 0 -pady 0 
pack propagate $w.top.right.buttom.m2.m3 false



button $w.top.right.buttom.m2.m3.b -text "Send Code" -command {
#ScilabEval {r_char=call('modbus_tcp',host,1,'c',port,2,'i','out',[1,12],3,'c')}
ScilabEval "TCL_SetVar('result',strcat(string(r_char)))" 
ScilabEval {r_char=call('modbus_tcp',host,1,'c',port,2,'i','out',[1,12],3,'i')}
}
entry $w.top.right.buttom.m2.m3.c -textvariable result

pack $w.top.right.buttom.m2.m3.b $w.top.right.buttom.m2.m3.c -side top





#######################buttom frame###########################

frame $w.buttom -width 1024 -height 30
pack $w.buttom -padx 0 -pady 0
pack propagate $w.buttom false
button $w.buttom.cmd0 -width 32 -height 2 -text "Main" -command {ScilabEval "TCL_EvalFile(DIR+'application_main.tcl')"}
button $w.buttom.cmd1 -width 32 -height 2 -text "Network frame"  -command {ScilabEval "TCL_EvalFile(DIR+'application_net.tcl')"}
button $w.buttom.cmd2 -width 35 -height 2 -text "Data Acquisition and Storage"  -command {ScilabEval "TCL_EvalFile(DIR+'application_acquision.tcl')"}
button $w.buttom.cmd3 -width 32 -height 2 -text "Data Monitor"  -command {ScilabEval "TCL_EvalFile(DIR+'application_monitor.tcl')"}
button $w.buttom.cmd4 -width 27 -height 2 -text "About"  -command {ScilabEval "TCL_EvalFile(DIR+'application_about.tcl')"}


#######################statusbar frame###########################

frame $w.statusbar -width 1020 -height 20 
pack $w.statusbar -padx 3 -pady 3
pack propagate $w.statusbar false
label $w.statusbar.l1 -text "Local time: "
label $w.statusbar.time -textvar local_time -fg red
label $w.statusbar.l2 -text " | "
label $w.statusbar.author -text "Designed by Zhe.Peng Longhua.Ma ZheJiang Univ. china" -fg green
label $w.statusbar.l3 -text " | "
label $w.statusbar.email -text "pengzhe1113@gmail.com" -fg blue
pack  $w.statusbar.email $w.statusbar.l3 $w.statusbar.author $w.statusbar.l2 $w.statusbar.time $w.statusbar.l1  -side right

proc time_loop {} {
global local_time
set m [expr { [clock seconds] - [clock scan 00:00:00] }]
set local_time [format %2.2d:%2.2d:%2.2d [expr {$m/3600}] [expr {($m%3600)/60}] [expr {($m%3600)%60}]]
after 1000 time_loop
}

time_loop 


#######################packframe###########################

pack $w.top $w.buttom  -side top
pack $w.buttom.cmd0 $w.buttom.cmd1 $w.buttom.cmd2 $w.buttom.cmd3 $w.buttom.cmd4 -side left

pack $w.top.left $w.top.right -side left
pack $w.top.left.top $w.top.left.buttom -side top
pack $w.top.left.top.middle1 $w.top.left.top.middle2 $w.top.left.top.middle3 $w.top.left.top.middle4 $w.top.left.top.middle5 -side top
pack  $w.top.left.top.middle5.l $w.top.left.top.middle5.m $w.top.left.top.middle5.r -side left


pack $w.top.right.top.m1 $w.top.right.top.m2 $w.top.right.top.m3 $w.top.right.top.m4 -side top
pack $w.top.right.top.m1.top  $w.top.right.top.m1.buttom  $w.top.right.top.m1.buttom2 -side top
pack $w.top.right.top.m1.top.l1 $w.top.right.top.m1.top.c1 $w.top.right.top.m1.top.l2 $w.top.right.top.m1.top.c2 -side left
pack $w.top.right.top.m1.buttom.l1  $w.top.right.top.m1.buttom.c1 $w.top.right.top.m1.buttom.l2 $w.top.right.top.m1.buttom.c2 $w.top.right.top.m1.buttom.l3 $w.top.right.top.m1.buttom.c3 -side left
pack $w.top.right.top.m2.top $w.top.right.top.m2.buttom -side top
pack $w.top.right.top.m2.top.l1 $w.top.right.top.m2.top.e1 $w.top.right.top.m2.top.l2 $w.top.right.top.m2.top.e2 -side left
pack $w.top.right.top.m2.buttom.l1 $w.top.right.top.m2.buttom.e1 $w.top.right.top.m2.buttom.l2 $w.top.right.top.m2.buttom.e2 -side left
pack $w.top.right.top.m4.top $w.top.right.top.m4.buttom -side top
pack $w.top.right.top.m4.top.l $w.top.right.top.m4.top.e -side left  -padx 20
pack $w.top.right.top.m4.buttom.l $w.top.right.top.m4.buttom.e -side left  -padx 20



pack $w.top.right.buttom.m1 $w.top.right.buttom.m2 -side left
pack $w.top.right.buttom.m2.m2 $w.top.right.buttom.m2.m3 -side top
pack $w.top.right.buttom.m1.t1  $w.top.right.buttom.m1.t0 $w.top.right.buttom.m1.t2 $w.top.right.buttom.m1.t3 $w.top.right.buttom.m1.t4 -side top



pack $w.top.left.buttom.middle1 $w.top.left.buttom.middle2 $w.top.left.buttom.buttom -side top
pack $w.top.left.buttom.middle1.left $w.top.left.buttom.middle1.middle $w.top.left.buttom.middle1.right -side left
pack $w.top.left.buttom.middle1.left.l1 $w.top.left.buttom.middle1.left.l2 $w.top.left.buttom.middle1.left.l3 $w.top.left.buttom.middle1.left.l4 -side top
pack $w.top.left.buttom.middle1.middle.e1 $w.top.left.buttom.middle1.middle.e2 $w.top.left.buttom.middle1.middle.e3 $w.top.left.buttom.middle1.middle.e4 -side top
pack $w.top.left.buttom.middle1.right.button
pack $w.top.left.buttom.middle2.label
pack $w.top.left.buttom.buttom.entry $w.top.left.buttom.buttom.button $w.top.left.buttom.buttom.label -side top 






#######################add pic###########################
catch {image delete image_main}
image create photo image_main -file [file join $DIRE "img" "application_main.GIF"]
label $w.top.l1 -image image_main -bd 0 -relief sunken
pack $w.top.l1



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

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


#######################initial###########################
set opcserver {VC Server}
set opcgroup group1
$w.top.left.top.middle2.addgroup configure -state "disabled"
$w.top.left.top.middle5.m.b configure -state "disabled"
$w.top.left.top.middle5.r.b configure -state "disabled"
$w.top.left.top.middle4.additem configure -state "disabled" 
$w.top.left.top.middle1.opcdisconnect configure -state "disabled" 
set port 1
set baurd 9600
set nbit 8
set stop 0
set parity 0
set station 1
set function send_ascii_1
set address 100
set length 5
set host 10.13.24.149

set e_host 10.13.24.149
set e_port 502
set e_function 1
set e_address 100
set e_length 5


