11

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

8
  • Can you tell us which Lib (and Version of it) you are using for Serial I/O? Are you sure you open COM5? Are you sure you can change serialPort Params after getting the outputstream? Commented Aug 26, 2014 at 9:50
  • Im using RXTXcomm.jar. Version: RXTX-2.2-20081207. I put traces, and im sure it opens the com5, and that it changes serialport params Commented Aug 26, 2014 at 9:56
  • I found this example, maybe it helps embeddedfreaks Commented Aug 26, 2014 at 10:00
  • 2
    @takluiper Post your solution you edited in your question as an answer and accept it so this question doesn't appear as unanswered. Thanks and cheers! Commented Jul 19, 2015 at 17:21
  • 1
    why not contact arduino directly and wait for arduino to send a "i'm ready!" flag? Commented Jun 3, 2020 at 22:06

2 Answers 2

4

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("<-");
          }
     }
}
Sign up to request clarification or add additional context in comments.

1 Comment

For ~1500ms are enough. Another way is to send a message from the Arduino in the setup function and have a polling for the data from the Java side waiting for that message and when received to unblock your sending of the data from the Java side.
3

You could use the Java-Arduino Communication Library. It can be found here: https://sourceforge.net/projects/javaarduinolibrary/. (be sure to download both jars) Then your code would look something like this:

import arduino.*;
class JavaArduinoComm {
        public static void main(String[] args) {
        Arduino obj = new Arduino('PortDescription', BAUD_RATE);
        obj.openConnection();
    }
}

Then you can use any of the following methods:

  1. String serialRead(int limit) - returns a string containing as many readings as the value of limit. recommended for reading
  2. void serialWrite(String s) - writes the contents of the entire string to the serial at once. written as string to serial.
  3. void serialWrite(String s, int noOfChars, int delay) - writes the contents of the strings to the serial gradually. It writes the string in incremental steps with 'noOfChars' charaacters each time, with a pause of 'delay' milliseconds between each write. written as string to serial. recommended to write String
  4. void serialWrite(char c) - writes the individual char to the serial in datatype char.
  5. void serialWrite(char c, int delay) - writes the individual char to the serial in datatype char and pauses the thread for delay milliseconds after. recommended to write char

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.