Posted to tcl by jbeale123 at Wed Jul 20 18:18:50 GMT 2011view raw

  1. # For COMn with n>9, you must use the Win32 device name e.g. {\\.\COM14}
  2. ############################################
  3. # Example (1): Poll the comport periodically
  4. # set serial [open \\.\COM14 r+]
  5.  
  6. proc serdisp { } {
  7. set serial [open "COM9:" r+]
  8. # fconfigure $serial -mode "115200,n,8,1"
  9. fconfigure $serial -mode "9600,n,8,1"
  10. fconfigure $serial -blocking 1 -timeout 100 -buffersize 50
  11.  
  12. for {set i 0} {$i < 20} {incr i} {
  13. set data [read $serial] ;# read ALL incoming bytes
  14. set size [string length $data] ;# number of received byte, may be 0
  15. if { $size } {
  16. puts "received $size bytes: $data"
  17. } else {
  18. puts "<no data>"
  19. update ;# Display output, allow to close wish-window
  20. }
  21. }
  22. }
  23.  
  24. # display data coming in on serial port
  25. serdisp
  26.