Hi, I'm trying to establish a serial link between the two aforementioned micro's and am finding it difficult to successfully send data from the Arduino to the IOIO. Obviously I'm not ruling out that the Arduino is where the problem lies. However in comparison to the Java code that the IOIO requires, the Arduino is much more familiar to me, so this makes me think itโs probably my Java that's letting me down.
What should happen is the IOIO lies dormant until a button on the GUI is pressed, when this happens, an int message of '1' is transmitted to the Arduino ten times (this is part of my error checking, and this bit works fine). The Arduino then reads the one's and transmits 10 back to say that it has received this message correctly.
The problem that I'm having is that when I try to send the int '10' back from the Arduino to confirm that it has received the data correctly I get a load of garbage and I can't work out why that is . . . All i could think was that maybe it's a syncing error? but as both devices are running on the same baud rate I'm not sure.
Also as you can see in order to both read and write simultaneously with the IOIO I am running a separate thread that is always reading until the message sent back is successful, in which case the thread should close (I think this bit works)
Now my questions:
Q1. Have I implemented the Thread correctly, i.e. initiated and closed?
Q2. I am aware of the .available() function, but having read the blurb it doesn't sound very reliable, is this true?
Q3. Are there any obvious errors with my codes layout (I'm confident that there are probably many!)
I hope that I have explained myself enough so that whoever reads this has some idea of what I'm trying to accomplish, any questions feel free to ask!
Here is the code that I am using to read in the Serial UART message:
package ioio.examples.simple;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import ioio.lib.api.DigitalOutput;
import ioio.lib.api.IOIO;
import ioio.lib.api.PwmOutput;
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.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.ToggleButton;
public class IOIOSimpleApp extends IOIOActivity {
private TextView textView_;
private SeekBar seekBar_;
private ToggleButton toggleButton_;
private InputStream in_;
int success = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView_ = (TextView) findViewById(R.id.TextView);
seekBar_ = (SeekBar) findViewById(R.id.SeekBar);
toggleButton_ = (ToggleButton) findViewById(R.id.ToggleButton);
enableUi(false);
}
class Looper extends BaseIOIOLooper {
private PwmOutput pwmOutput_;
private DigitalOutput led_;
private Uart uart_;
private OutputStream out_;
int counter = 0;
int test = 0;
Thread readThread = new readDataThread();// creates the read thread
@Override
public void setup() throws ConnectionLostException {
try {
pwmOutput_ = ioio_.openPwmOutput(12, 100);
led_ = ioio_.openDigitalOutput(IOIO.LED_PIN, true);
uart_ = ioio_.openUart(4, 6, 38400, Uart.Parity.NONE,
Uart.StopBits.ONE);
out_ = uart_.getOutputStream();
in_ = uart_.getInputStream();
readThread.start();// Starts the thread
setText("in setup");
enableUi(true);
} catch (ConnectionLostException e) {
enableUi(false);
throw e;
}
}
@Override
public void loop() throws ConnectionLostException {
try {
if (success == 5) {
readThread.interrupt();// Ends the thread
setText("Successful transmission");
Thread.sleep(2000);
success = 0;
}
pwmOutput_.setPulseWidth(500 + seekBar_.getProgress() * 2);
led_.write(!toggleButton_.isChecked());
if (toggleButton_.isChecked() && counter < 10) {
out_.write(1);
counter = counter + 1;
} else {
}
} catch (IOException e) {
} catch (InterruptedException e) {
ioio_.disconnect();
} catch (ConnectionLostException e) {
enableUi(false);
throw e;
}
}
}
@Override
protected IOIOLooper createIOIOLooper() {
return new Looper();
}
private void enableUi(final boolean enable) {
runOnUiThread(new Runnable() {
@Override
public void run() {
seekBar_.setEnabled(enable);
toggleButton_.setEnabled(enable);
}
});
}
private void setText(final String str) {
runOnUiThread(new Runnable() {
@Override
public void run() {
textView_.setText(str);
}
});
}
public class readDataThread extends Thread {
public void run() {
while (true) {
try {
int i = in_.read();
String aString = Integer.toString(i);
if (i == 10) {
success = 5;
setText("Read in successfully");
Thread.sleep(2000);
} else {
success = 0;
setText(aString);
Thread.sleep(2000);
}
} catch (InterruptedException ex) {
Thread.currentThread().interrupt(); // very important
break;
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("Shutting down thread");
}
}
}