Go Down

Topic: Serial event (Read 718 times) previous topic - next topic

Chimera

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: [Select]
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: [Select]
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

zoomkat

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.
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Chimera

Thank you for the explanation!

Regards,
K

Go Up