How to send and receive data with UART module

562 views
Skip to first unread message

YA

unread,
Sep 7, 2011, 8:06:02 AM9/7/11
to ioio-users
I am trying to do some basic testing with UART to send text from
android Activity and receive the text back and
display it on the textview , so i can be sure i am able to send data
and receive as well , and then i can move
forward , but when i try to do it displays the random bytes not the
text i am trying to send, so can you check this
what i am doing wrong, i am trying this code.



package ioio.examples.hello;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import ioio.examples.hello.R;
import ioio.lib.api.DigitalOutput;
import ioio.lib.api.Uart;
import ioio.lib.api.Uart.Parity;
import ioio.lib.api.Uart.StopBits;
import ioio.lib.api.exception.ConnectionLostException;
import ioio.lib.util.AbstractIOIOActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;


public class MainActivity extends AbstractIOIOActivity {
private ToggleButton button_;
public TextView textview;
private EditText enterText;
private static int counter = 0;



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textview = (TextView)findViewById(R.id.title);
button_ = (ToggleButton) findViewById(R.id.button);
enterText = (EditText) findViewById(R.id.editText1);

}


class IOIOThread extends AbstractIOIOActivity.IOIOThread {
/** The on-board LED. */
private DigitalOutput led_;
private Uart uart;
private InputStream in;
private OutputStream out;
private byte [] buffer = new byte[128];
String b = "test";
int availableBytes;



@Override
protected void setup() throws ConnectionLostException {
led_ = ioio_.openDigitalOutput(0, true);

runOnUiThread(new Runnable(){
@Override
public void run(){
textview.setText("hi");
}
});



uart = ioio_.openUart(3, 10, 9600, Parity.NONE, StopBits.ONE);
in = uart.getInputStream();
out = uart.getOutputStream();


// button_.setText("hi");
// textview.setText("hi");


}

@Override
protected void loop() throws ConnectionLostException {
led_.write(!button_.isChecked());

try {
// textview.setText("hello" +
Integer.toString(counter));
try {
out.write(b.getBytes());
availableBytes =in.read(buffer);
runOnUiThread(new Runnable() {
@Override
public void run() {
textview.setText("available bytes " + availableBytes);
}
});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


sleep(1000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}
}

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

Lucas Mourão Lopes

unread,
Sep 4, 2015, 12:41:54 AM9/4/15
to ioio-users, aliya...@gmail.com
Try this:

 private void turnOffLed()
    {
        if (btSocket!=null)
        {
            try
            {
              btSocket.getOutputStream().write("a\0".toString().getBytes());
            }
            catch (IOException e)
            {
                msg("Error");
            }
        }
    }


Reply all
Reply to author
Forward
0 new messages