views:

2453

answers:

3

I'm trying to communicate between my PC (Windows 7 using Netbeans and RXTX) with an Arduino Pro, using the serial port. The Arduino is actually connected to the PC using an FTDI cable.

The code is based on the Java SimpleRead.Java found here.

Currently the Arduino simply prints out a string when it starts up. My Java program should print the number of bytes that have been read and then print out the contents. The Java program works, sort of...

If the string is long (>10 bytes or so) the output will get broken up.

So if on the Arduino I print

Serial.println("123456789123456789"); //20 bytes including '\r' and '\n'

The output of my Java program may look something like:

Number of Bytes: 15   
1234567891234  
Number of Bytes: 5  
56789

or

Number of Bytes: 12   
1234567891  
Number of Bytes: 8  
23456789

I'm thinking it's a timing problem, because when I manually go through the code using the debugger, the result string is always what it should be: one 20 byte string.

I've been messing with various things but I haven't been able to fix the problem.

Here is the part of the code that is giving me problems:

static int baudrate = 9600,
           dataBits = SerialPort.DATABITS_8,
           stopBits = SerialPort.STOPBITS_1,
           parity   = SerialPort.PARITY_NONE;    

byte[] readBuffer = new byte[128];

...
...

public void serialEvent(SerialPortEvent event)
{
   if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) {

    try {
        if (input.available() > 0) { 
            //Read the InputStream and return the number of bytes read
            numBytes = input.read(readBuffer);

            String result  = new String(readBuffer,0,numBytes);
            System.out.println("Number of Bytes: " + numBytes);
            System.out.println(result);
        }
    } catch (IOException e) {
        System.out.println("Data Available Exception");
    }
}
+3  A: 

Serial data is just a stream of data. Depending on when you read it and the buffering that is happening, only part of the data may be available when you read it.

Since you are using line oriented data, what you will want to do is buffer the data until you see the line terminator and only then process the data.

R Samuel Klatchko
For some reason I assumed that data would be sent contiguously in a single stream. On the PC end, I only need to view the Serial output, so I don't think I will bother trying to store the complete incoming line and then display it. I think it I will just display the data byte-by-byte as it comes in. So I'll use aByte = input.read(); instead of input.read(readBuffer); Thanks
SharpBarb
@SharpBarb - no need to switch to reading a byte at a time. If you really want to do that, I'd recommend reading a buffer and then within your app just process each byte separately.
R Samuel Klatchko
A: 

I haven't used Java RXTX, but I've played with Arduino and Processing and it's pretty easy to read/write values from Arduino. Here is a read sample that comes with Processing(File > Examples > Libraries > Serial > SimpleRead)

/**
 * Simple Read
 * 
 * Read data from the serial port and change the color of a rectangle
 * when a switch connected to a Wiring or Arduino board is pressed and released.
 * This example works with the Wiring / Arduino program that follows below.
 */


import processing.serial.*;

Serial myPort;  // Create object from Serial class
int val;      // Data received from the serial port

void setup() 
{
  size(200, 200);
  // I know that the first port in the serial list on my mac
  // is always my  FTDI adaptor, so I open Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
}

void draw()
{
  if ( myPort.available() > 0) {  // If data is available,
    val = myPort.read();         // read it and store it in val
  }
  background(255);             // Set background to white
  if (val == 0) {              // If the serial value is 0,
    fill(0);                   // set fill to black
  } 
  else {                       // If the serial value is not 0,
    fill(204);                 // set fill to light gray
  }
  rect(50, 50, 100, 100);
}



/*

// Wiring / Arduino Code
// Code for sensing a switch status and writing the value to the serial port.

int switchPin = 4;                       // Switch connected to pin 4

void setup() {
  pinMode(switchPin, INPUT);             // Set pin 0 as an input
  Serial.begin(9600);                    // Start serial communication at 9600 bps
}

void loop() {
  if (digitalRead(switchPin) == HIGH) {  // If switch is ON,
    Serial.print(1, BYTE);               // send 1 to Processing
  } else {                               // If the switch is not ON,
    Serial.print(0, BYTE);               // send 0 to Processing
  }
  delay(100);                            // Wait 100 milliseconds
}

*/

As far as I remember, the baud thingy you setup in Arduino when you instantiate Serial is pretty important. If you use 9600 to send for example, you should use the same number to listen.

Also it's pretty important to send your information as BYTE, otherwise you'll have stuff like \r or \n in the way.

Shorter version, try:

Serial.println(123456789123456789,BYTE);

The simpler the better.

George Profenza
A: 

Hello. I'm really sorry to bump this thread, but I am having issues with java RXTX and my Arduino as well. I am using NetBeans as my IDE, and have set it up properly (I hope) based on the RXTX Wiki site. Using the same resources as SharpBarb used, I was able to get the SerialRead program to work, but I am having troubles with the SerialWrite program. (I have made sure that my arduino is connected to serial port COM4, and have modded any programs to work with this port.) The serialWrite program seems to execute properly as no errors are thrown, however, my arduino seems to reset itself when the SerialWrite program is run.

Right now I am just trying to get this to function properly, so I made a simple sketch for my arduino to signal me that it has received some data:


int received = 0;

void setup(){ Serial.begin(9600); pinMode(13,OUTPUT); }

void loop(){

if(Serial.available()>0){

received = Serial.read();
digitalWrite(13,HIGH);//pin 13 is an LED, it is turned on to signal data has been received
//Serial.print("Data has been recieved...");

}

delay(1000); digitalWrite(13,LOW); }


I have no idea why my arduino seems to be resetting, but any help with this issue would be greatly appreciated. Thanks in advance.

@SharpBarb - could you by any chance send my your source code for your above mentioned project? you can contact info is here: http://inventor22.myopenid.com/ Any code examples would really help out.

Inventor22