Hi, this is my first post. I have Arduino a few days, but I still can't connect it and send any data to Arduino or receive it from Arduino using Java. I've tested RXTX and JavaComm but still there is nothing working, so maybe you will help me.
This is first version of my code:
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.*;
import java.io.InputStream;
import java.io.OutputStream;
import gnu.io.RXTXCommDriver.*;
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"));
System.out.print(CommPortIdentifier.getPortIdentifiers().toString());
}
}
and output:
run:
Using port: COM4
java.lang.NoClassDefFoundError: javax/comm/CommDriver thrown while loading gnu.io.RXTXCommDriver
java.lang.NoClassDefFoundError: javax/comm/CommDriver thrown while loading gnu.io.RXTXCommDriver
gnu.io.CommPortEnumerator@5740bb
BUILD SUCCESSFUL (total time: 3 seconds)
There is also second version:
package arduino2;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.*;
import java.io.InputStream;
import java.io.OutputStream;
import gnu.io.RXTXCommDriver.*;
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 madness", 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()));
}
}
}
and output:
Using port: COM4
java.lang.NoClassDefFoundError: javax/comm/CommDriver thrown while loading gnu.io.RXTXCommDriver
Exception in thread "main" gnu.io.NoSuchPortException
at gnu.io.CommPortIdentifier.getPortIdentifier(CommPortIdentifier.java:218)
at arduino2.Main.main(Main.java:21)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)
With Arduino software all works fine, i don't understand why it doesn't with my soft. In Netbeans project are all .dll files, all Jar libraries from arduino lib folder and also preferences.txt .
But still no effect. Thanks for any help, maybe someone can upload working netbeans project, so i can try it out, maybe my laptop isn't working good, i'm looking errors all day long. Forgive me my english.