I have a problem with RXTX library and Arduino, I'm trying to store a variable obtained from Arduino serial output into a variable that it's declared in my Java code and then use it to execute some actions. This is a part of code that I use to read the serial output from Arduino.
Now the question is, how can I store that data starting from this code?
I need to store data obtained from an ultrasonic sensor and let it show to my Java GUI.
public void serialEvent(SerialPortEvent evt) {
if (evt.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
byte singleData = (byte)input.read();
if (singleData != NEW_LINE_ASCII) {
logText = new String(new byte[] { singleData });
finestra.logArea.append(logText);
} else {
finestra.logArea.append("\n");
}
}
catch (Exception e) {
logText = "Failed to read data. (" + e.toString() + ")";
finestra.logArea.setForeground(Color.red);
finestra.logArea.append(logText + "\n");
}
}
This part of code is just to read all that came from the serial.
Why don't you try to use Javino middleware instead of RxTx?
Your Java code will be like that:
Javino j = new Javino();
String receivedMessage = new String();
if (j.requestData("COM3","hi"))
receivedMessage = j.getData();
And your Arduino code (inside main loop):
if(javino.availablemsg()){
msg = javino.getmsg();
}
if (msg=="hi) {
javino.sendmsg("Your Value");
}
msg = "";
And so it is.