Search code examples
javaserial-portarduino

Send String from Java to Arduino (simple example)


I'm new to Arduino and Java, but I made a program so simple that I don't get why it isn't working.

I send a String to the serial port that correspond to Arduino (COM5):

import java.io.*;
import java.util.*;
import gnu.io.*;

public class SimpleWrite {

static Enumeration portList;
static CommPortIdentifier portId;
static String messageString = "color FF00FFEND";
static SerialPort serialPort;
static OutputStream outputStream;

public static void main(String[] args) {
    portList = CommPortIdentifier.getPortIdentifiers();


    while (portList.hasMoreElements()) {

        portId = (CommPortIdentifier) portList.nextElement();
        if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {

             if (portId.getName().equals("COM5")) {

                try {
                    serialPort = (SerialPort)
                    portId.open("SimpleWriteApp", 2000);

                    outputStream = serialPort.getOutputStream();

                    serialPort.setSerialPortParams(9600,
                        SerialPort.DATABITS_8,
                        SerialPort.STOPBITS_1,
                        SerialPort.PARITY_NONE);

                    outputStream.write(messageString.getBytes());
                    System.out.println(messageString);

                    outputStream.close();
                    serialPort.close();
                } 
                catch (IOException e) {System.out.println("err3");}
                catch (PortInUseException e) {System.out.println("err");}
                catch (IOException e) {System.out.println("err1");}
                catch (UnsupportedCommOperationException e) {System.out.println("err2");}
            }
        }
    }
}
}

and the code in Arduino to get that string:

char inputBuffer[10];   

void setup() {                
  Serial.begin(9600);  
}

void loop() {
    while (true) 
    {
      if (Serial.available() > 0) {
          Serial.readBytes(inputBuffer, Serial.available());
          delay(5000);
          Serial.print("I got this ->");
          Serial.print(inputBuffer);
          Serial.println("<-");
      }
    }
}

The while(true) is for testing purposes. I get nothing printed.

I'm using RXTXcomm.jar. Version: RXTX-2.2-20081207


Solution

  • It is solved. I put a Thread.sleep(4000) after opening the port in the java code and now it works. The problem was that the Arduino is reset everytime the port is opened, so I was sending the data when the Arduino wasn't ready to listen.

    char inputBuffer[10];   
    
    void setup()
    {                
        Serial.begin(9600);  
    }
    
    void loop()
    {
         while (true) 
         {
              if (Serial.available() > 0)
              {
                  Serial.readBytes(inputBuffer, 10);
                  delay(5000);
                  Serial.print("I got this ->");
                  Serial.print(inputBuffer);
                  Serial.println("<-");
              }
         }
    }