read string sent from java from serial port in arduino

Hi, i'm trying to send a string with this code in java. I know it works cause i monitorized a virtual serial port and it says it writes the bytes i send. I use the port COM5 which is the one im using in my arduino.

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);
                    } catch (PortInUseException e) {System.out.println("err");}
                    try {
                        serialPort.setSerialPortParams(9600,
                            SerialPort.DATABITS_8,
                            SerialPort.STOPBITS_1,
                            SerialPort.PARITY_NONE);

			serialPort.setFlowControlMode(
        			SerialPort.FLOWCONTROL_NONE);

                    } catch (UnsupportedCommOperationException e) {System.out.println("err2");}
                    try {
                        outputStream = serialPort.getOutputStream();
                    } catch (IOException e) {System.out.println("err1");}

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

			//outputStream.close();
			//serialPort.close();

                    } catch (IOException e) {System.out.println("err3");}
                }
            }
        }
    }
}

The code in arduino is this. I try to print something to see if it works and is receiving correctly, but it doesn't even enter in the if because it doesn't detect that data is received (sorry for my english, hope it is understandable).
I put the delay so i can see the com monitor in arduino in time, because i have to have it closed while i run the java app.

void setup() {      
  Rb.init();  
  // initialize the digital pin as an output.
  Serial.begin(9600);  
}

// the loop routine runs over and over again forever:
void loop() {
    while (true) 
    {
      if (Serial.available() > 0) {
          Serial.readBytes(inputBuffer, Serial.available());
          delay(15000);
          Serial.print("I got this ->");
          Serial.print(inputBuffer);
          Serial.println("<-");
       }
   }
}

Hope somebody can help cause i'm stuck in this. Thank you

Please post your code.
All of it.

The java code is complete. Here goes the arduino code:

#include <Rainbowduino.h>
 
String inputString = ""; 
char inputBuffer[10];   // For incoming serial data
char colhex[384];
unsigned long colorhex;

// the setup routine runs once when you press reset:
void setup() {      
  Rb.init();  
  // initialize the digital pin as an output.
  Serial.begin(9600);  
}

// the loop routine runs over and over again forever:
void loop() {

      if (Serial.available() > 0) {
          Serial.readBytes(inputBuffer, Serial.available());
          delay(15000);
          Serial.print("I got this ->");
          Serial.print(inputBuffer);
          Serial.println("<-");  
      }

}

I have the rainbowduino.h imported cause im using rainbowduino and i have done some tests with it.

Let's make a list of steps that the Java code performs, if I can find them all in that piss-poorly indented code:
serialPort = (SerialPort)portId.open("SimpleWriteApp", 2000);
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
outputStream = serialPort.getOutputStream();
outputStream.write(messageString.getBytes());

The try/catch structure might LOOK good, but it is a complete waste of effort. If opening the serial port fails, it is useless to then set the parameters and write to it.

Opening the port resets the Arduino. Jamming data at it while it's rebooting is useless. By the time the Arduino is ready to receive data, you're in listen mode, so it's no wonder the Arduino never hears anything.

      if (Serial.available() > 0) {
          Serial.readBytes(inputBuffer, Serial.available());

This is a load of crap. Suppose, by some miracle, that 64 bytes are available to read. How the hell are they supposed to fit in a 10 element array?

Ok ok, I see that the java code is not structurated properly, i did a copy paste from a code i found, and when i saw that it writes with my configuration i leaved it like that. Sorry for that.

You're right with the size of the buffer, I have changed it.

The arduino is reset when I open the port in java? or you refer to when i open the monitor serial?

The arduino is reset when I open the port in java? or you refer to when i open the monitor serial?

If the Java application is using the serial port to talk to the Arduino, why would you open the Serial Monitor to talk to the Arduino, too?

Not that it matters. It is opening the serial port that resets the Arduino, whether it is Java, Python, C#, or the Serial Monitor doing it.

I was trying to read the string that i sent with the java code in the console.

Anyway, your advice helped me a lot, i put a Thread.sleep(4000); after opening the port in the java code and now it works

Thank you.

This demo uses Python to communicate with an Arduino also includes a JRuby version of the Python code (JRuby uses the JVM). The Arduino code also works directly with the Arduino IDE.

I suggest you write your Java code to work with the Arduino code to get things started. Once you have established communications ( :slight_smile: ) you can expand from there.

...R