views:

313

answers:

2

Hello everyone

So i found out how to connect the arduino to my java program. But using the seriel connections dosent 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 serieal port?

P.S I've used the sample code from the arduino.cc homepage to connect to the serial port.

+1  A: 

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.

Jeff Beck
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 :)
Maciej Hehl
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?
Casper Marcussen
@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.
Jeff Beck
@Casper can you post the code you are using and a link to the board you are using?
Jeff Beck
A: 

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()));
        }
    }
}

}

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

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

The code simpley receives a number, and determines which analog reading it should be sending back, from my arduino

I am not very experienced with these libraries, but i know the basic about java.

Sorry couldn't comment such a big post.

Casper Marcussen
Please post Your code for the Arduino too. Especially the setup() function
Maciej Hehl
It's very simple, since i was thinking that i could handle the serial reading in the Loop function:Void setup() {Serial.begin(9600);void loop() {if (Serial.available() > 0) {Serial.println("I've got a signal");}Serial.println("I didn't receive any data");delay(1000);}I have also tried with just printing numbers such as 33 and 42, but it does not change anything. The porpuse is getting the Arduino connected to a java program, and making the java generate a number from math.random, and set one of the LEDs as high, and read the analog value, and decide if it is high enough.
Casper Marcussen
Is this possible to do by exchanging the sort of commands through serial connection?
Casper Marcussen
Hm I'm not sure what this line does, but I have a gut feeling, that it sets the baudrate. port.setSerialPortParams(Preferences.getInteger("serial.debug_rate"). If the debug rate is the same as the one used for uploading programs to the board It might be different than 9600. I think the bootloader operates at 19200. Check it out
Maciej Hehl
One more thing. You have to wait a bit after opening the port before sending anything to the board. After opening the port arduino is automatically reset and bootloader starts. You have to wait until it timeouts and the execution goes to Your code
Maciej Hehl