Java example on arduino.cc needs updating

Is there a way to get http://arduino.cc/playground/Interfacing/Java updated? I don’t have an account to update it. The example is very broken with the latest Arduino 0017 release. The original example also has a high CPU overhead, and this updated example removes that issue.

Here is what I did:

  • Create SerialTest.java
import java.io.*;
import gnu.io.*;
import java.util.*;

public class SerialTest implements SerialPortEventListener {
   /** The port we're normally going to use. */
   private String mzPortName = "COM3";
   /** The serial port */
   private SerialPort moSerialPort;
   /** Buffered input stream from the port */
   private BufferedInputStream moBufInputStream;
   /** The output stream to the port */
   private OutputStream moOutputStream;
   /** milliseconds to block while waiting for port open */
   private static final int TIME_OUT      = 2000;
   private static final int DATA_RATE     = 9600;
   public boolean initialize()
   {
      CommPortIdentifier oPortId = null;
      Enumeration ePortList;
      boolean bFoundPort = false;
      
      ePortList = CommPortIdentifier.getPortIdentifiers();

      //iterate through, looking for the port named in mzPortName
      while ((ePortList.hasMoreElements()) && !bFoundPort)
      {
         oPortId = (CommPortIdentifier) ePortList.nextElement();
         bFoundPort = oPortId.getName().equals(mzPortName);
      }

      //if port was found, open it
      if (!bFoundPort)
      {
         System.out.println("Could not find specified port.");
         return false;
      }

      try
      {
         //open serial port
         moSerialPort = (SerialPort) oPortId.open(mzPortName, TIME_OUT);

         //set port parameters
         moSerialPort.setSerialPortParams(DATA_RATE,
            SerialPort.DATABITS_8,
            SerialPort.STOPBITS_1,
            SerialPort.PARITY_NONE);

         //open the streams
         moBufInputStream = new BufferedInputStream(
            moSerialPort.getInputStream());
         moOutputStream = moSerialPort.getOutputStream();

         //add event listener
         moSerialPort.addEventListener(this);
         moSerialPort.notifyOnDataAvailable(true);
      }
      catch (Exception e)
      {
         System.err.println(e.toString());
         close();
         return false;
      }

      //OK, if we've made it this far, we're good to go.
      return true;
   }

   /**
    * Close the port, and release its resources.
    */
   public synchronized void close()
   {
      try
      {
         if (moBufInputStream != null)
         {
            moBufInputStream.skip(moBufInputStream.available()); //flush stream
            moBufInputStream.close();
            moBufInputStream = null;
         }
         if (moSerialPort != null)
         {
            moSerialPort.close();
            moSerialPort = null;
         }
      }
      catch (Exception e)
      {
         System.err.println(e.toString());
      }
   }

   /**
    * Handle an event on the serial port. Read the data, decode it, and 
    * store it.
    */
   public synchronized void serialEvent(SerialPortEvent oEvent)
   {
      if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) 
      {
         try
         {
            int available = moBufInputStream.available();
                  byte chunk[] = new byte[available];
            moBufInputStream.read(chunk, 0, available);

            // Display results are codepage dependent
            System.out.print(new String(chunk));
         }
         catch (Exception e)
         {
            System.err.println(e.toString());
         }
      }
      // Ignore all the other eventTypes
   }

      public static void main(String[] args) throws Exception{
            SerialTest main = new SerialTest();
            main.initialize();
            System.out.println("started");
      }
}
  • Add \arduino-0017\lib\RXTXcomm.jar to your class path.
  • Add \arduino-0017\ to your PATH on Windows. You can add the same directory to the runtime path environment variable for other platforms, like Linux or Mac OS.
  • Compile the SerialTest.java class.
  • Run SerialTest.

These steps should replace the Setup, Edit the new Java file, Compile the new Java file and Run your new Java program sections. processing.app.Preferences seems to be internal API for the Arduino development environment, and it no longer includes the run.bat file any more.

I don't have an account to update it.

You can create an account to update it at http://www.arduino.cc/playground/Main/LoginForm. The link is also near the bottom of the leftmost column on each Playground page (Participate - create an account).

Thanks for contributing both here and in the Playground!

Thanks. I didn't scroll down that far, and I thought it would have been on the login page. I'll go update it.