# sct_pfeiffer_tpg261.tcl # Created 23/1/13 by Andrew Kerrigan, summer student in the ANSTO sample environment # SICS ScriptContext setup for the pfeiffer 261 pressure gauge # # Usage: # add_tpg261 name IP port [filename] # # Example: # add_tpg261 myObj 137.157.202.78 4004 /home/nbi/pressurelog_060213.txt # # This will set up: # SCT_OBJECT myObj # SCT_CONTROLLER sct_tpg261_myObj # Log file /home/nbi/pressurelog_060213.txt # The SCT_OBJECT has a node "currentPressure" containing the # current pressure reading from the tpg261 as a float, in whatever # units the tpg261 is currently reading in # # The current pressure will be written to the log file along with a time stamp every time it is polled. namespace eval ::scobj::tpg261 { proc sendPR1 {par nextState} { if [hpropexists [sct] geterror] { hdelprop [sct] geterror } sct send "$par" return $nextState } proc isReady {nextState} { set data [sct result] switch -glob -- $data { "ASCERR:*" { sct geterror $data set nextState idle } default { if {$data == "\x06"} { sct send "\x05" } } } return $nextState } proc getPR1 {path nextState} { set data [sct result] switch -glob -- $data { "ASCERR:*" { sct geterror $data set nextState idle } default { set val [string range $data 3 12] set status [string range $data 0 0] sct update $val hsetprop ${path}/currentPressure status $status sct utime readtime set nextState statusUpdate if {[hgetpropval $path/currentPressure logging] = 1} { set floc [hval ${path}/filename] set fn [open $floc a+] set curtime [hgetpropval ${path}/currentPressure readtime] puts $fn "$curtime, $pressure, $status" close $fn } } return $nextState } } proc statusUpdate {path} { set status [hgetpropval $path/currentPressure status] switch $status { 0 { set text "Sensor OK" } 1 { set text "Sensor underrange" } 2 { set text "Sensor overrange" } 3 { set text "Sensor error" } 4 { set text "Sensor off" } 5 { set text "No sensor" } 6 { set text "Identification error" } default { set text "Status unreadable" } } hsetprop $path/currentPressure statusText $text return idle } proc mk_sct_pfeiffer_tpg261 {sct_controller klass tempobj filename} { set ns ::scobj::tpg261 makesicsobj $tempobj SCT_OBJECT sicslist setatt $tempobj klass $klass sicslist setatt $tempobj long_name $tempobj set scobj_hpath /sics/$tempobj hfactory ${scobj_hpath}/currentPressure plain user float hsetprop ${scobj_hpath}/currentPressure read ${ns}::sendPR1 "PR1" isReady hsetprop ${scobj_hpath}/currentPressure isReady ${ns}::isReady getPR1 hsetprop ${scobj_hpath}/currentPressure getPR1 ${ns}::getPR1 $scobj_hpath statusUpdate hsetprop ${scobj_hpath}/currentPressure statusUpdate ${ns}::statusUpdate $scobj_hpath hsetprop ${scobj_hpath}/currentPressure status 0 hsetprop ${scobj_hpath}/currentPressure statusText UNKNOWN hsetprop ${scobj_hpath}/currentPressure readtime UNKNOWN hfactory ${scobj_hpath}/filename plain user text hset ${scobj_hpath}/filename $filename if {$filename = "noFile"} { hsetprop ${scobj_hpath}/currentPressure logging 0 } else { hsetprop ${scobj_hpath}/currentPressure logging 1 set floc $filename set fn [open $floc a+] puts $fn "Time (s), Pressure (mbar), Status" close $fn } ::scobj::hinitprops $tempobj ::scobj::set_required_props $scobj_hpath if {[SplitReply [environment_simulation]]=="false"} { $sct_controller poll ${scobj_hpath}/currentPressure } } proc add_tpg261 {name IP port {filename "noFile"}} { if {[SplitReply [environment_simulation]]=="false"} { makesctcontroller sct_tpg261_$name std ${IP}:$port "\r\n" } mk_sct_pfeiffer_tpg261 sct_tpg261_$name environment $name $filename } } publish ::scobj::tpg261::add_tpg261 user #::scobj::tpg261::add_tpg261 prgauge 137.157.202.78 4002 namespace import ::scobj::tpg261::*