How-To: Clear the UART receive buffer and Timeout if UART read data not received in time.

2,861 views
Skip to first unread message

Hamish Ahern

unread,
Apr 20, 2013, 6:55:53 PM4/20/13
to ioio-...@googlegroups.com
If you want to Clear the UART receive buffer. (which you must do to ensure your receive buffer does not contain junk before sending a uart command that expects a response)

Add this code to IOIOLib\ioio.lib.impl\QueueInputStream.jaja

    @Override
    synchronized public void reset() throws IOException {
        queue_.clear();
    }

and call it from inside your ZZZ class .loop() function.   (ZZZ extends BaseIOIOLooper)
   uartIn_.reset();
   uartOut_.write(0x14);
   uartIn_.wait(2000); //block for up to 2000ms waiting for a response. (not yet tested if this works)
   bytesread = uartIn_.read(readBuffer, 0, 1024);    //class level variable:  private byte[] readBuffer = new byte[1024];
   //todo:  if bytesread != bytesExpected.. wait again. todo: keep a nanoseconds variable so we don't wait for too long.


some ZZZ class .setup()

            uart_ = ioio_.openUart(13, 14, 92500, Parity.NONE, StopBits.ONE);
            uartIn_    = uart_.getInputStream();
            uartOut_ = uart_.getOutputStream();

Hamish Ahern

unread,
Apr 20, 2013, 7:37:42 PM4/20/13
to ioio-...@googlegroups.com
hmm, I can't edit the posts on this forum..  so I'll just have to post updates here:  this is going to get messy.

don't call uartIn_.wait(2000); it will give you an error.

if you want timeouts.. edit this IOIOLib\ioio.lib.impl\QueueInputStream.java function like so:  
i.e 2000ms timeout on the uart.read()

@Override
    synchronized public int read(byte[] b, int off, int len) throws IOException {
        if (len == 0) {
            return 0;
        }
        try {
            //while (state_ == State.OPEN && queue_.isEmpty()) {
            if (state_ == State.OPEN && queue_.isEmpty()) { //dont wait forever.. just 2000ms
                wait(2000);
            }
            if (state_ == State.KILLED) {
                throw new IOException("Stream has been closed");
            }
            if (state_ == State.CLOSED && queue_.isEmpty()) {
                return -1;
            }
            if (len > queue_.size()) {
                len = queue_.size();
            }
            for (int i = 0; i < len; ++i) {
                b[off++] = queue_.remove();
            }
            return len;
        } catch (InterruptedException e) {
            throw new IOException("Interrupted");
        }
    }

Ytai Ben-Tsvi

unread,
Apr 21, 2013, 12:41:59 AM4/21/13
to ioio-...@googlegroups.com
The cleaner way to do timeouts in this context is simply to interrupt the blocked thread when you no longer want to wait. This is what the catch (InterruptedException e) is for.


--
You received this message because you are subscribed to the Google Groups "ioio-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ioio-users+...@googlegroups.com.
To post to this group, send email to ioio-...@googlegroups.com.
Visit this group at http://groups.google.com/group/ioio-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Hamish Ahern

unread,
Apr 21, 2013, 4:57:53 PM4/21/13
to ioio-...@googlegroups.com
do you mean like this?  I wonder what code should I put in the mInterruptThread to make the read unblock.

   InterruptHandler.postDelayed(mInterruptThread, 2000); //2000msec timeout
   returnVal = (byte)uartIn_.read();  //read blocks
   InterruptHandler.removeCallbacks(mInterruptThread);


private InterruptThread mInterruptThread;
mInterruptThread = new InterruptThread();
private class InterruptThread extends Thread {
        public void run() {
            new Thread(new Runnable() {
                public void run() {
                  
                        // what to write here to make the other thread unblock the read  wait?  notifyAll?

                }
            }).start();
        }
    }

Hamish Ahern

unread,
Apr 21, 2013, 4:58:48 PM4/21/13
to ioio-...@googlegroups.com
private Handler InterruptHandler = new Handler();

Ytai Ben-Tsvi

unread,
Apr 21, 2013, 8:31:40 PM4/21/13
to ioio-...@googlegroups.com
Search for TimerTask on this list for some examples.


--
Reply all
Reply to author
Forward
0 new messages