Method to trigger an event when no data received through UART.

106 views
Skip to first unread message

passion4ioio

unread,
May 20, 2013, 2:45:00 AM5/20/13
to ioio-...@googlegroups.com
Hi,

I have been trying to trigger an event(make an IOIO pin high and low) on occurrence of condition of no data being received (RX) through UART.

on using the method

  instance1 = rd.readLine();

the method ,readline() gets blocked when no data is received through RX of UART and remains blocked until the next byte is received.
Also no newline character is passed on by the other side and hence not received on ioio side, whenever data transmission is stopped.
I used the .available() function also, however there are instances of empty queue even when data is being received, therefore cannot use this function as well.
Therefore it is difficult to detect the condition of data not being received.

Does any method which can detect the condition of " data not being received through Rx for "x" seconds" exist so as to trigger an event on occurrence of such condition.

Ytai Ben-Tsvi

unread,
May 20, 2013, 2:56:13 AM5/20/13
to ioio-...@googlegroups.com
You can set a TimerTask just before blocking on read(), x seconds into the future, which will interrupt() your thread. It will cause read() to throw an IOException. If read succeeds, you can cancel that task.



--
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.
 
 

LimitlessLED LED

unread,
May 22, 2013, 2:23:53 AM5/22/13
to ioio-...@googlegroups.com
that is too much overhead creating a new timertask all the time, just mdify the queueinput java   and where you see wait() in the read()   change it to wait(2000)  this will ensure it times out when no data comes along, to timeout after 2 seconds.

Ranu Gupta

unread,
May 22, 2013, 7:36:54 AM5/22/13
to ioio-...@googlegroups.com
I changed wait() to wait(2000), but this didn't work for me.
I expected that after 2 seconds on "no data received condition" the function comes out of blocked readline() function, but it didn't.
I even tried the timer task, it didn't work for me.I cannot make out what's going wrong.. Please help..


Code:



class Looper extends BaseIOIOLooper {
Uart uart,uart1;
InputStream in, in1;
OutputStream out, out1;
//String instance1=null, instance2=null;
BufferedReader rd,rd2;
MyThread thread_;
Timer updateTimer = new Timer("gForceUpdate");
public void setup() throws ConnectionLostException {
uart=ioio_.openUart(6,5, n, Uart.Parity.NONE, Uart.StopBits.ONE);
uart1=ioio_.openUart(r, tk, n, Uart.Parity.NONE, Uart.StopBits.ONE);
in=uart.getInputStream();
out=uart.getOutputStream();
in1=uart1.getInputStream();
out1=uart1.getOutputStream();
but.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
byte [] wrBuff =ed1.getText().toString().getBytes();
   out.write(wrBuff);
   Thread.sleep(200);
   byte [] rdBuff=new byte[in.available()];
   in.read(rdBuff);
   ed2.setText(new String(rdBuff));
   byte [] wrBuff1 =ed3.getText().toString().getBytes();
   out1.write(wrBuff1);
   Thread.sleep(200);
   byte [] rdBuff1=new byte[in1.available()];
   in1.read(rdBuff1);
   ed4.setText(new String(rdBuff1));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
rd=new BufferedReader(new InputStreamReader(in));

rd2=new BufferedReader(new InputStreamReader(in1));
       
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "connected", Toast.LENGTH_SHORT).show();
}
});
}
public void loop() throws ConnectionLostException,InterruptedException {
String instance1=null, instance2=null;
try { 
iWrite("loop started");
//here i need to detect the condition of data not being received and trigger an event.
                                  updateTimer.scheduleAtFixedRate(new TimerTask(){
        public void run(){
        iWrite("refreshed");
       
        }
      }, 100, 5000);
instance1 = rd.readLine();
} catch(IOException ioe) {
iWrite("timeout");
ioe.printStackTrace();
}
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
@Override
protected IOIOLooper createIOIOLooper() {
return new Looper();
}
private void iWrite(String myValue)
    {
        final String crossValue = myValue;
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
            ed2.setText(ed2.getText() + crossValue + "\n");
            }
        });

    }
private void iWrite2(String myValue)
    {
        final String crossValue = myValue;
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
            ed4.setText(ed4.getText() + crossValue + "\n");
            }
        });
   }  

Ytai Ben-Tsvi

unread,
May 22, 2013, 8:43:03 PM5/22/13
to ioio-...@googlegroups.com
The way you're asking the question is not very effective.
Please narrow down your program to the bare minimum that exhibits the problem. Then describe exactly what you're expecting to see and what you're seeing instead.

In general, your usage of the TimerTask is weird. The intention is that you schedule (once, not fixed-rate!) a timer task before every read() that you want to abort, and then have this TimerTask interrupt() your original thread (the IOIO thread).

There are examples for this on the forum. Search for them.

Ranu Gupta

unread,
May 23, 2013, 3:02:17 AM5/23/13
to ioio-...@googlegroups.com
Thanx ytai for ur reply,

Here is the minimised code in which I need the help.

public void loop() throws ConnectionLostException,InterruptedException {
try {
//here I need to detect the condition of data not being received and trigger an event.
    
                instance1 = rd.readLine();
          //display instance one on ui
} catch(IOException ioe) {
ioe.printStackTrace();
}
}
}

As in above code what I need to do is when data is not receiving I just want to print the statement(trigger an event to reset the RXpin )
I also use the timer task code stated below but I don't know why it not work for me.
Please help me If you can.

public void loop()throws ConnectionLostException, InterruptedException {

final Thread current= Thread.currentThread();
try {
TimerTask t = new TimerTask() {
 public void run() {
   current.interrupt();
 }
};
timer.schedule(t, 2000);
try {
instance1 = rd.readLine();  
                                                t.cancel();
                                             }
                                             } catch (InterruptedException e) {
iWrite("interrupted");
t.cancel();
}
                                         

Ytai Ben-Tsvi

unread,
May 23, 2013, 3:08:12 AM5/23/13
to ioio-...@googlegroups.com

Try without the reader, just read directly from the InputStream. Let me know what's the observed behavior. Or better: use a debugger and see where it hangs.

Reply all
Reply to author
Forward
0 new messages