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

proc response {fd} {
    fileevent $fd readable [info coroutine]
    puts "entering response"
    while {![eof $fd]} {
        readplc $fd 2
    }
}

proc readplc {fd len} {
    puts "entering readplc fd=$fd len=$len"
    set str [read $fd $len]
    while {[string length $str] < $len} {
        yield
        set data [read $fd [expr {$len - [string length $str]}]]
        puts [tcl::unsupported::representation $str]
        puts [tcl::unsupported::representation $data]
        ; Code hangs here with 100% cpu use
        append str $data
        puts "done"
    }
    return $str
}

proc plcopen {} {
    global plcfh
    if {[catch {open /dev/ttyS0 r+} plcfh]} {return 0}
    fconfigure $plcfh -mode 57600,n,8,1 -translation binary -blocking 0
    return 1
}

if {[plcopen]} {
    coroutine reader response $plcfh
}

vwait forever