Skip to content

Instantly share code, notes, and snippets.

@ubinix-warun
Created March 23, 2014 12:33
Show Gist options
  • Save ubinix-warun/9722490 to your computer and use it in GitHub Desktop.
Save ubinix-warun/9722490 to your computer and use it in GitHub Desktop.
Tcl/Expect Script for Receive and Send over Serial-Port ttyX http://embeddedfreak.wordpress.com/2007/09/24/logging-serial-port-data/
#! /bin/sh
# the next line restarts using expect \
exec expect $0 ${1+"$@"}
#!/usr/local/bin/expect -f
#
# serialWatch.exp
# Connect up to a device on a serial port
# Time stamp any incoming lines to stdout
# port is any serial port (omit the /dev/ prefix)
# e.g. ttya, cua0, serialA, serialB, boca01 - boca16
if {$argc != 1} {
puts "$argc, $argv"
puts "usage: serial.exp port"
exit
}
proc timeStamp {} {
global tcl_version
if {$tcl_version >= 7.5} {
# "clock" command requires Tcl v7.5 or greater
# internal routine a little faster than making a system call
set stamp [clock format [clock seconds] -format %Y-%m-%d,%T]
} else {
# fall back to standard UNIX system call
set stamp [exec /bin/date +%Y-%m-%d,%T]
}
return $stamp
}
puts "\n[timeStamp] Log starting..."
set port /dev/$argv
set spawned [spawn -open [open $port w+]]
puts "[timeStamp]: [string trim $spawned \r\n]"
set baud 57600
# -parenb means don't use a parity bit
# -cstopb means "not 2 stop bits, but 1"
# cs8 means 8 bits
# -echo means no echo (full duplex?)
stty ispeed $baud ospeed $baud raw -echo cs8 -parenb -cstopb onlcr < $port
log_user 0
# log each input line
# add a timeStamp at the beginning of each line
while {1} {
# expect "FIXSTR" {
# puts "[timeStamp]: [string trim $expect_out(buffer) \r\n]"
# }
# expect -re {H_STR.*E_STR[0-9]{2}} {
# puts "[timeStamp]: [string trim $expect_out(buffer) \r\n]"
# }
expect -re {FIXSTR[0-9]{2}} {
send "FIXRES"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment