# keithley 2701 Digital multimeter
namespace eval kdvm {} {
}

proc stdConfig::kdvm {} {
  controller std "\n" 5
  prop startcmd "*IDN?"

  obj Keithley2701 rd -none
  prop read kdvm::read
  kids channels {
    node u1 upd
    node u2 upd
    node chan out
    default 0
    prop write kdvm::setchan
    prop enum auto,chan1,chan2
  }
}

proc kdvm::read {} {
  sct send "READ?"
  return kdvm::update 
}

proc kdvm::update {} {
  set mode [hvali [sct]/chan]
  if {$mode == 0} {
    set chan [silent 1 sct channel]
  } else {
    set chan $mode
  }
  hupdate [sct]/u$chan [sct result]
  hdelprop [sct]/u$chan geterror
  if {$mode != 0} {
    return idle
  }
  set chan [expr 3-$chan]
  sct send "ROUT:CLOS (@10${chan}); *IDN?"
  sct channel $chan
  return stdSct::complete
}

proc kdvm::setchan {} {
  set mode [sct target]
  if {$mode == 0} {
    sct update $mode
    return idle
  }
  set chan $mode
  sct send "ROUT:CLOS (@10${chan}); *IDN?"
  sct channel $chan
  return kdvm::updatechan
}

proc kdvm::updatechan {} {
  sct update [sct result]
  return idle
}


