Pages: [1]   Go Down
Author Topic: Serial event  (Read 586 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Dear readers,

My question is probably silly, since I must be missing something, but here goes...

I am using Serial event to receive data on Arduino Uno which I send from my PC (please see the attached image).

The procedure is:

I use floating point numbers (0.1 precision), which I multiply by 10 and cast them to 16-bit value. I split the value to 2x8 bits, which I send over the serial port. The Arduino code is:

Code:
byte received_data[4];
boolean received_all_data = false;
float Tlimit_down = 0.0;
float Tlimit_up = 0.0;

void setup() {
  Serial.begin(115200);
}

void loop() {
  if(received_all_data) {
  Serial.println(Tlimit_down);
  Serial.println(Tlimit_up);
  received_all_data = false;
  }
}

void serialEvent() {
  while(Serial.available() < 4){}
   for(int i=0;i<4;i++) {
     received_data[i] = Serial.read();       
   }
  Tlimit_down = (float)((received_data[0] << 8) + (received_data[1]))/(10.0);
  Tlimit_up = (float)((received_data[2] << 8) + (received_data[3]))/(10.0);
  received_all_data = true;   
}

After the data is read in full, I return it to the serial port and read it on my PC (see the read buffer box on the attached image).

Since the Tlimit_up and Tlimit_down are global variables, I change them every time I send new data to the serial port form my PC, right?
So if I put another Serial.println as follows:

Code:
void loop() {
  if(received_all_data) {
  Serial.println(Tlimit_down);
  Serial.println(Tlimit_up);
  received_all_data = false;
  }
[b]Serial.println(Tlimit_up);[/b]
}

And after closing the serial port with the PC and opening the Serial monitor in Arduino, the output should have the last value I've sent through the serial port, right? Even though I closed the serial port. But the output is always 0.00! Could please someone help me with this? Is this happening because I close the serial port? Because in the read buffer (attached image) the values seem to be correct...

Thank you and best regards,
K


* PC_serial.jpg (14.29 KB, 292x195 - viewed 18 times.)
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
And after closing the serial port with the PC and opening the Serial monitor in Arduino, the output should have the last value I've sent through the serial port, right?

Unless modified to defeat the reset, opening and closing the serial port on the pc will cause the arduino to reset. This may cause the loss of variables from the previous session.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you for the explanation!

Regards,
K
Logged

Pages: [1]   Go Up
Jump to: