############################################################################################
##     SciSCADA Toolbox
##     Serial.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 wish_dir "Serial"
set text_temp "dfsd"

set w .serial
catch {destroy $w}
toplevel $w
wm title $w "SciSCADA's Interface:Serial"
wm state $w zoom 

############initial#################
set Port 1
set Baudrate 9600
set Nbits 8
set Parity 0
set Stopbits 0
set Protocol 0
set Timeout 1000



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.middle frame###########################

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

#######################middle.middle.left frame###########################

labelframe $w.middle.middle.left -width 250 -height 500 -text "Configure" -bg white
pack $w.middle.middle.left -padx 0 -pady 0 
pack propagate $w.middle.middle.left false

#######################middle.middle.left.top1 frame###########################

frame $w.middle.middle.left.top1 -width 250 -height 50  -bg white
pack $w.middle.middle.left.top1 -padx 0 -pady 0 
pack propagate $w.middle.middle.left.top1 false
label $w.middle.middle.left.top1.label -width 15 -height 50 -text "Port" -bg white
combobox $w.middle.middle.left.top1.cmb -width 15 -height 50 -textvariable cmd1 -bg white
pack $w.middle.middle.left.top1.label $w.middle.middle.left.top1.cmb -side left
$w.middle.middle.left.top1.cmb list insert end "1"
$w.middle.middle.left.top1.cmb list insert end "2"
$w.middle.middle.left.top1.cmb list insert end "3"
$w.middle.middle.left.top1.cmb list insert end "4"

frame $w.middle.middle.left.top2 -width 250 -height 50  -bg white
pack $w.middle.middle.left.top2 -padx 0 -pady 0 
pack propagate $w.middle.middle.left.top2 false
label $w.middle.middle.left.top2.label -width 15 -height 50 -text "Baudrate" -bg white
combobox $w.middle.middle.left.top2.cmb -width 15 -height 50 -textvariable cmd2 -bg white
pack $w.middle.middle.left.top2.label $w.middle.middle.left.top2.cmb -side left
$w.middle.middle.left.top2.cmb list insert end "4800"
$w.middle.middle.left.top2.cmb list insert end "9600"
$w.middle.middle.left.top2.cmb list insert end "19200"

frame $w.middle.middle.left.top3 -width 250 -height 50  -bg white
pack $w.middle.middle.left.top3 -padx 0 -pady 0 
pack propagate $w.middle.middle.left.top3 false
label $w.middle.middle.left.top3.label -width 15 -height 70 -text "Nbits" -bg white
combobox $w.middle.middle.left.top3.cmb -width 15 -height 70 -textvariable cmd3 -bg white
pack $w.middle.middle.left.top3.label $w.middle.middle.left.top3.cmb -side left
$w.middle.middle.left.top3.cmb list insert end "6"
$w.middle.middle.left.top3.cmb list insert end "7"
$w.middle.middle.left.top3.cmb list insert end "8"

frame $w.middle.middle.left.top4 -width 250 -height 50  -bg white
pack $w.middle.middle.left.top4 -padx 0 -pady 0 
pack propagate $w.middle.middle.left.top4 false
label $w.middle.middle.left.top4.label -width 15 -height 70 -text "Parity" -bg white
combobox $w.middle.middle.left.top4.cmb -width 15 -height 70 -textvariable cmd4 -bg white
pack $w.middle.middle.left.top4.label $w.middle.middle.left.top4.cmb -side left
$w.middle.middle.left.top4.cmb list insert end "0"
$w.middle.middle.left.top4.cmb list insert end "1"
$w.middle.middle.left.top4.cmb list insert end "2"

frame $w.middle.middle.left.top5 -width 250 -height 50  -bg white
pack $w.middle.middle.left.top5 -padx 0 -pady 0 
pack propagate $w.middle.middle.left.top5 false
label $w.middle.middle.left.top5.label -width 15 -height 70 -text "Stopbits" -bg white
combobox $w.middle.middle.left.top5.cmb -width 15 -height 70 -textvariable cmd5 -bg white
pack $w.middle.middle.left.top5.label $w.middle.middle.left.top5.cmb -side left
$w.middle.middle.left.top5.cmb list insert end "0"
$w.middle.middle.left.top5.cmb list insert end "1"

