1

So I found out how to connect the Arduino to my java program. But using the serial connections doesn't give any useful data back, its either in the wrong format, or just sends it as a box. I've looked at the related questions posted early in here, but none of the tips seems to help. So does anyone know how to send the data between an Arduino and a computer using the serial port?

This is the code I'm using, provided by this person: http://silveiraneto.net/2009/03/01/arduino-and-java/

package serialtalk;

import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.InputStream;
import java.io.OutputStream;
import processing.app.Preferences;

public class Main {
    static InputStream input;
    static OutputStream output;

    public static void main(String[] args) throws Exception{
        Preferences.init();
        System.out.println("Using port: " + Preferences.get("serial.port"));
        CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(
                Preferences.get("serial.port"));

        SerialPort port = (SerialPort)portId.open("serial talk", 4000);
        input = port.getInputStream();
        output = port.getOutputStream();
        port.setSerialPortParams(Preferences.getInteger("serial.debug_rate"),
                SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE);
        while(true){
            while(input.available()>0) {
                System.out.print((char)(input.read()));
            }
        }
    }
}

The Arduino is this: http://www.arduino.cc/en/Main/ArduinoBoardDuemilanove

The code simply receives a number, and determines which analog reading it should be sending back, from my Arduino.

2
  • Is it a problem with Your java application or something else?. Can You send text to the board using the serial monitor for example? Commented Apr 30, 2010 at 14:40
  • I am able to send data to the Netbeans IDE from the arduino, when just writing "Hello World!". The serial monitor in the arduino IDE does not seem to recieve anything when i run my java program Commented May 1, 2010 at 9:28

1 Answer 1

1

When dealing with serial connections make sure of the following key points:

  • BAUD Rates match
  • DATABITS should match
  • STOPBITS should match
  • PARITY should match
  • Make sure you are using the correct cable (Null modem if needed)
  • Make sure the cable run isn't too long

All of the above can cause odd stuff to come out of the com port on the Java side. Once you get the hang of this it gets much easier.

My personal favorite library here.

Sign up to request clarification or add additional context in comments.

4 Comments

It's most probably a virtual COM port and the cable is a USB cable, unless the arduino board is pretty old. If it were a real RS232C connection one could also consider the type of handshaking :)
It is the duemilanove Im using, I am already using the RXTX library imported into the Netbeans IDE i use. I am able to send data to the serial monitor in netbeans, and receive the data sent from the arduino, but i cant receive the data and interfer with the ardiuno, such as using an If statement, maybe my data is sent wrong?
@Maciej H I have used the arduino with java and with other components I just know with that I have had to adjust these parameters to get everything to play nice.
@Casper can you post the code you are using and a link to the board you are using?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.