RF Generator simulator for the sct_rfamp.c protocol handler.

r2917 | ffr | 2010-05-10 15:52:24 +1000 (Mon, 10 May 2010) | 2 lines
This commit is contained in:
Ferdi Franceschini
2010-05-10 15:52:24 +10:00
committed by Douglas Clowes
parent 47fd7c781f
commit d2cf223047

View File

@@ -0,0 +1,192 @@
# $Revision: 1.1.2.1 $
# $Date: 2010-05-10 05:52:24 $
# Author: Ferdi Franceschini (ffr@ansto.gov.au)
# Last revision by: $Author: ffr $
# Run with tclsh SIM_RFGen.tcl
# Creates a socket server which listens for connections and accepts commands
# from clients (eg SICS).
proc serverOpen {channel addr port} {
global voltage
global connected
set connected 1
set voltage 0
fconfigure $channel -translation binary -buffering none
fileevent $channel readable "readLine $channel"
puts "OPENED"
return;
}
set VOLTAGE [format "%02d" 50]
set CURRENT [format "%02d" 0]
set FREQUENCY [format "%03d" 170]
set bSWITCHES [binary format B8 10001111]
set bOPSTATE [binary format B8 10000101]
proc readLine {channel} {
global ADDRESS
global CURRENT
global FREQUENCY
global VOLTAGE
global bSWITCHES
global bOPSTATE
set data [read $channel 1]
binary scan $data c sigStart
puts stdout "RECEIVED: start byte=$sigStart"
if {$sigStart != 2} {
puts stderr "ERROR: start byte should be 2 not $sigStart"
flush stdout
flush stderr
flush $channel
return
}
set ADDRESS [read $channel 1]
puts "RECEIVED: address = $ADDRESS"
set cmdType [read $channel 1]
puts "RECEIVED: command type = $cmdType"
switch $cmdType {
"L" {
set data [read $channel 1]
binary scan $data c sigEnd
if {$sigEnd != 3} {
puts stderr "ERROR: end byte should be 3 not $sigEnd"
flush stdout
flush stderr
flush $channel
return
}
puts stdout "RECEIVED: end byte=$sigEnd"
puts -nonewline $channel [binary format c 0x02]
puts stdout "SENT start byte 2"
puts -nonewline $channel $ADDRESS
puts stdout "SENT address = $ADDRESS"
puts -nonewline $channel "L"
puts stdout "SENT command type = L"
puts -nonewline $channel $CURRENT
puts stdout "SENT current = $CURRENT"
puts -nonewline $channel $FREQUENCY
puts stdout "SENT frequency = $FREQUENCY"
puts -nonewline $channel $VOLTAGE
puts stdout "SENT voltage = $VOLTAGE"
puts -nonewline $channel $bSWITCHES
binary scan $bSWITCHES B8 SW
puts stdout "SENT switches $SW"
puts -nonewline $channel $bOPSTATE
binary scan $bOPSTATE B8 OS
puts stdout "SENT opstate $OS"
puts -nonewline $channel [binary format c 0x03]
puts stdout "SENT end byte 3"
}
"S" {
set CURRENT [read $channel 2]
puts stdout "RECEIVED: current = $CURRENT"
set FREQUENCY [read $channel 3]
puts stdout "RECEIVED: frequency = $FREQUENCY"
set bSWITCHES [read $channel 1]
binary scan $bSWITCHES B8 SW
puts stdout "RECEIVED: switches = $SW"
set data [read $channel 1]
binary scan $data c sigEnd
if {$sigEnd != 3} {
puts stderr "ERROR: end byte should be 3 not $sigEnd"
flush stdout
flush stderr
flush $channel
return
}
puts stdout "RECEIVED: end byte=$sigEnd"
}
default {
puts stderr "ERROR: Unknown command type $cmdType, should be 'L' or 'S'"
}
}
flush stdout
flush stderr
flush $channel
return
}
# Reads a command from a DMC2280 client and then performs an appropriate
# action depending on whether or not the command produces output or has
# been defined.
set lasttime 0
proc xreadLine {who channel} {
global voltage
global didRead
global B
global currtime
global lasttime
set currtime [clock seconds]
if {[expr $currtime - $lasttime] > 30} {
puts stdout "Time for a NAK"
puts -nonewline $channel [binary format c 0x15]
set lasttime [clock seconds]
flush stdout
flush $channel
return
}
set data [read $channel 1]
puts -nonewline stdout "RECEIVED: <$data"
switch $data {
"v" {
set z [read $channel 1]
if { $z == "z" } {
puts stdout "$z>"
puts stdout "SEND: TESTER 1.0z"
puts -nonewline $channel "TESTER 1.0z"
} else {
puts stdout ">"
}
}
"h" {
set v [read $channel 1]
binary scan $v c volts
set z [read $channel 1]
if { $z == "z" } {
puts stdout [format "%d$z>" $volts]
binary scan $v c voltage
puts stdout "SEND: ACK for $voltage"
puts -nonewline $channel [binary format c 0x06]
} else {
puts stdout [format "%d$z>" $volts]
puts stdout "SEND: NAK for bad h command"
puts -nonewline $channel [binary format c 0x15]
}
}
"H" {
set z [read $channel 1]
if { $z == "z" } {
puts stdout "$z>"
set voltage [expr int(rand()*1000 )]
puts stdout [format "SEND: H%dz" $voltage]
puts -nonewline $channel "H[binary format c $voltage]z"
} else {
puts stdout "$z>"
puts stdout "SEND: NAK for bad H command"
puts -nonewline $channel [binary format c 0x15]
}
}
default {
puts stdout ">"
puts stdout "SEND: NAK for bad <$data> command"
puts -nonewline $channel [binary format c 0x15]
}
}
flush stdout
flush $channel
return;
}
# startserver -port 1034
proc startserver {args} {
global tcl_interactive;
array set parr $args;
set connected 0;
set server [socket -server serverOpen $parr(-port)];
after 100 update;
if {$tcl_interactive==0} {vwait forever }
return;
}
startserver -port 65001