[En-Nut-Discussion] More insight into my uart fread() problem

Kyle Rhodes kyle at ghbraille.com
Fri Nov 21 12:12:40 CET 2003


Hello again,

After more debugging and pulling out my hair, I gained more insight into
the problem, however not the cause.  First of all, I made my code as
verbose as possible with debugging codes, and I discovered something.
Under the condition that two or more packets (60 bytes) are read from
the serial port in a row, the second read fails.  (we knew that)
However, I discovered it fails after the first byte.  I think this
because on subsequent reads, after the connection has been reset, but
not the controller, the magic number (4 bytes at the beginning of the
packet) is missing the first byte.  Therefore, I get a magic number
error after the initial error.  I figured if I reset the input buffer by
calling fread(0, 0, 0, 0) after the connection was established, it would
reset the 1 byte offset and everything would read normal.  I realize
this is not a solution, but it was a test.  However, it didn't work.
The one byte offset was still present.  Naturally, I verified that I'm
not sending too much/little data into the serial port, and this was
verified experimentally.  Every time 60 bytes get sent into the serial
port.  

So my conclusion:  After one read of 60 bytes from the serial port, a
second fails after the first byte.  This in turn leaves extra data in
the input buffer so when the next read comes along there are already 59
bytes in the buffer.  Since the next read occurs after the device
attached to the serial port sends 60 bytes, there are now 119 bytes in
the buffer.  When the read comes along it pulls 60 bytes, however the
magic number is incorrect, so it throws an error.  

The thing I don't understand if my theory is correct, why didn't the
fread(0, 0, 0, 0) reset the read buffer and have everything start over.
In uartavr.c it appears that this is the case based on the code in the
function UartAvrRead():

    /*
     * Call without data pointer discards receive buffer.
     */
    if (buffer == 0) {
        ifs->if_rd_idx = ifs->if_rx_idx;
        return 0;
    }


Maybe I came to the wrong conclusion, but regardless, why is the second
read failing?  Also note, that I'm setting both tx and rx timeouts to 0,
just in case, which should be blocking, no timeout, correct?  


Thanks,

Kyle Rhodes
Hardware / Software Engineer
gh, LLC
3000 Kent Ave
Lafayette, IN 47906
(765) 775-3776  ext 205





More information about the En-Nut-Discussion mailing list