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

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


set DIRE
set wish_dir "Serial"
set text_temp "dfsd"
set send_display "Display What is Send"
set open_result -1
set w .serial
catch {destroy $w}
toplevel $w
wm title $w "SciSCADA's Interface:Ethernet"
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 

#######################left frame###########################

frame $w.left -width 300 -height 704 -bg white
pack $w.left -padx 0 -pady 0
pack propagate $w.left false

#######################middle frame###########################

frame $w.middle -width 524 -height 704  -bg white
pack $w.middle -padx 0 -pady 0 
pack propagate $w.middle false

#######################middle.top frame###########################

frame $w.middle.top -width 524 -height 150  -bg white
pack $w.middle.top -padx 0 -pady 0 
pack propagate $w.middle.top false


#######################middle.m1 frame###########################

labelframe $w.middle.m1 -width 524 -height 300  -bg white -text "Configure"
pack $w.middle.m1 -padx 0 -pady 0 
pack propagate $w.middle.m1 false

#######################middle.m1.t1 frame###########################

frame $w.middle.m1.t1 -width 524 -height 50  -bg white
pack $w.middle.m1.t1 -padx 0 -pady 0 
pack propagate $w.middle.m1.t1 false

label $w.middle.m1.t1.l -text "Host:"
entry $w.middle.m1.t1.e -textvariable host

pack $w.middle.m1.t1.l $w.middle.m1.t1.e -side left


#######################middle.m1.t0 frame###########################

frame $w.middle.m1.t0 -width 524 -height 50  -bg white
pack $w.middle.m1.t0 -padx 0 -pady 0 
pack propagate $w.middle.m1.t0 false

label $w.middle.m1.t0.l -text "Port:"
entry $w.middle.m1.t0.e -textvariable port

pack $w.middle.m1.t0.l $w.middle.m1.t0.e -side left

#######################middle.m1.t2 frame###########################

frame $w.middle.m1.t2 -width 524 -height 50  -bg white
pack $w.middle.m1.t2 -padx 0 -pady 0 
pack propagate $w.middle.m1.t2 false

label $w.middle.m1.t2.l -text "Function:"
entry $w.middle.m1.t2.e -textvariable function

pack $w.middle.m1.t2.l $w.middle.m1.t2.e -side left

#######################middle.m1.t3 frame###########################

frame $w.middle.m1.t3 -width 524 -height 50  -bg white
pack $w.middle.m1.t3 -padx 0 -pady 0 
pack propagate $w.middle.m1.t3 false

label $w.middle.m1.t3.l -text "Address:"
entry $w.middle.m1.t3.e -textvariable address

pack $w.middle.m1.t3.l $w.middle.m1.t3.e -side left

#######################middle.m1.t4 frame###########################

frame $w.middle.m1.t4 -width 524 -height 50  -bg white
pack $w.middle.m1.t4 -padx 0 -pady 0 
pack propagate $w.middle.m1.t4 false

label $w.middle.m1.t4.l -text "Length:"
entry $w.middle.m1.t4.e -textvariable length

pack $w.middle.m1.t4.l $w.middle.m1.t4.e -side left



#######################middle.m2 frame###########################

labelframe $w.middle.m2 -width 524 -height 100  -bg white -text "Construct Code"
pack $w.middle.m2 -padx 0 -pady 0 
pack propagate $w.middle.m2 false



button $w.middle.m2.b -text "Construct Code" -command {
ScilabEval "function=$function" 
ScilabEval "address=$address" 
ScilabEval "length=$length" 
ScilabEval "host='$host'" 
ScilabEval "port=$port" 
ScilabEval {modbus_char=call('modbus_construct',function,1,'i',address,2,'i',length,3,'i','out',[1,12],4,'i')}
ScilabEval "TCL_SetVar('send_code',strcat(string(modbus_char)))" 
ScilabEval {modbus_char=call('modbus_construct',function,1,'i',address,2,'i',length,3,'i','out',[1,12],4,'i')}
ScilabEval "function=$function" 
ScilabEval "address=$address" 
ScilabEval "length=$length" 
ScilabEval "host='$host'" 
ScilabEval "port=$port" 
}

entry $w.middle.m2.c -textvariable send_code 

pack $w.middle.m2.b $w.middle.m2.c -side top

#######################middle.m3 frame###########################

labelframe $w.middle.m3 -width 524 -height 100  -bg white -text "Send Code"
pack $w.middle.m3 -padx 0 -pady 0 
pack propagate $w.middle.m3 false



button $w.middle.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.middle.m3.c -textvariable result

pack $w.middle.m3.b $w.middle.m3.c -side top

#######################right frame###########################

frame $w.right -width 200 -height 704  -bg white
pack $w.right -padx 0 -pady 0 
pack propagate $w.right false



#######################Dao hang frame###########################

frame $w.middle.bb -width 524 -height 10
pack $w.middle.bb -padx 0 -pady 0 
pack propagate $w.middle.bb
button $w.middle.bb.cmd0 -width 16 -height 1 -text "OPC" -command {ScilabEval "TCL_EvalFile(DIR+'opc.tcl')"}
button $w.middle.bb.cmd1 -width 16 -height 1 -text "Modbus"  -command {ScilabEval "TCL_EvalFile(DIR+'modbus.tcl')"}
button $w.middle.bb.cmd2 -width 16 -height 1 -text "Mysql"  -command {ScilabEval "TCL_EvalFile(DIR+'mysql.tcl')"}
button $w.middle.bb.cmd3 -width 16 -height 1 -text "Serial"  -command {ScilabEval "TCL_EvalFile(DIR+'serial.tcl')"}
button $w.middle.bb.cmd4 -width 16 -height 1 -text "Ethernet"  -command {ScilabEval "TCL_EvalFile(DIR+'ethernet.tcl')"}

pack $w.middle.bb.cmd0 $w.middle.bb.cmd1 $w.middle.bb.cmd2 $w.middle.bb.cmd3 $w.middle.bb.cmd4 -side left

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

frame $w.statusbar -width 1020 -height 20 
pack $w.statusbar -padx 3 -pady 3
pack propagate $w.statusbar false
label $w.statusbar.time -text "Time:2006-7-7 18:45:34 | "
label $w.statusbar.mouse -text "  | "
label $w.statusbar.note -text " | "
label $w.statusbar.author -text "Designed by ZhePeng ZheJiang Univ. china"

pack $w.statusbar.time  $w.statusbar.mouse  $w.statusbar.note  $w.statusbar.author -side left

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

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

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

catch {image delete imageserial_right}
image create photo imageserial_right -file [file join $DIRE "img" "right.GIF"]
label $w.right.l1 -image imageserial_right -bd 0 -relief sunken
pack $w.right.l1

catch {image delete imageserial_top}
image create photo imageserial_top -file [file join $DIRE "img" "ethernet.gif"]
label $w.middle.top.l1 -image imageserial_top -bd 0 -relief sunken
pack $w.middle.top.l1


#######################Initial###########################

set port 502
set baurd 9600
set nbit 8
set stop 0
set parity 0
set station 1
set function 1
set address 100
set length 5
set host 10.13.24.149