Updated 2015-03-02 02:28:26 by RLE

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

A more complex serial port monitor program for Windows and Linux written in Tcl/Tk using BWidget can be found at

http://www.rolf-schroedter.de/moni (updated link Jan-30-2006 by RoS)

A full-featured terminal emulator (again, reusing a text widget) can be found in the example directory of the Expect distribution.

http://expect.nist.gov

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}

[Chuck Winters] To use this on the PocketPC modify the size to fit the itty-bitty screen (-width 24 -height 10). I also had to replace the "catch (console hide)" with "wm deiconify ." Works beautifully.

mailto:Rolf.Schroedter@dlr.de (Rolf Schroedter)