namespace eval bronkhorst {} {
}

proc stdConfig::bronkhorst {} {
  controller std "\r\n"

# driver for mass flow meter Bronkhorst MassView 
# serial interface is set to:
# Char Size/Stop Bits:         8/1    Input Speed:                38400
# Flow Ctrl:                  None    Output Speed:               38400
# Parity:                     None    Modem Control:               None


  obj BRONKHORST upd
  kids "flow meter" {
    node counter rd

# 0C=length
# 80=address
# 04=read
# 00 process (only process)
# 80 parameter
# 68 41=process 104,float, fbnr 1
# 80 parameter
# 21 40=process 33, float, fbnr 0
# 00 last parameter
# 01 20=process 1, int, fbnr 0
    prop readcmd ":0C800400806841802140000120"
    prop readfmt ":1080020080%8s80%8s00%4x"
    prop update bronkhorst::update

    node ff upd

    node fi upd
  }
}

proc bronkhorst::update {} {
  if {[scan [sct result] [sct readfmt] cn f1 f2] != 3} {
    error "bad result format: '[sct result]' $cn $f1 $f2"
  }
  set cn [cnvrt xieee2float $cn]
  set f1 [cnvrt xieee2float $f1]
  hupdate /flow/counter $cn
  hupdate /flow/ff $f1
  set t [silent 0 sct read_time]
  if {$t != 0} {
    catch {
      set tim [expr [sct utime] - $t]
      hupdate /flow [expr ($cn - [sct last_counter]) / $tim]
      hupdate /flow/fi $tim
    }
  }
  sct last_counter $cn
  return idle
}
