Arduino java interface

hello, i'm trying to interface arduino with JAVA. i copied the code from i got no error during compilation. but during runtime, program doesnot stop but continues to wait at the following line.

serialPort.setSerialPortParams(DATA_RATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); so i commented this line. then the program was able to get the number of byte available for reception. but when it executes the line, 0, available);

it throws an exception.

here is the whole program that i modified

import; import; import; import; import; import; import java.util.Enumeration;

public class SerialTest implements SerialPortEventListener { SerialPort serialPort; /** The port we're normally going to use. / private static final String PORT_NAMES[] = { "COM6" // Windows }; /* Buffered input stream from the port / private InputStream input; /* The output stream to the port / private OutputStream output; /* Milliseconds to block while waiting for port open / private static final int TIME_OUT = 2000; /* Default bits per second for COM port. */ private static final int DATA_RATE = 9600;

public void initialize() { CommPortIdentifier portId = null; Enumeration portEnum = CommPortIdentifier.getPortIdentifiers(); System.out.println("???????????????????????????");

// iterate through, looking for the port while (portEnum.hasMoreElements()) { CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement(); System.out.println(currPortId.getName()); for (String portName : PORT_NAMES) { System.out.println(portName); if (currPortId.getName().equals("COM6")) { portId = currPortId; System.out.println("portId"); System.out.println(portId.getName()); break; } } }

if (portId == null) { System.out.println("Could not find COM port."); return; }

try { // open serial port, and use class name for the appName. serialPort = (SerialPort)"com6", TIME_OUT); System.out.println(portId.getClass().getName());

// set port parameters //serialPort.setSerialPortParams(DATA_RATE, //SerialPort.DATABITS_8, //SerialPort.STOPBITS_1, //SerialPort.PARITY_NONE); serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE); //serialPort.setSerialPortParams(); System.out.println("Parameter set");

// open the streams input = serialPort.getInputStream(); output = serialPort.getOutputStream(); System.out.println("Streams set");

// add event listeners serialPort.addEventListener(this); serialPort.notifyOnDataAvailable(true); System.out.println("INPUT OUTPUT"); } catch (Exception e) { System.out.println("HEREEEEEEEEEEEEEEEE"); System.err.println(e.toString()); } }

/** * This should be called when you stop using the port. * This will prevent port locking on platforms like Linux. */ public synchronized void close() { if (serialPort != null) { serialPort.removeEventListener(); serialPort.close(); } }

/** * Handle an event on the serial port. Read the data and print it. */ public synchronized void serialEvent(SerialPortEvent oEvent) { if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) { try { System.out.println("FINALLLLLLLLLLLLLLLLLLLLL"); int available = input.available(); System.out.println(available); byte chunk[] = new byte[available]; System.out.println("Starting");, 0, available); System.out.println("data received");

// Displayed results are codepage dependent System.out.print(new String(chunk)); } catch (Exception e) { System.out.println("here atleastttttttt"); System.err.println(e.toString()); } } // Ignore all the other eventTypes, but you should consider the other ones. }

public static void main(String[] args) throws Exception { SerialTest main = new SerialTest(); System.out.println("hello"); System.out.println("Hello"); main.initialize(); System.out.println("Started"); } }

here is link to original program:

here is the screenshot of the error that i get:

Well that would be because the lines you commented out are the lines that configure the serial port. I'm afraid thats not such a great idea. As for why they weren't working in the first place, im not very experienced in Java, but it looks like you don't have sufficient permission to access the serial port. If you could post the log mentioned in your screenshot, it would be helpful.