Version 4 of A simple serial terminal

Updated 2003-10-31 04:09:22

How to build a simple serial port monitor in a text widget. -- RoS

A more complex serial port monitor program for Windows and Linux written in Tcl/Tk using BWidgets can be found at http://home.t-online.de/home/rolf.schroedter/moni/moni.html


You may also look at More Serial Port Samples .


 ###############################################################################
 # Term for a simple terminal interface
 ###############################################################################
 # The terminal bindings are implemented by defining a new bindtag 'Term'
 ###############################################################################

 # Configure your serial port here
 #
 set Term(Port) com1
 set Term(Mode) "19200,n,8,1"
 set Term(Font) Courier

 # Global variables
 #
 set Term(Text) {}

 ##################### Terminal In/Out events ############################
 proc term_out { chan key } {
     switch -regexp -- $key {
         [\x07-\x08] -
         \x0D        -
         [\x20-\x7E] { puts -nonewline $chan $key; return -code break }
         [\x01-\x06] -
         [\x09-\x0C] -
         [\x0E-\x1F] -
         \x7F        { return }
         default     { return }
     } ;# switch
 }

 proc term_in { ch } {
     upvar #0 Term(Text) txt

     switch -regexp -- $ch {
         \x07    { bell }
         \x0A    { # ignore }
         \x0D    { $txt insert end "\n" }
         default { $txt insert end $ch }
     }
     $txt see end
 }

 proc receiver {chan} {
     foreach ch [ split [read $chan] {}] {
         term_in $ch
     }
 }

 ##################### Windows ############################
 proc scrolled_text { f args } {
     frame $f
     eval {text $f.text \
         -xscrollcommand [list $f.xscroll set] \
         -yscrollcommand [list $f.yscroll set]} $args
     scrollbar $f.xscroll -orient horizontal \
         -command [list $f.text xview]
     scrollbar $f.yscroll -orient vertical \
         -command [list $f.text yview]
     grid $f.text $f.yscroll -sticky news
     grid $f.xscroll -sticky news
     grid rowconfigure $f 0 -weight 1
     grid columnconfigure $f 0 -weight 1
     return $f.text
 }

 ##### main #######


 set chan [open $Term(Port) r+]
 fconfigure $chan -mode $Term(Mode) -translation binary -buffering none -blocking 0
 fileevent $chan readable [list receiver $chan]

 set Term(Text) [scrolled_text .t -width 80 -height 25 -font $Term(Font) ]
 pack .t -side top -fill both -expand true

 bind $Term(Text) <Any-Key> [list term_out $chan %A]

 catch {console hide}

mailto:[email protected] (Rolf Schroedter)


The link above is now: http://www.rolf-schroedter.de/moni/moni.html Oct-30-2003, JM