[time-nuts] Serial Port Logging Script?
Christian Vogel
vogelchr at vogel.cx
Mon Jan 22 13:38:29 UTC 2007
Hi everyone,
I have been lurking for quite some time, so let me use this
opportunity to say hello to everyone.
Regarding logging serial port-data to files, there is always
trusty old tcl. It should be installed on almost every Solaris,
HPUX, AIX, Linux, Freebsd, ... machine there is.
I used the "extended" version below to read out a Hameg-8123
counter over a period of several weeks, it worked flawlessly.
The "reduced" version will just print out all lines it receives,
prefixed with a human-readable and a unix-since-epoch timestamp.
21:15:35 1166818535 FRA 9.999984604 MHz
21:16:42 1166818602 FRB 9.999998405 MHz
21:17:48 1166818668 RAB 0.999998619
21:18:55 1166818735 FRA 9.999984597 MHz
Greetings from Germany,
Chris
----- snip (reduced version) -----
#!/usr/bin/tclsh8.4
# callback when there is reable data on the serial port
proc getData { chan } {
if { [eof $chan] } {
return
}
# get one line from serial port
set s [gets $chan]
# form timestamp
set t [clock seconds]
set f [clock format $t -format "%H:%M:%S"]
# print timestamp
puts "$f $t $s"
}
set sp [ open "/dev/ttyS0" "r+" ]
fconfigure $sp -blocking 0 -mode "9600,n,8,1" -handshake none -buffering
line
fileevent $sp readable [ list getData $sp ]
set quit 0
while { $quit == 0 } {
vwait quit
}
----- full script ----
#!/usr/bin/tclsh8.4
set currchan ""
proc getData { chan } {
global currchan
if { [eof $chan] } {
return
}
# get one line from serial port
set s [gets $chan]
# puts "chan << $s"
# check if it looks like a number (starts with 0-9)
# if there is no recent measurement, the counter will
# respond with "Not available" or such.
if { ! [string match "\[0-9.\]*" $s ] } {
return
}
# form header
set t [clock seconds]
set f [clock format $t -format "%H:%M:%S"]
# print timestamp, unix seconds, string from counter
if { "" != "$currchan" } {
puts "$f $t $currchan $s"
}
# Advance channel.
switch $currchan {
FRA { set currchan "FRB" }
FRB { set currchan "RAB" }
default { set currchan "FRA" }
}
puts $chan "$currchan"
}
# send "XMT" to counter, which tells the counter to return
# the most recent measurement
proc doTick { chan } {
puts $chan "XMT"
after 1000 [ list doTick $chan ]
}
set sp [ open "/dev/ttyS0" "r+" ]
fconfigure $sp -blocking 0 -mode "9600,n,8,1" -handshake none -buffering
line
fileevent $sp readable [ list getData $sp ]
# input channel A AC, 50 Ohm, 1:10
puts $sp "ACA"
puts $sp "OAL"
puts $sp "LVA0.00"
puts $sp "AA1"
# input channel B
puts $sp "ACB"
puts $sp "OBL"
puts $sp "LVB0.00"
puts $sp "AB1"
# gate time: 65535 ms
puts $sp "SMT65535"
# poll counter every 1000 ms
after 1000 [ list doTick $sp ]
set quit 0
while { $quit == 0 } {
vwait quit
}
More information about the Time-nuts_lists.febo.com
mailing list