Read Serial.print() from arduino on laptop? Java

Ok, so I'm using Java and I'm trying to set up communication between a program running on the computer and the arduino. I've successfully been able to send data from the computer to the arduino, but I've hit a stumbling block in trying to send data from the arduino to the computer and read it in my custom application. I know it's readable from the output box in the arduino IDE, but I'm trying to get it working with my program.

Anyone know how?

I'm using the Serial class that's used by the Arduino IDE in order to write to the arduino and that works well. I'm now trying to use the Serial class's read function to read from the arduino when the arduino executes Serial.print(string), but it doesn't seem like there's anything available in the serial port. Has anybody done this before?

Hmmm... I'm now executing program and watching the TX and RX LEDs and it doesn't seem like the arduino ever sends anything out, but yet it does blink to indicate it's being sent something.

Hah! There was a disconnection, and so now when I send out the "SPEAK!" command to the arduino, it now spits out its contents as indicated the TX led, but it's not reading it in the application. I'll have to play around with this...

Have you considered starting with a processing app on the pc?

I haven't considered processing but I don't think it'd be that straightforward to build it ontop of the existing application (And I have a feeling it would complicate installation), unless I could call a processing app within java.

So, I tried structuring it off the link here:

http://www.arduino.cc/playground/Interfacing/Java

With the first example, and it seems to open the port fine and it actually reads whatever the arduino is speaking out. So, I try to write to the arduino using the outputStream and it appears that it's actually sending something because the RX led will only blink when I use outputStream.write(value), but it appears that the arduino isn't responding to the message (It's basically a command to the arduino to send some values back). If I send the message, "The letter D", through the Arduino IDE, the arduino responds in kind.

So it appears the arduino is getting the message sent to it, but it doesn't seem to be either getting it correctly or responding to it correctly.

Here's the code where it's sending the valuies. The auto-generated try/catch blocks really make it inelegant.

Basically, this sends the value 68 to the arduino(Which corresponds to the ascii value 'D'), the arduino is supposed to detect a D with its program and then spit out the values it contains, which the computer then reads and prints to the screen. It reliably detects the D within the arduino IDE, but it doesn't seem to here.

System.out.println("Now sending out the letter 'D' to dump its values");
                        CommPortIdentifier portId;
                        try {
                              portId = CommPortIdentifier.getPortIdentifier("COM4");
                              SerialPort port;
                              try {
                                    port = (SerialPort)portId.open("serial madness", 4000);
                                    try {
                                          input = port.getInputStream();
                                          output = port.getOutputStream();
                                          try {
                                                port.setSerialPortParams(
                                                            9600,
                                                            SerialPort.DATABITS_8,
                                                            SerialPort.STOPBITS_1,
                                                            SerialPort.PARITY_NONE);
                                                output.write(68);
                                                //output.flush();
                                                while(true){
                                                      while (input.available()>0)
                                                            System.out.print((char)(input.read()));
                                                }

                                          } catch (UnsupportedCommOperationException e) {
                                                // TODO Auto-generated catch block
                                                e.printStackTrace();
                                          }
                                    
                                    } catch (IOException e) {
                                          // TODO Auto-generated catch block
                                          e.printStackTrace();
                                    }
                                    
                              } catch (PortInUseException e1) {
                                    // TODO Auto-generated catch block
                                    e1.printStackTrace();
                              }
                        } catch (NoSuchPortException e1) {
                              // TODO Auto-generated catch block
                              e1.printStackTrace();
                        }

I wrote to the program zillions of times to see if “flooding it” would cause it to respond and, what do you know, it did. It didn’t respond to all of it, but it responded to at least some. I then wondered… why wouldn’t it respond?

I thought maybe there was some “set up” time that the program went through before acception serial communication, so I put a 5 second wait in between establishing the Serial Port connection and writing to the arduino and lo and behold, it responded correctly.