Nomadyn Nomadyn - 1 year ago 62
Java Question

Reading from the serial port with Java (Arduino duemilanove)

Mission: Display data in a program on a PC.

Extended mission: (after solving this problem) I will eventually save the data to files and expand the program into a GUI.

Microcontroller: Arduino duemilanove

OS: Windows 7

Language: Java

Compiler: Eclipse

Io method: USB type 'B' (arduino side), USB type A (PC side)

I've read at least 30 different tutorials/questions here and on other websites. I've grabbed examples from sites and downloaded a few libraries and keep getting red lines and errors. I've now scrapped it all and decided to humbly come here and see if anyone is willing to help me out.

I suspect I'm in over my head but that's fine, I'm happy to learn, and be called stupid!

I understand other software, such as 'processing' is already available that could do this for me. I want to make this software myself.

I've looked at RXTX and COMM API. To be honest, all I've gotten out of it so far is that I know it exists, some stuff is outdated apparently, and it seems most tutorials assume a large amount of knowledge in various subjects.

I also looked at more questions asked here as I write this question, and most of them I have already seen before. I'm waving the white flag! someone help me learn what I'm doing!

Code example of what I want to do.

// How would I make this class read from the arduino?

class arduinoData {

static int t = (int) 72.91; //pull from arduino instead of making it up

static int temperature(){

return t;


public class example
public static void main(String[] args)
System.out.println("temp from the sensor is: "+ arduinoData.temperature()+"F");
//temp from the sensor is: 71F

Edit: Arduino serial monitor works correctly. I'll add the arduino code to clear that up.

#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 3
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

DeviceAddress Thermometer = { 0x28, 0x56, 0xA0, 0xA5, 0x03, 0x00, 0x00, 0x7D };

void setup(void)
sensors.setResolution(Thermometer, 10);

pinMode(13, OUTPUT);


void printTemperature(DeviceAddress deviceAddress)
float tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00) { Serial.print("Error."); }
else {Serial.print(DallasTemperature::toFahrenheit(tempC));Serial.print("F");}

void loop(void)
digitalWrite(13, HIGH);
digitalWrite(13, LOW);
Serial.print("temp from sensor: ");printTemperature(Thermometer);

Arduino output:

Temp from sensor: 70.25F

Temp from sensor: 70.70F

Temp from sensor: 70.70F

Temp from sensor: 70.70F


Ok I think I have rxtx installed correctly via the tutorial mentioned below. However, I'm lost at this point:

For Windows you can create a batch file called run.bat in the same
directory as the sample code mentioned above. This batch file can be
used on your system, but you may need to modify the paths to match the
install locations of your Java and Arduino IDE on your system.

setlocal set PATH=%PATH%;C:\Program Files (x86)\arduino-0017\
"C:\Program Files (x86)\Java\jdk1.6.0_12\bin\javac" -cp "C:\Program
Files (x86)\arduino-0017\lib\RXTXcomm.jar" "C:\Program
Files (x86)\Java\jdk1.6.0_12\bin\java" -cp "C:\Program Files
(x86)\arduino-0017\lib\RXTXcomm.jar;." SerialTest

Basically, you will need to add the RXTXcomm.jar to your class path,
and you will need to add the associated JNI interface DLL
(rxtxSerial.dll) to your PATH. For other platforms, you will need to
add the same JNI shared library to your run time path.

When you run the run.bat file, you should see the same results as the
Arduino IDE Serial Monitor. You can use Ctrl+Break to stop the

What am I supposed to be doing???

Answer Source

Check if your firmware is actually making Arduino print to the usb, that is virtualized as serial port: load the firmware and test the serial communication via the "Serial monitor" (the lens button on the right).

Once done, you know your problem is related to the java code that attemps to read from the COM interface. If you still have issues here, your problem has moved to "reading from the serial port with Java".

For a general reference about interfacing Arduino with Java I suggest you to read this tutorial with explanations and code samples: