Giving wrong value while reading data from serial port

processing code:-

import processing.serial.*;
Serial myport;

float v1,v2,a,cda=0.0,how=0.0,water=0.0;
void setup()
{
  myport = new Serial(this, Serial.list()[0], 9600);
}



void draw()
{
  if(0<myport.available())
  {
    cda=myport.read();
    how=myport.read();
    water=myport.read();
     println(cda);
     println(how);
     println(water);
  }
  
}

Output:-

49.0
57.0
46.0
48.0
53.0
13.0
10.0
55.0
50.0
46.0
56.0
50.0
13.0
10.0
-1.0
49.0
57.0
46.0
48.0
53.0
13.0
10.0
55.0
50.0
46.0
56.0
50.0
13.0
10.0
-1.0
49.0
57.0
46.0
48.0
53.0
13.0
10.0
55.0
50.0
46.0
56.0
50.0
13.0
10.0
-1.0
.
.
.

Serial port output:-

19.08
72.85
19.08
72.85
19.08
72.85
19.08
72.85
19.08
72.85
19.08
72.85
19.08
72.85
.
.
.

1 Like

•Please format your code

The output is perfectly fine and exactly what your Code should do. You might want to take a look at the Serial read() function.

Then you can see that read() gives back an int between 0 and 255 representing the byte being sent over. You are sending 19.08CRLN72.85CRLN, and what you get is 4957464853131055504656501310-1 where :

'1' = 49;
'9' = 57;
'.' = 46;
'0' = 48;
'5' = 53; //apparently it‘s 19.05, not 19.08
Carriage Return = 13;
Line Feed = 10;

'7' = 55;
'2' = 50;
'.' = 46;
'8' = 56;
'2' = 50; //apparently it’s 72.82
CR = 13;
LF = 10;

1 Like

so what should I write instead of serial.read() to get the input right?

You should use readStringUntil(10); , to read the String until it reaches the LineFeed (or maybe CR, but that‘s up to you).

Then just use float(String) to convert it to float.

1 Like