Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 21566059e28212699d1ec65ec9d2b5025ff7fadf
Ticket: 2953593fffffffffffffffffffffffffffffffff
[gets $fd data] returns 0 in nonblocking mode
User & Date: egavilan on 2010-02-17 15:12:52
Changes

  1. assignee changed to: "andreas_kupries"
  2. cmimetype changed to: "text/plain"
  3. comment changed to:
    Reading a serial port in nonblocking mode sometimes returns 0 for the number of bytes read.
    
    The [gets] documentation states "If varName is specified and an empty string is returned in varName because of end-of-file or because of insufficient data in non-blocking mode, then the return count is -1".
    
    In this case I expect either -1 or 10 (the length of the incoming message from the device, which never sends empty lines).
    
    The next script shows the issue:
    
    ===========================================================================
    proc readserial {fd} {
        global short empty normal
    
        set chars [chan gets $fd data]
        if {$chars == -1} {
            if {[chan eof $fd]} {
                chan close $fd
                done
            }
            incr short
            return
        }
        # this shouldn't happen, but it does anyway
        if {$chars == 0} {
            incr empty
            return
        }
        incr normal
        if {$normal == 10000} {
            done
        }
    }
    
    proc done {} {
        global short empty
        chan puts "Empty reads: $empty\nShort reads: $short"
        exit
    }
    
    set empty  0
    set short  0
    set normal 0
    
    set fd [open /dev/ttyUSB0 r]
    chan configure $fd \
        -mode 9600,n,8,1 \
        -buffering line \
        -blocking 0
    chan event $fd readable [list readserial $fd]
    ===========================================================================
    
    Notes:
    In blocking mode there's no "empty" reads.
    This happen in both windows and linux.
    The serial port is a usb<->serial adapter.
    The whole script has a Tk gui, showing the value read in a label.
    
  4. foundin changed to: "None"
  5. is_private changed to: "0"
  6. priority changed to: "5 Medium"
  7. resolution changed to: "None"
  8. status changed to: "Open"
  9. submitter changed to: "egavilan"
  10. subsystem changed to: "24. Channel Commands"
  11. title changed to: "[gets $fd data] returns 0 in nonblocking mode"
  12. type changed to: "Bug"