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