Go Down

Topic: Java example is for Unix, what about Windows (Read 646 times) previous topic - next topic

LMI

Sep 14, 2009, 09:25 pm Last Edit: Sep 14, 2009, 11:29 pm by LMI1 Reason: 1
There are imports for gnu. and processing. Besides there are no run.bat or testrun.bat files in C:\Program Files\arduino-0017\ folders. Perhaps the instructions are dated?

Edit: I have problems with contacting Arduino forums today. This post was here 3 times. I deleted other two.

mellis


LMI

From this:
http://www.arduino.cc/playground/Main/InterfacingWithSoftware

to here (Java)
http://www.arduino.cc/playground/Interfacing/Java

LMI

Well, no answers.
I googled "serial communication java" and this I found. Among others:
http://en.wikibooks.org/wiki/Serial_Programming/Serial_Java
Sun has stopped Java serial port support and those strange imports are from an other serial port library rxtx http://www.rxtx.org/.
I have not tried it, but it is promised to work.

Robert Hunt

This is a sample I wrote which runs under Windows with the RXTX library. It connects to the Arduino on COM3 and prints the character 'H' to it and closes the port. You can change the port parameters easily.

I had to insert a sleep instruction into the code as trying to talk to the serial port immediatly doesn't seem to work, it needs a second or so before it will send data.

You will need the RXTXcomm.jar in the classpath as well as rxtxSerial.dll

Code: [Select]

import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.InputStream;
import java.io.OutputStream;

public class CommTest {

   static InputStream input;
   static OutputStream output;
   static SerialPort port;

   public static void main(String[] args) throws Exception {

       // Open the serial port
       CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier("COM3");
       port = (SerialPort) portId.open("Arduino", 4000);

       // Get the port input/output streams
       input = port.getInputStream();
       output = port.getOutputStream();

       // Set the port parameters
       port.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

       // Wait for the serial port
       Thread.sleep(1500);

       // Write 'H' to the serial port
       output.write(72);

       // Close the serial port
       port.close();
   }
}

LMI

Thanks

There is also a useful info about how to install rxtx on homepage, I think. Although RXTX wepsite is a bit messy.

I have not tried your code yet, because our project is "resting" for a while, but java code is nice to have.

Is the java example code any good?

Go Up