Embedded Freaks..

September 24, 2007

Logging Serial Port Data

Filed under: embedded-tips — Tags: — kunilkuda @ 7:22 am

Serial port is usually used to log embedded’s report. This is very important to diagnose your problem. Use this script to create traceable log (log with timestamp) :

#! /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 9600
# -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 "\n" { puts "[timeStamp]: [string trim $expect_out(buffer) \r\n]" } } [/sourcecode] This script will produce serial output something like this :

2007-09-21,11:53:19: E0SamplingTimer fired
2007-09-21,11:53:21: E6Logged data :E61F000000FFFFFFFFF9005D00C0FF0000CB4F
2007-09-21,11:53:21: E0Data logged
2007-09-21,11:53:51: E0SamplingTimer fired
2007-09-21,11:53:53: E6Logged data :E63F000000FFFFFFFFFB006300C0FF0000CC4F
2007-09-21,11:53:53: E0Data logged
2007-09-21,11:54:23: E0SamplingTimer fired
2007-09-21,11:54:24: E6Logged data :E65F000000FFFFFFFF09016D00C0FF0000CC4F
2007-09-21,11:54:24: E0Data logged
2007-09-21,11:54:54: E0SamplingTimer fired
2007-09-21,11:54:56: E6Logged data :E67F000000FFFFFFFF0D016B00C0FF0000CC4E
2007-09-21,11:54:56: E0Data logged
2007-09-21,11:55:06: E3Packet received
2007-09-21,11:55:06: E2Download packet received
2007-09-21,11:55:06: E6Dump dataflash from time1 to time2
2007-09-21,11:55:06: E6Read data :E61F000000FFFFFFFFF9005D00C0FF0000CB4F
2007-09-21,11:55:06: E2Dump data :E20B1C7B0F5C040A1F000000FFFFFFFFF9005D00C0FF0000CB4F
2007-09-21,11:55:06: E2Sent data :E2220400000000001F000000FFFFFFFFF9005D00C0FF0000CB4F
2007-09-21,11:55:06: E2Send download protocol reply
2007-09-21,11:55:06: E2Send download protocol done

Check the details of its usage in here.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: