Problem with inputStream

34 views
Skip to first unread message

mohamed safwen trabelsi

unread,
Jan 30, 2016, 11:29:48 PM1/30/16
to ioio-users
i am trying to read RFID tag  with android using ioio,i am using  pin 34 for digital input  , my RFID reader is EM4100-RS232 reader  After running the code the screen freez   


 please can someone help me
import android.os.Environment;
import ioio.lib.api.DigitalInput;
import ioio.lib.api.DigitalOutput;
import ioio.lib.api.IOIO;
import ioio.lib.api.IOIO.VersionType;
import ioio.lib.api.Uart;
import ioio.lib.api.exception.ConnectionLostException;
import ioio.lib.util.BaseIOIOLooper;
import ioio.lib.util.IOIOLooper;
import ioio.lib.util.android.IOIOActivity;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
* This is the main activity of the HelloIOIO example application.
*
* It displays a toggle button on the screen, which enables control of the
* on-board LED. This example shows a very simple usage of the IOIO, by using
* the {@link IOIOActivity} class. For a more advanced use case, see the
* HelloIOIOPower example.
*/
public class Main extends IOIOActivity {
private ToggleButton toggleButton;
private InputStream in;
private TextView tag;

/**
* Called when the activity is first created. Here we normally initialize
* our GUI.
*/
// public String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/aaTutorial";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
tag=(TextView) findViewById(R.id.lool);
// File dir = new File(path);

}

/**
* This is the thread on which all the IOIO activity happens. It will be run
* every time the application is resumed and aborted when it is paused. The
* method setup() will be called right after a connection with the IOIO has
* been established (which might happen several times!). Then, loop() will
* be called repetitively until the IOIO gets disconnected.
*/
class Looper extends BaseIOIOLooper {
/** The on-board LED. */
private DigitalOutput led_;
private DigitalInput IN_;
private Uart uart;
public void Save(File file, String[] data)
{
FileOutputStream fos = null;
try
{
fos = new FileOutputStream(file);
}
catch (FileNotFoundException e) {e.printStackTrace();}
try
{
try
{
for (int i = 0; i<data.length; i++)
{
fos.write(data[i].getBytes());
if (i < data.length-1)
{
fos.write("\n".getBytes());
}
}
}
catch (IOException e) {e.printStackTrace();}
}
finally
{
try
{
fos.close();
}
catch (IOException e) {e.printStackTrace();}
}
}

@Override
protected void setup() throws ConnectionLostException {
showVersions(ioio_, "IOIO connected!");
led_ = ioio_.openDigitalOutput(0, true);

// IN_ = ioio_.openDigitalInput(34, DigitalInput.Spec.Mode.PULL_UP);
//toast(IN_.toString());
try {
uart = ioio_.openUart(34, 1, 115200, Uart.Parity.NONE,
Uart.StopBits.ONE);
in = uart.getInputStream();
}catch (Exception e){
toast(e.getMessage()+"Execs");
}
enableUi(true);

}
int i;
char c;
String ch="v";
/**
* Called repetitively while the IOIO is connected.
*
* @throws ConnectionLostException
* When IOIO connection is lost.
* @throws InterruptedException
* When the IOIO thread has been interrupted.
*
* @see ioio.lib.util.IOIOLooper#loop()
*/
@Override
public void loop() throws ConnectionLostException, InterruptedException {
led_.write(!toggleButton.isChecked());
// File file = new File(path + "/savedFile.txt");
try {

while((i=in.read())!=-1)
{
// converts integer to character

c=(char)in.available();
ch+=c;
toast(c+"");

//String [] saveText = ch.split(System.getProperty("line.separator"));

// toast(ch);


//Save (file, saveText);


}


tag.setText(ch.toString());

// String [] saveText = ch.split(System.getProperty("line.separator"));


// Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_LONG).show();

// Save (file, saveText);
// toast(ch + "tag");

} catch (IOException e) {
toast(e.getMessage());
}
Thread.sleep(100);
}

/**
* Called when the IOIO is disconnected.
*
* @see ioio.lib.util.IOIOLooper#disconnected()
*/
@Override
public void disconnected() {
enableUi(false);
toast("IOIO disconnected");
}

/**
* Called when the IOIO is connected, but has an incompatible firmware version.
*
* @see ioio.lib.util.IOIOLooper#incompatible(IOIO)
*/
@Override
public void incompatible() {
showVersions(ioio_, "Incompatible firmware version!");
}
}

/**
* A method to create our IOIO thread.
*
* @see ioio.lib.util.AbstractIOIOActivity#createIOIOThread()
*/
@Override
protected IOIOLooper createIOIOLooper() {
return new Looper();
}

private void showVersions(IOIO ioio, String title) {
toast(String.format("%s\n" +
"IOIOLib: %s\n" +
"Application firmware: %s\n" +
"Bootloader firmware: %s\n" +
"Hardware: %s",
title,
ioio.getImplVersion(VersionType.IOIOLIB_VER),
ioio.getImplVersion(VersionType.APP_FIRMWARE_VER),
ioio.getImplVersion(VersionType.BOOTLOADER_VER),
ioio.getImplVersion(VersionType.HARDWARE_VER)));
}

private void toast(final String message) {
final Context context = this;
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
});
}

private int numConnected_ = 0;

private void enableUi(final boolean enable) {
// This is slightly trickier than expected to support a multi-IOIO use-case.
runOnUiThread(new Runnable() {
@Override
public void run() {
if (enable) {
if (numConnected_++ == 0) {
toggleButton.setEnabled(true);
}
} else {
if (--numConnected_ == 0) {
toggleButton.setEnabled(false);
}
}
}
});
}
}


Ytai Ben-Tsvi

unread,
Jan 30, 2016, 11:59:43 PM1/30/16
to ioio-...@googlegroups.com
Not sure what you're trying to achieve, but let's read your loop() code together to make it obvious why it does nothing and gets stuck:
  
while((i=in.read())!=-1) {
  // Wait until a byte is available to read, or the input stream is closed. If the former, discard what you just read and go into the block. Otherwise, exit the loop.
  c=(char)in.available();
  // Read how many bytes are available to be read without blocking. This would typically be 0 or a small value, since you've just read a byte if it was available. Interpret this value to a char type (not very meaningful!).
  ch+=c;
  // Append this probably unreadable character to a string.
  toast(c+"");
  // Toast it.
}

So essentially, your loop() method will only exit when the stream has been closed (typically, when your app exits or the IOIO disconnects). It is typically not intended to be used like that.
It will also display a toast every time a character is received over the UART, but this toast would include a pretty bogus string, which I'm not sure how Android would treat.

--
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 https://groups.google.com/group/ioio-users.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages