####################################################################################################
##     SciSCADA Toolbox
##     Modbus.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 DIRE_main
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:Modbus"
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')" 
	}

	$mapplication add command -label "Data Curve" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/application_curve.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_main 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 150  -bg white -text "Serial Configure"
pack $w.middle.m1 -padx 0 -pady 0 
pack propagate $w.middle.m1 false

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

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

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

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

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

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

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

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

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

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

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

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

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

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

button $w.middle.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.middle.m3.c configure -state "normal"
$w.middle.m1.buttom2.button configure -state "disabled"
$w.middle.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.middle.m1.buttom2.button2 -text "Disconnect" -command { 
ScilabEval "closecom(getcomhandles())"
$w.middle.m3.c configure -state "disabled"
$w.middle.m1.buttom2.button2 configure -state "disabled"
$w.middle.m1.buttom2.button configure -state "normal"
}

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

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

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


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

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

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

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

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

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

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


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

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

button $w.middle.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.middle.m3.e -textvariable modbus_text

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

#######################middle.m4 -frame###########################

labelframe $w.middle.m4 -width 524 -height 150  -bg white -text "Result"
pack $w.middle.m4 -padx 0 -pady 0 
pack propagate $w.middle.m4 false

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

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

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

#######################middle.m4.buttom frame###########################

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

button $w.middle.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.middle.m4.buttom.e -textvariable result_int

#######################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+'tcl/opc.tcl')"
	}
button $w.middle.bb.cmd1 -width 16 -height 1 -text "Modbus"  -command {
	destroy $w
	ScilabEval "TCL_EvalFile(DIR+'tcl/modbus.tcl')"
	}
button $w.middle.bb.cmd2 -width 16 -height 1 -text "Mysql"  -command {
	destroy $w
	ScilabEval "TCL_EvalFile(DIR+'tcl/mysql.tcl')"
	}
button $w.middle.bb.cmd3 -width 16 -height 1 -text "Serial"  -command {
	destroy $w
	ScilabEval "TCL_EvalFile(DIR+'tcl/serial.tcl')"
	}
button $w.middle.bb.cmd4 -width 16 -height 1 -text "Ethernet"  -command {
	destroy $w
	ScilabEval "TCL_EvalFile(DIR+'tcl/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 $w.middle.m4 -side top
pack $w.middle.m1.top  $w.middle.m1.buttom  $w.middle.m1.buttom2 -side top
pack $w.middle.m1.top.l1 $w.middle.m1.top.c1 $w.middle.m1.top.l2 $w.middle.m1.top.c2 -side left
pack $w.middle.m1.buttom.l1  $w.middle.m1.buttom.c1 $w.middle.m1.buttom.l2 $w.middle.m1.buttom.c2 $w.middle.m1.buttom.l3 $w.middle.m1.buttom.c3 -side left
pack $w.middle.m2.top $w.middle.m2.buttom -side top
pack $w.middle.m2.top.l1 $w.middle.m2.top.e1 $w.middle.m2.top.l2 $w.middle.m2.top.e2 -side left
pack $w.middle.m2.buttom.l1 $w.middle.m2.buttom.e1 $w.middle.m2.buttom.l2 $w.middle.m2.buttom.e2 -side left
pack $w.middle.m4.top $w.middle.m4.buttom -side top
pack $w.middle.m4.top.l $w.middle.m4.top.e -side left  -padx 20
pack $w.middle.m4.buttom.l $w.middle.m4.buttom.e -side left  -padx 20


#######################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" "modbus_top.gif"]
label $w.middle.top.l1 -image imageserial_top -bd 0 -relief sunken
pack $w.middle.top.l1


#######################initial###########################

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

$w.middle.m3.c configure -state "disabled"
$w.middle.m1.buttom2.button2 configure -state "disabled"


#######################proc###########################



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

proc showMessageBox1 {w} {
    global msgboxIcon msgboxType
    
    tk_messageBox -message "Opening Com Success" -type ok	-parent $w
}

proc showMessageBox2 {w} {
    global msgboxIcon msgboxType
    
    tk_messageBox -message "Opening Com Failure " -type ok	-parent $w
}


