Communication With Java

Hi I have a Duemilanove and an Uno. I am using two boards because I need 8 PWM pins (using 4 from each).

All I need to do is send 4 numbers to the Arduino and use those 4 values to set the PWM output on 4 pins.

I am having trouble sending serial communication to/from my Uno to a computer (the Duemilanove works great) and really need to get this solved today (thanks for the help)

I connect the Duemilanove to my computer and it shows up as device /dev/ttyUSB0 and I connect the Uno to my computer and it shows up as device /dev/ttyACM0.

If I send serial communication from the Arduino IDE the UNO communicates fine. If I try sending from my computer it fails to find the device.

I have debugged it and found that the method Enumeration portEnum = CommPortIdentifier.getPortIdentifiers(); does not return /dev/ttyACM0 as a device (it does return /dev/ttyUSB0 which is why the Duemilanove works)

Any way to get around this? I am kinda desperate on this.

This is my Arduino code (both boards have same code)

// Communication with Antenna
int inByte = 0;         // Incoming serial byte
int outputPin1 = 11;
int outputPin2 = 10;
int outputPin3 = 9;
int outputPin4 = 6;

void setup()  { 
  
  // Open a serial connection to the computer
  Serial.begin(9600);
  
  // Set the PWM ports to output
  pinMode(outputPin1,OUTPUT);
  pinMode(outputPin2,OUTPUT);
  pinMode(outputPin3,OUTPUT);
  pinMode(outputPin4,OUTPUT);
} 

void loop()  { 
  
  if (Serial.available() > 0) {
    
    Serial.println("Recieved Message");
    
    // Get incoming byte, Echo the number back to 
    // the computer then write voltage to PWN pin
    inByte = Serial.read();
    Serial.println(inByte);
    analogWrite(outputPin1, inByte);
    delay(20);
   
    inByte = Serial.read();
    Serial.println(inByte);
    analogWrite(outputPin2, inByte);   
    delay(20);
    
    inByte = Serial.read();
    Serial.println(inByte);
    analogWrite(outputPin3, inByte);   
    delay(20);
    
    inByte = Serial.read();
    Serial.println(inByte);
    analogWrite(outputPin4, inByte);   
    delay(20);
  }
}

This is my Java code:

package cessararduino;

import gnu.io.NoSuchPortException;
import java.io.InputStream;
import java.io.OutputStream;
import gnu.io.CommPortIdentifier; 
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent; 
import gnu.io.SerialPortEventListener; 
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;

public class SerialTest implements SerialPortEventListener {

        SerialPort serialPort_09;
        SerialPort serialPort_10;
        
        /** The port we're normally going to use. */
    private static final String PORT_NAMES_09[] = { 
            "/dev/tty.usbserial-A9007UX1", // Mac OS X
            "/dev/ttyUSB0", // Linux
            "COM3", // Windows
            };
        private static final String PORT_NAMES_10[] = { 
            "/dev/tty.usbserial-A9007UX1", // Mac OS X
            "/dev/ttyACM0", // Linux
                        "/dev/ttyACM1", // Linux
                        "/dev/ttyUSB1", // Linux
            "COM3", // 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() {
            System.out.println("Initializing the object");            
        }
        
    public void initialize_09() {
        CommPortIdentifier portId_09 = null;
        Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
        try {
            CommPortIdentifier.getPortIdentifier("/dev/ttyACM0");

            // iterate through, looking for the port
        } catch (NoSuchPortException ex) {
            Logger.getLogger(SerialTest.class.getName()).log(Level.SEVERE, null, ex);
        }

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

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

        try {
            // open serial port, and use class name for the appName.
            serialPort_09 = (SerialPort) portId_09.open(this.getClass().getName(),
                    TIME_OUT);

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

            // open the streams
            input = serialPort_09.getInputStream();
            output = serialPort_09.getOutputStream();

            // add event listeners
            serialPort_09.addEventListener(this);
            serialPort_09.notifyOnDataAvailable(true);
        } catch (Exception e) {
            System.err.println(e.toString());
        }
            
    }

    public void initialize_10() {
        CommPortIdentifier portId = null;
        Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();

        // iterate through, looking for the port
        while (portEnum.hasMoreElements()) {
            CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
            for (String portName : PORT_NAMES_10) {
                if (currPortId.getName().equals(portName)) {
                    portId = currPortId;
                    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_10 = (SerialPort) portId.open(this.getClass().getName(),
                    TIME_OUT);

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

            // open the streams
            input = serialPort_10.getInputStream();
            output = serialPort_10.getOutputStream();

            // add event listeners
            serialPort_10.addEventListener(this);
            serialPort_10.notifyOnDataAvailable(true);
        } catch (Exception e) {
            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_10 != null) {
            serialPort_09.removeEventListener();
            serialPort_09.close();
        }
        if (serialPort_10 != null) {
            serialPort_10.removeEventListener();
            serialPort_10.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 {
                int available = input.available();
                byte chunk[] = new byte[available];
                input.read(chunk, 0, available);

                // Displayed results are codepage dependent
                System.out.print(new String(chunk));
            } catch (Exception e) {
                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 serialPort_09 = new SerialTest();
            SerialTest serialPort_10 = new SerialTest();
            
            try{
        serialPort_09.initialize_09();
                //serialPort_10.initialize_10();
                
                // Wait 2 seconds for serial port to settle
                Thread.sleep(2000);
                
                System.out.println("Started");
                
                // Send 6 numbers to the arduino board
                serialPort_09.output.write(0);   // Output1
                serialPort_09.output.write(64);  // Output2
                serialPort_09.output.write(128); // Output3
                serialPort_09.output.write(255); // Output4
                
                // Send 6 numbers to the arduino board
                serialPort_10.output.write(0);   // Output1
                serialPort_10.output.write(64);  // Output2
                serialPort_10.output.write(128); // Output3
                serialPort_10.output.write(255); // Output4
            }
            catch (Exception e)
            {
                serialPort_09.close();
                serialPort_10.close();
            }
                
    }
}