Go Down

Topic: Serial communication with java (Read 568 times) previous topic - next topic

jrey

Good afternoon,

Recently purchased an Arduino Leonardo, I am a software developer but is very motivating to make software that communicates with the physical world.

The question is:

I'm working on a project to read data from a temperature sensor and store them in a database.

The reading part of the sensor from Arduino is very simple, I'm going to communicate with Java I searched and there is much info.

I followed this tutorial as it

http://silveiraneto.net/2009/03/01/arduino-and-java/

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

My program does not come to this part:

hile(input.available()>0) {
                System.out.print((char)(input.read()));

Any suggestions, the immensely appreciate.

PaulS

Have you tried with a different Arduino? The Leonardo is different in that the DtrEnable flag for the serial port needs to be set. I have no idea how to do that with Java (hope you do).


spatula

I would start from the Arduino part: if you upload the program from the IDE and open the serial monitor (in the Tools menu), do you see the message "Is there anybody out there?"

I assume that the values in the preferences file are ok, i.e. serial.port corresponds to the name of a serial port you can see in your system's resources (and in Arduino IDE), and serial.debug_rate is 9600. Can you confirm that?

One possible reason you don't get messages from the Java application is that you are running the program while the Arduino IDE is still running: the IDE is using the serial port and the Java application cannot access it. You may try and close the Arduino IDE after uploading the program (but leaving the USB connected) also check that there is no avrdude process running in background, then start the Java application.

If unsuccessful, can you change the Java code to inspect the properties of 'port' after this line of code is executed?

Code: [Select]
SerialPort port = (SerialPort)portId.open("serial talk", 4000);

I don't know the class, but perhaps there are methods in SerialPort that can check whether the port is actually open.

Please remember to enclose any code you post within code blocks.

PeterH


Any suggestions, the immensely appreciate.


What operating system are you running the Java application on? What have you set serial.port to? How have you verified that you have specified the correct port?
I only provide help via the forum - please do not contact me for private consultancy.

jrey

Hi,

Windows 7.

and the port is also verified.

PeterH


Hi,

Windows 7.

and the port is also verified.


Here is what I actually asked:

Quote

What operating system are you running the Java application on?

What have you set serial.port to?

How have you verified that you have specified the correct port?

I only provide help via the forum - please do not contact me for private consultancy.

jrey

Hi,

follow this link, by the way very good.

But my mistake was that I did on 64 bit when I go to 32 bit everything worked very well

thanks.

Go Up