Version 3 of How to read the seral port, and display in hexadecimal

Updated 2008-10-23 09:28:07 by dkf

Here is the code, I'm using to read and display the data stream coming via the serial interface. I'm displaying the content in 0x?? notation to ease readability. The TCL script works fine and should help debugging. I use it for debugging purposes, ie my microcontroller has few memory, therefore I can make hex-dumps in this way. ds


 ### Constants
 set baudrate   38400
 set parity     n
 set databits   8
 set stopbits   1

 ### G U I
 text .t1 -width 85 -font {courier 8 } -tabs {3c 6c 9c 13c left}\
      -cursor gobbler
 pack .t1 -side top -anchor nw
 button .b1 -text Exit -command exit
 pack .b1 -side bottom -anchor nw -fill x -expand true
 wm title . "Com1 Reader - HexCharacters "

 ### M A I N
 .t1 tag configure underline -underline true
 .t1 tag configure bold -font {courier 10 bold}
 .t1 tag configure finsih -font {courier 6}
 .t1 insert end "\tRead COM1: $baudrate, Parity=$parity,\
     Databits=$databits, Stopbits=$stopbits\n\n" {underline bold}

 ### Procedures

 proc rd_chid {chid} {
   set msg [read $chid 3]
   set listOfLetters [split $msg {} ]
   set serialIPinHex ""

   foreach iChar $listOfLetters {
      scan $iChar "%c" decimalValue
      set hexValue [format "%02X" $decimalValue ]
      set hexValue "0x$hexValue"
      set serialIPinHex "$serialIPinHex $hexValue"
   }
   .t1 insert @0,0 "hallo:\"$serialIPinHex\"\n"   
 }

 proc open_com {} {
   global com
   global baudrate
   global parity
   global databits
   global stopbits

   set com [open com1: r+]
   fconfigure $com -mode $baudrate,$parity,$databits,$stopbits\
      -blocking 0 -translation auto -buffering none -buffersize 12

   fileevent $com readable [list rd_chid $com]
 }

 open_com

 # the channel shouldn't be closed before exiting

 #close $com