Posted to tcl by schelte at Tue Sep 14 22:57:53 GMT 2010view raw

  1. proc response {fd} {
  2. fileevent $fd readable [info coroutine]
  3. puts "entering response"
  4. while {![eof $fd]} {
  5. readplc $fd 2
  6. }
  7. }
  8.  
  9. proc readplc {fd len} {
  10. puts "entering readplc fd=$fd len=$len"
  11. set str [read $fd $len]
  12. while {[string length $str] < $len} {
  13. yield
  14. set data [read $fd [expr {$len - [string length $str]}]]
  15. puts [tcl::unsupported::representation $str]
  16. puts [tcl::unsupported::representation $data]
  17. ; Code hangs here with 100% cpu use
  18. append str $data
  19. puts "done"
  20. }
  21. return $str
  22. }
  23.  
  24. proc plcopen {} {
  25. global plcfh
  26. if {[catch {open /dev/ttyS0 r+} plcfh]} {return 0}
  27. fconfigure $plcfh -mode 57600,n,8,1 -translation binary -blocking 0
  28. return 1
  29. }
  30.  
  31. if {[plcopen]} {
  32. coroutine reader response $plcfh
  33. }
  34.  
  35. vwait forever