2 bytes via serial

Hi,

I am beginer in programming, and I need some help to read 2 bytes (msb/lsb) that comes after a request (0x01 to msb and 0x02 to lsb) via serial, and then, make an mathematical operation and display on an 2x16 display. I have the functions of my project that use only 1 byte working good. One example:

void funcao4()
{
int MAP;
float MAP1;
delay(600);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("MAP[mmHG]");

Serial.write(0x06); //request

if (Serial.available() > 0)
{

MAP = Serial.read() ; //read
MAP1 = (MAP * 2.8759 + 91); //operation

lcd.setCursor(0,1);
lcd.print(MAP1); //display

}
}

regards.

Read the how to use this forum sticky and please post your code correctly.

After the request you need to wait until there are two bytes in the buffer

if (Serial.available() > 1)

And then read them

 MAP = Serial.read() ;   //read most significant byte
  MAP = (MAP << 8 ) + Serial.read() ;   //read least significant byte

Are you sure you are receiving bytes and not ASCII digits? That method only works for raw bytes.

I don't know the answer you ask, but if you use "Tools - Auto format" on your code, some smarter than I, may be able to understand and help you easier.

 Serial.write(0x06);  //request
 
    if (Serial.available() > 0)

The chances that the data you requested are available to read in such a short time are very slim indeed.
You probably need a loop to wait for them to come back.

Are you sure you are receiving bytes and not ASCII digits? That method only works for raw bytes.

Yes i am receiving bytes, this code works (only 1 byte). My problem is 2 bytes, how can I make the operation?

[/quote]

My problem is 2 bytes, how can I make the operation?

I thought I told you?

Grumpy_Mike:

My problem is 2 bytes, how can I make the operation?

I thought I told you?

I m telling about this operation, now with 2 bytes : MAP1 = (MAP * 2.8759 + 91)

There is no need to change that as the variable MAP will be a two byte variable.
Unless you are not telling us something. What does that formular do anyway?

The chances that the data you requested are available to read in such a short time are very slim indeed.
You probably need a loop to wait for them to come back.

Alternatively, you can move the delay(600) ;
or simply exchange the two

 void funcao4()
 { 
    int MAP;
    float MAP1;

    Serial.write(0x06);  //request

    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("MAP[mmHG]");

    delay(600);
    
    if (Serial.available() >= 2) 
    {
    MAP = Serial.read() << 8; // msb
    MAP |= Serial.read();  // lsb
    // ....
 }

My friends, I think I didn´t explain very well my problem. The code (function) that I have beed posted, is working, what doesn t works are other similar functions that use 2 bytes instead 1 byte and needs 2 requests, 1 for msb and 1 for lsb (0x01 and 0x02 for example), I am trying to use your suggestions but the function doens t work anyway :. The porpouse is read values from a car ecu. The mathematical operation is the transfer function.

The code that doesn t works:

void funcao1()

{

  int RPM;
  int RPM1;
  float RPM2;
  
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("RPM");

  delay(400);
  
  Serial.write(0x01);

  if (Serial.available() > 0) 
  {
    RPM = Serial.read() ; //msb
  }

  Serial.write(0x02);

  if (Serial.available() > 0) 
  {
    RPM1 = (RPM << 8) + Serial.read() ; //msblsb
  }

  RPM2 = (30000000 / RPM1) ;

  lcd.setCursor(0,1);
  lcd.print(RPM2); 

}

The smiley face is probably messing with you. 8)

zoomkat:
The smiley face is probably messing with you. 8)

ahahah ok you're right, I've eliminated the dude

Just think what happens if after you send the request you have not got an answer.
The if statement does not trigger and you read nothing.
You have to wait for the data to arrive. Use a while loop that only terminates when there is a byte to read, that is until serial avaliable gives you a non zero value.