Trying to read data from Revduino, heres the code,
First the Arduino code, it sets up listening on address 0x51, prints anything it receives, then sends back 11 bytes ‘abcdefghijk’.
#include <Wire.h>
void setup()
{
Wire.begin(0x51); // join i2c bus with address #4
Wire.onReceive(receiveEvent); // register event
Serial.begin(115200);
Serial.print(“Hello”);
}
void loop()
{
delay(100);
}
void receiveEvent(int howMany)
{
Serial.print(“GotString!”);
String rxString = “”;
while ( Wire.available() > 0 )
{
char n=(char)Wire.read();
if(((int)n)>0) //((int)(’ ')))
rxString += n;
}
Wire.write(“abcdefghijk”);
Serial.print(rxString);
}
Now the java code, its in the thread, creates a I2C with address 0x51 on the MXP port, does a transaction sending 'hellothere
', which the arduino sees and prints! But it gets a buffer underrun every time, see output after code.
package org.usfirst.frc.team4069.robot;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import edu.wpi.first.wpilibj.I2C;
import edu.wpi.first.wpilibj.Timer;
public class ArduinoThread implements Runnable
{
I2C mi2sdev;
byte] fromArduino = new byte[512];
public int enabled = 1;
public ArduinoThread()
{
mi2sdev = new I2C(I2C.Port.kMXP, 0x51); // 4);
}
public void run()
{
byte] buff = "hellothere
".getBytes();
while (enabled == 1)
{
try
{
if (mi2sdev.transaction(buff, buff.length, fromArduino, 11) == false) // mi2sdev.readOnly(fromArduino, 3)==false) //mi2sdev.transaction(buff, 0, fromArduino, 11)==false)
{
String str = fromArduino.toString();
System.out.println("From: " + str);
}
else
{
System.out.println("nothing?");
}
}
catch (BufferUnderflowException be)
{
System.out.println("underflow?");
}
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
} // while
}// run()
}
Arduino OUTPUT:
GotString!hellothere
GotString!hellothere
GotString!hellothere…on and on
Java output:
underflow?
underflow?
underflow?
underflow?
Any idea why the java is not getting abcdefghijk???
Thanks!