Serial.Write/Print on COM

Hi guys,
so I'm making a communication between Arduino and JavaFX app. I'm having trouble with serial input on javafx app. Can't read real data sent.There are some fixed numbers on read like 52,57,10,13 etc. Serial output from javafx to arduino was not a problem, but can't make input. So here is my code from arduino with temp sensor DHT11, serial read and on action 1, serial output.

#include <DHT.h>

#define DHTPIN 2
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);
char c = -1;

void setup() {
  Serial.begin(115200);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  dht.begin();
}

void loop() {
  while (Serial.available()) {
    c = Serial.read();
    Serial.println(c, DEC);

    if (c == '0') {

      digitalWrite(13, LOW);
      Serial.write('0');
      delay(50);
    }

    if (c == '1') {

      digitalWrite(13, HIGH);
      int ocitanjeTemp = dht.readTemperature();
      Serial.println(ocitanjeTemp);
      Serial.write(0);
      delay(500);
    }
  }

  delay(10);
}

this is javaFX code:

package sample;

import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.InputStream;
import java.io.OutputStream;

public class comm {
    private static final String PORT_NAMES[] = {
            "/dev/tty.usbmodem", // Mac OS X
            "/dev/usbdev", // Linux
            "/dev/tty", // Linux
            "/dev/serial", // Linux
            "COM3", // Windows
    };

    protected SerialPort serialPort;
    public static OutputStream serialOut;
    public static InputStream serialIn;

    public comm() throws Exception {
        CommPortIdentifier port = CommPortIdentifier.getPortIdentifier("COM3");
        CommPort commPort = port.open(null, 2000); //this.getClass().getName()
        serialPort = (SerialPort) commPort;
        serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        serialOut = serialPort.getOutputStream();
        serialIn = serialPort.getInputStream();
        serialPort.notifyOnDataAvailable(true);
    }




//here is a function for reading temp from serial port --------------------------

    public void ocitanjeTemp(/*nesto*/) throws Exception{
        int ocitanje = 0;

        try {

            ocitanje= serialIn.read();
            System.out.println("Ocitanje Serial inputa ="+ ocitanje);
        } catch (Exception e){
            System.out.println("Nije uspio ocitati temp.");
        }
    }

You are receiving ASCII text. 52, 57 10, 13 is "49\r\n" in ASCII. That is what you should expect it you typed '1' then Enter on the PC, because 49 is the ASCII code for the character '1'...

Regards,
Ray L.

Problem is a function for serial write/read on port, I think.
Arduino should sending room temp, which is 24 C.
I don' know how to get that, I'm trying with all kinds of var types.

Kapusta:
Problem is a function for serial write/read on port, I think.
Arduino should sending room temp, which is 24 C.
I don' know how to get that, I'm trying with all kinds of var types.

Why not consult the DHT documentation and/or examples, instead of guessing?

Does the JSSC library work with JavaFX ?

If so, it may be worth trying. The Arduino IDE uses it.

...R

aarg:
Why not consult the DHT documentation and/or examples, instead of guessing?

I'm not guessing, it's float. I know that. But it can be transform into int or char as well. Can't send float to port.

 float readTemperature(bool S=false, bool force=false);

Robin2, i didn't try with that. thanks for advice.
I note that, serialOut is working well. (from javafx to arduino, sending ones and zeros in char)

You ARE reading the data being sent by the Arduino, and what you are receiving is absolutely correct, given how you're sending it. You are sending a '1', followed by carriage return, then line feed. The Arduino is receiving all three characters correctly, then converting them to decimal (you are doing Serial.println(c, DEC)). As ASCII '1' character is decimal 49, so the Arduino sends a '4' (decimal 52) followed by a '9' (decimal 57) followed by a line feed (decimal 10), and finally a carriage return (decimal 13). This is EXACTLY what your code is written to do. If that's not what you want, then you need to modify your code. If you want to simply return exactly the same characters you received, then get rid of the "DEC", and simply use "Serial.print(c);".

Regards,
Ray L.

You are right, I didn't see that serial.print in serial available
Didn't know the right difference between serial.print and serial.write.
Thank you all for your advices and time

The problem was like RayLivingston did describe it, It was reading 1 and sending it right back into javaFX

Kapusta:
Can't send float to port.

Why not?

It says: "call of overloaded 'write(float&)' is ambiguous"
So I assume it can't send more than 2 bytes at once

Kapusta:
It says: "call of overloaded 'write(float&)' is ambiguous"
So I assume it can't send more than 2 bytes at once

Strange. I don't see any place in your code where you send the temperature using write(). I only see this:

      int ocitanjeTemp = dht.readTemperature();
      Serial.println(ocitanjeTemp);

That is what I was talking about. println() will definitely accept a float.

Kapusta:
It says: "call of overloaded 'write(float&)' is ambiguous"
So I assume it can't send more than 2 bytes at once

That error message has nothing whatsoever to do with how much data you can send. There is more than one function named "write" which takes a single float as an argument, and the compiler cannot figure out which one to use. That is a problem in your program, or a library.

Regards,
Ray L.

RayLivingston:
That error message has nothing whatsoever to do with how much data you can send. There is more than one function named "write" which takes a single float as an argument, and the compiler cannot figure out which one to use. That is a problem in your program, or a library.

Regards,
Ray L.

There are no versions of write that take a float as an argument. print and println can take a float as an argument and send it out as ascii characters. To use write to send a float as raw binary you have to cast it to an array of 4 bytes.

Delta_G:
There are no versions of write that take a float as an argument. print and println can take a float as an argument and send it out as ascii characters. To use write to send a float as raw binary you have to cast it to an array of 4 bytes.

Ah! So, instead, the compiler cannot figure out which of the OTHER writes to use, so the error is telling the user "cast it to something I understand".

The point is, it is a programming problem, and has nothing to do with how much data can be sent.

Regards,
Ray L.

RayLivingston:
Ah! So, instead, the compiler cannot figure out which of the OTHER writes to use, so the error is telling the user "cast it to something I understand".

The point is, it is a programming problem, and has nothing to do with how much data can be sent.

Regards,
Ray L.

Right. It's saying you're calling an overload of the write function that it can't find.

Thank you for your knowledge and time.I'm in learning progress, so I need help from someone like you guys. Thanks.