I'm trying to have an Arduino UNO send values to a IOIO board (https://github.com/ytai/ioio/wiki/UART) over UART. As someone turns a rotary encoder, I want it to send a 0 for CW, 1 for CCW, and 2 for a press. Everything checks out in the Serial Monitor from the Arduino, but I don't know how to read the values and parse them on the Java-end correctly. It all comes through as seemingly random numbers, sometimes occasionally the correct number is there.
I've tried both of these methods on the Arduino side:
Serial.write(1);
byte data[] = {1};
Serial.write(data, 1);
Also Serial.write automatically writes to pin 1, so theres no need to create a SoftwareSerial object.
When reading this on the Java side, I just get mostly 255, occasionally the correct number, and occasionally a random number in between 0 and 255:
@Override
public void connect() throws ConnectionLostException {
try{
// rx pin = 6
mUart = ioio_.openUart(RX_PIN, IOIO.INVALID_PIN, 9600, Parity.NONE, StopBits.ONE);
mInput = mUart.getInputStream();
}
catch(ConnectionLostException e){
Log.e(TAG, "connection lost:" + e.getMessage());
ioio_.disconnect();
throw e;
}
}
@Override
public void loop(int loopCount) throws ConnectionLostException {
try{
byte[] response = new byte[1];
int read = mInput.read();
}catch(IOException e){
Log.d(TAG, "error: " + e.getMessage());
}
}
I've also tried using BufferedReaders, while passing Strings through Serial.println, but a lot of crazy characters were getting output from the Java side (tried encoding in both UTF-8 and ASCII).
Baud rates are matched up at 9600, and I'm on a 5v RX pin on the IOIO, and that pin is connected to the TX pin (pin 1) on the Arduino Uno.
Does anyone point to a simple way of transmitting & receiving an integer?