Transmitting to the arduino via Java

Greetings, I am interested in transmitting a string to the arduino from my computers com port with Java. Right now I am using this :

code to receive, and this works well. Can someone show me the example code to output a string? The article itself says :

"Transmitting data to the arduino from the computer. In my experience, it seems that the Serial connection must be connected somewhere above 1 second (I have 1.5 seconds in my program) before a transmission can be made. It seems that for at least the atmega168 and diecimila, doing so ensures your transmission actually is transmitted. For doing that, Thread.sleep(delay) is used and delay is specified in milliseconds so I would use Thread.sleep(1500) after the Serial port is established. It seems this might be due to the program resetting when a serial connection is made, and the program has a necessary setup time.

The OutputStream comes with 3 different write methods to send data from the computer to the Arduino. In the above example, you could use output.write(String) to send data, as in output.write("Hello Arduino!"). There's also one that'll send an integer value and another that will transmit an array of Bytes.

I found on the arduino's program, to accurately receive the data instead of receiving non-intelligible "255" data points, I had to maintain a generous delay between the commands. I ended up using 20 ms for safe measure, but the actual necessary number is likely much lower and one could use a "255 filter" to extract meaningful data, assuming none of your meaningful data consists of the number 255.

But neglect to show any code. Please help!

Thank you muchly!


The simple function will process a byte at a time, although there are more interesting read functions available. "

See RXTXcomm documentation. java has no inherent serial i/o. they use rxtx library but its not official (there is none).

Use Tcl. Tcl has built in support for serial i/o, runs on about 12 more platforms than java, is smaller, faster, and can be compiled into a standalone exe for your users.

Java Code. Here you make a SerialPort(rxtxcomm) object and gets it outputstream.

public void initwritetoport() {
      // initwritetoport() assumes that the port has already been opened and
      //    initialized by "public nulltest()"

      try {
         // get the outputstream
         outputStream = serialPort.getOutputStream();
      } catch (IOException e) {}

      try {
         // activate the OUTPUT_BUFFER_EMPTY notifier
      } catch (Exception e) {
         System.out.println("Error setting event notification");

   public void writetoport() {
      System.out.println("Writing \""+messageString+"\" to "+serialPort.getName());
      try {
         // write string to serial port
      } catch (IOException e) {}

Tcl here, set the comdevice, special junk for win32 and usb comm ports... then configure the baud, etc. then set rd_chid as a callback function when data arrives.

  set com [open \\\\.\\$com r+]

fconfigure $com -mode $baudrate,$parity,$databits,$stopbits \
     -blocking 0 -translation auto -buffering line -buffersize 32       
  fileevent $com readable [list rd_chid $com]