Reading COM & stdin

I'm trying to read COM & then stdin:

  1. Wait & get data from COM
  2. Ask for tag
  3. stdout tag & msg from com port.
  4. start over

Working example:

  set serial [open com1: r+];
  fconfigure $serial -mode "9600,n,8,1";
  fconfigure $serial -blocking 0 -buffering line;

  set run_it 1;

  while {$run_it == 1} {
    set data [gets $serial];
    set size [string length $data];

    if {$size} {
      set re {[^a-z0-9]*}
      regsub -nocase -- $re $data {} msg;

      # get first 18 characters
      set id [string range $msg 0 18];

      puts stdout "ID: $id";
      puts -nonewline stdout "Tag: "
      flush stdout; # stdout default buffering mode is LINE

      set tag [gets stdin];

      puts "tag=$tag id=$id"
    } 
  }

escargo 7 Feb 2006 - You don't need to compare run_it to 1; you could just use it like you use size. You could set re outside of the loop, since it is invariant.

Is the program behaving the way you want? (I might add a check for tag being empty in its own loop, or else looking for a special value for tag and then setting run_it to zero.)