Reading I2C MXP port on roborio

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!

Hey!
You are treating as if the transaction method is plainly a Boolean but instead you should treat it like if its a method.
Try taking a look at our code that is on our repo, which accomplishes this very function.
The repo is here: https://github.com/FRC-Team-1403/RioDuinoIMUCode

-axton900

https://www.arduino.cc/en/Reference/Wire

The Arduino Wire Library for I2C Communications has two functions that are very familiar. They are onReceive() and onRequest.

The roboRIO always acts as the master for all I2C transactions, so the RoboRIO has to tell the Arduino to read AND write values. The onReceive(handler) function tells the Arduino to execute handler when the RoboRIO writes data to the Arduino. The onRequest(handler) functions tells the Arduino to execute handler when the RoboRIO is reading data from the Arduino.

My point is that you have to use onRequest(). Since the Arduino is setup as a slave device, it has to be told by the RoboRIO that it is okay to send data. Otherwise, data will not be sent. Overall, onRequest() is enabling to Arduino to Write values to the RoboRIO, and onReceive() is enabling the Arduino to read values.

I believe that this is the correct solution for this issue