#!/usr/local/bin/wish
#
# probem1.tcl / probe check utility, does do a lot more
# than tell you if it's connected
#
# Reed Wade / rwade@spiderplant.com
# 5 Feb 98
#
#



wm title . "HLTWare QwikCheck v1/5Feb98"


frame .m 
pack .m -fill both -expand 1

label .m.l1 -text "Device Name:"
entry .m.dev
label .m.l2 -text "Address:"
entry .m.addr
button .m.test -text "Test for Probe at This Address" -command test
message .m.msg -aspect 200 -justify center -text \
   "HLTWare Probe QwikCheck v1/5Feb98"
button .m.exit -text "Quit" -command exit
button .m.help -text "Help" -command "showHelp MAIN"

if {$tcl_platform(platform) == "windows"} {
  .m.dev insert 0 "com1:"
  .m.l1 configure -text "Comm Port:"
  set deviceMode "RDWR"
} else {
  set deviceMode "RDWR NONBLOCK"
  set deviceMode "RDWR"
  .m.dev insert 0 "/dev/tty01"
}
.m.addr insert 0 "32"

grid .m.l1 -row 0 -stick nes
grid .m.dev -row 0 -column 1 -stick news
grid .m.l2 -row 1 -stick nes
grid .m.addr -row 1 -column 1 -stick news
grid .m.test -row 2 -columnspan 2 -stick news
grid .m.msg -row 3 -columnspan 2 -stick news
grid .m.exit -row 4 -stick news
grid .m.help -row 4 -column 1 -stick news

proc test {} {
  set device [.m.dev get]
  set addr [.m.addr get]
  global deviceMode
  global portFP
  catch "close $portFP"

  # first try to open the device
  if {[catch "set portFP [open $device $deviceMode]" ERR]} then {
    tk_messageBox -icon error -type ok \
      -title "Couldn't Open: $device" \
      -message "Couldn't Open: $device ($ERR)"
    return
  }
  fconfigure $portFP -mode 1200,n,8,1 -buffering none 
  .m.msg configure -text "\nSending request...\n"
  update ; update idletasks 

  # now send the request
  
  read $portFP
  puts -nonewline $portFP [binary format c [expr $addr]]
  after 1000 

  set A ""
  while {[string length [set a [read $portFP]]]} {
    append A $a
    after 200
  }
  
  if {$A == ""} then {
    .m.msg configure -text "No Probe Responded"
  } else {
    .m.msg configure -text "Probe Response: $A"
  }

  catch "close $portFP"
}

proc showHelp {text} {
  set h ".help"
  catch "destroy $h"

  toplevel $h
  wm title $h "Help"

  frame $h.f1
  button $h.ok -text Ok -command "destroy $h"

  pack $h.f1 -side top -fill both -expand 1 
  pack $h.ok -side top -fill y

  text $h.f1.text -relief sunken -bd 2  \
    -yscrollcommand "$h.f1.scroll set" -setgrid 1 \
    -height 25 -width 60 -wrap word
  scrollbar $h.f1.scroll -command "$h.f1.text yview"
  pack $h.f1.scroll -side right -fill y
  pack $h.f1.text -expand yes -fill both
  global HELP_$text
  $h.f1.text insert 0.0 [set HELP_$text]
}


set HELP_MAIN {
   HLTWare Probe QwikCheck v1/5Feb98

   This utility should be used to check whether a probe can 
   be read. It is not intended to be more useful than that.

   Enter the device name or comm port to which your Hot Little 
   Therm is connected.

   Then enter the address of the probe you want to read. This 
   can be a decimal (32) or hex (0x20) value.

   For a version 9 Hot Little Therm, the first probe will use 
   address 0x20, the second probe will use 0x30, etc.

   The probe should respond with the temperature in degrees C. 
   A response of '######' means there is a device serving that 
   address range but no probe for that address is connected.
   A response of '******' means the device had a problem reading
   that probe, this normally indicates a cabling problem or a
   faulty probe chip.

   A response like 'V93-7221 S1 T1000002343459823....' means
   you've hit the dump mode address (0x10 for the first device).

   This version has been tested under Windows95. 

   Reed Wade, Spiderplant
   http://www.spiderplant.com/
   therm@spiderplant.com
}
