Go to Post If FIRST isn't your life, then you're doing something wrong! - BandChick [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Spotlight this post!  
Unread 04-02-2017, 10:30
ericalbers ericalbers is offline
Registered User
no team
 
Join Date: Feb 2015
Location: earth
Posts: 8
ericalbers is an unknown quantity at this point
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\n', 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\n".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!
Reply With Quote
 


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 22:47.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi