UART and GPS Example

1,268 views
Skip to first unread message

Bob

unread,
Jul 13, 2011, 3:28:21 PM7/13/11
to ioio-users
This is a compilation of code from this user group. You will see the
raw NMEA string in the TextView.

Thanks to Henrique for testing it!

Add your package name and a TextView named "GPSSentance"

import java.io.IOException;
import java.io.InputStream;



import ioio.lib.api.Uart;
import ioio.lib.api.exception.ConnectionLostException;
import ioio.lib.util.AbstractIOIOActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class GPSHelp extends AbstractIOIOActivity {

private Uart uart;
private TextView output;
private InputStream in;



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
output = (TextView)findViewById(R.id.GPSSentence);
}




class IOIOThread extends AbstractIOIOActivity.IOIOThread {

Boolean imBusy = false;

private int bufferSize =1000;
@Override
protected void setup() throws ConnectionLostException {
try{
//38400 is the rate for my gps module
uart = ioio_.openUart(4,5,
38400,Uart.Parity.NONE,Uart.StopBits.ONE);
in = uart.getInputStream();
}
catch(ConnectionLostException e)
{
iWrite(e.getMessage());
iWrite(Log.getStackTraceString(e));
}
}

@Override
protected void loop() throws ConnectionLostException {

if(!imBusy)
{
imBusy = true;
getGPS();
}
}

private void getGPS()
{

try {
int availableBytes =in.available();

if (availableBytes > 0) {
byte[] readBuffer = new byte[bufferSize];
in.read(readBuffer, 0, availableBytes);
char[] Temp = (new String(readBuffer, 0,
availableBytes)).toCharArray();
String Temp2 = new String(Temp);
iWrite("Received:" + Temp2 );" after "String
Temp2 = new String(Temp);

}

sleep(500);
} catch (InterruptedException e) {
iWrite("Error: " +e );
} catch (IOException e) {
// TODO Auto-generated catch block
//
iWrite("Error: " +e );e.printStackTrace();
}
imBusy = false;
}
private void iWrite(String myValue)
{
final String crossValue = myValue;
runOnUiThread(new Runnable() {
@Override
public void run() {
output.setText( output.getText() + crossValue + "\n");
}
});

}



}

@Override
protected AbstractIOIOActivity.IOIOThread createIOIOThread() {
return new IOIOThread();
}
}
Reply all
Reply to author
Forward
0 new messages