frame $w.middle.middle.left.top6 -width 250 -height 50  -bg white
pack $w.middle.middle.left.top6 -padx 0 -pady 0 
pack propagate $w.middle.middle.left.top6 false
label $w.middle.middle.left.top6.label -width 15 -height 70 -text "opem" -bg white

button $w.middle.middle.left.top6.cmd2 -width 15 -height 1 -text "Close" -command {ScilabEval "closecom(getcomhandles())"
$w.middle.middle.left.top6.cmd2 configure -state "disabled"
$w.middle.middle.left.top6.cmd configure -state "normal"
$w.middle.middle.right.cmb configure -state "disabled"
}
button $w.middle.middle.left.top6.cmd -width 15 -height 1 -text "Open" -command {
ScilabEval "handle_serial = opencom('Port',$cmd1,'Baudrate',$cmd2,'Nbits',$cmd3,'Parity',$cmd4,'Stopbits',$cmd5,'Protocol',0,'Timeout',1000)"
$w.middle.middle.left.top6.cmd configure -state "disabled"
$w.middle.middle.left.top6.cmd2 configure -state "normal"
$w.middle.middle.right.cmb configure -state "normal"
}
pack $w.middle.middle.left.top6.cmd $w.middle.middle.left.top6.cmd2 -side left

#######################middle.middle.right frame###########################

labelframe $w.middle.middle.right -width 250 -height 500 -text "data" -bg white
pack $w.middle.middle.right -padx 0 -pady 0 
pack propagate $w.middle.middle.right false

#######################middle.middle.right.label1 frame###########################

label $w.middle.middle.right.label1 -width 15 -height 2 -text "Recieve" -bg white
label $w.middle.middle.right.label2 -width 15 -height 2 -text "Send" -bg white
text $w.middle.middle.right.text1 -width 250 -height 5  -bg white
entry $w.middle.middle.right.entry -textvariable cominput
#checkbutton $w.middle.middle.right.check -text "Auto Send" -width 15 -height 10 -bg white
button $w.middle.middle.right.cmb -width 15 -height 1 -text "Send" -command {
ScilabEval "ok = writecom(handle_serial,'$cominput')"
}

button $w.middle.middle.right.cmb2 -width 15 -height 1 -text "Recieve" -command {
ScilabEval {[inbytes,ok,nbytes] = readbincom(handle_serial,10)}
ScilabEval {TCL_SetVar('text_temp',ascii(inbytes))}
ScilabEval {[inbytes,ok,nbytes] = readbincom(handle_serial,10)}
$w.middle.middle.right.text1 insert end "$text_temp"
ScilabEval {[inbytes,ok,nbytes] = readbincom(handle_serial,10)}
ScilabEval {TCL_SetVar('text_temp',ascii(inbytes))}
ScilabEval {[inbytes,ok,nbytes] = readbincom(handle_serial,10)}
}


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

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

#######################daohang 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 {
	destroy $w
	ScilabEval "TCL_EvalFile(DIR+'opc.tcl')"
	}
button $w.middle.bb.cmd1 -width 16 -height 1 -text "Modbus"  -command {
	destroy $w
	ScilabEval "TCL_EvalFile(DIR+'modbus.tcl')"
	}
button $w.middle.bb.cmd2 -width 16 -height 1 -text "Mysql"  -command {
	destroy $w
	ScilabEval "TCL_EvalFile(DIR+'mysql.tcl')"
	}
button $w.middle.bb.cmd3 -width 16 -height 1 -text "Serial"  -command {
	destroy $w
	ScilabEval "TCL_EvalFile(DIR+'serial.tcl')"
	}
button $w.middle.bb.cmd4 -width 16 -height 1 -text "Ethernet"  -command {
	destroy $w
	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.middle -side top
pack $w.middle.middle.left $w.middle.middle.right -side left
pack $w.middle.middle.left.top1 $w.middle.middle.left.top2 $w.middle.middle.left.top3 $w.middle.middle.left.top4 $w.middle.middle.left.top5 $w.middle.middle.left.top6 -side top
pack $w.statusbar -side bottom

pack $w.middle.middle.right.label1  $w.middle.middle.right.text1 $w.middle.middle.right.cmb2 $w.middle.middle.right.label2 $w.middle.middle.right.entry $w.middle.middle.right.cmb -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" "serial_top.GIF"]
label $w.middle.top.l1 -image imageserial_top -bd 0 -relief sunken
pack $w.middle.top.l1