Serial.read() always returns 0 even when nothing is send to COM serial port

I send a variable using a programming language to COM serial port. I have a condition if(incomingByte != 0) that is satisfied if I send something to the serial port using the mentioned PL. However, due to serial ports concurrency, I cannot see what's received at that exact moment.

However, when I don't send anything, I open Serial Monitor and output the read variable thanks to command Serial.read(), I always receive 0.

From the documentation, we can read that serial read returns "-1 if no data is available".

I attach a picture.

Any help will be much appreciated.

Post your code properly inline. Posting a picture of text is plain silly.

Steve

int incomingByte = 0; // for incoming serial data

void setup() {
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
   pinMode(13, OUTPUT);
 
}

void loop() {
    int inverse;
  // send data only when you receive data:
  if (Serial.available() > 0) {
   char incomingByte = (char)Serial.read();
//     Serial.write(Serial.read());
//    inverse = incomingByte;

  }
    
  if(incomingByte == '1'){
    digitalWrite(13, HIGH);
    
  }
   else if(incomingByte == '0'){
       digitalWrite(13, LOW);
   }
    // say what you got:
    Serial.print("I received: ");
    Serial.println((char)incomingByte, HEX);
      
        
  }

Here you declare a global variable named incomingByte:

int incomingByte = 0; // for incoming serial data

Here you declare a variable local to the if block named incomingByte:

   char incomingByte = (char)Serial.read();

that variable goes out of scope at the end of the if block.

Here you print the value of the global variable named incomingByte:

    Serial.println((char)incomingByte, HEX);

The Arduino language allows you to declare multiple variables of the same name at different scopes. This is called "variable shadowing", and is the cause of a lot of confusion.

You need to understand the difference between a variable declaration:

char incomingByte = (char)Serial.read();

and a variable definition:

incomingByte = (char)Serial.read();

A declaration creates a new variable. A definition assigns a value to an existing variable.

pert:
You need to understand the difference between a variable declaration:

char incomingByte = (char)Serial.read();

and a variable definition:

incomingByte = (char)Serial.read();

A declaration creates a new variable. A definition assigns a value to an existing variable.

The second one is an assignment, not a definition. The first is a definition (which is also a declaration) and initialization.

Hi,

Thanks all for your replies,

I have modified my code to the following:

int incomingByte; // for incoming serial data

void setup() {
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
   pinMode(13, OUTPUT);
 
}

void loop() {
  
  if (Serial.available() > 0)
    incomingByte = Serial.read();
    
  if(incomingByte != 0){  
  digitalWrite(13, HIGH);
 
  }
    
     else if(incomingByte == 0){
       digitalWrite(13, LOW);
       delay(1000);
       digitalWrite(13, HIGH);
   }

       Serial.println(incomingByte, DEC);
        
  }

If I send anything from the PL to the Arduino through COM (32-bit LongInt), the condition if(incomingByte != 0) is satisfied and the led lights. When I open Serial Monitor in Arduino IDE, the second condition else if is satisfied and led starts blinking (even though I am not anymore sending anything, I receive tons of 0's, while the expected result should be -1).

P.S.:

*** The real problem behind this that I am trying to solve is reading correctly a 32-bit Long Integer that I send from the PL. The only thing Arduino understands is !=0, and I would like it to recognize the 0 as well, so I can turn off the Led. As I can't see the values I receive, I can really debug the code, or even know the origin of the problem. I know that INT in Arduino is only 16 bits and that encoding of the data types must not necessarily coincide. How should I refine the variable in the PL before sending it to Arduino, so it is interpreted correctly?? ***

What the Hell is the "PL"?

Two thirds of a Programmable Logic Controller?

The initial problem was solved; the loop was not right.

The second problem is still unsolved.

Hello mahdibk,

[quote author=mahdibk link=msg=4467875 date=1581067152]   
     else if(incomingByte == 0){
 
  }

mahdibk:
When I open Serial Monitor in Arduino IDE, the second condition else if is satisfied and led starts blinking (even though I am not anymore sending anything, I receive tons of 0's, while the expected result should be -1).

When you open Serial Monitor the Arduino board is reset.
As incomingByte is then reinitialized to 0, the second condition else if(incomingByte == 0){ is satisfied.

Regards,
bidouilleelec

christop:
The second one is an assignment, not a definition. The first is a definition (which is also a declaration) and initialization.

Thanks for the correction @cristop!

mahdibk:
If I send anything from the PL to the Arduino through COM (32-bit LongInt), the condition if(incomingByte != 0) is satisfied and the led lights. When I open Serial Monitor in Arduino IDE, the second condition else if is satisfied and led starts blinking (even though I am not anymore sending anything, I receive tons of 0's, while the expected result should be -1).

You are not receiving tons of 0's. You are not receiving anything. Since you are not receiving anything, the global variable 'incominByte' is not being changed from its default value: 0. Every time you look the value it is still 0. Did you only want to process each incoming byte once? If so:

void loop()
{
  if (Serial.available() > 0)
  {
    incomingByte = Serial.read();


    if (incomingByte != 0)
    {
      digitalWrite(13, HIGH);
    }
    else // if (incomingByte == 0)
    {
      digitalWrite(13, LOW);
      delay(1000);
      digitalWrite(13, HIGH);
    }


    Serial.println(incomingByte, DEC);
  }
}
1 Like