Serial.read is -1?

Testing my program by connecting two Arduino`s together via serial, Tx - Rx

Program starts with no connection and waits for data, serial is connected, data is read, but a string of -1 is added to it.

If disconnect the serial connection, serial is still read, but with all -ones.

I would have thought that with no data input, the program would wait?

Here`s part of receive test code

void loop() {
  if (Serial.available() > 0 ) {
   while (inByte != 0xFF) 
   {
         inByte = Serial.read();
         counter++;
        Serial.print(inByte);
        Serial.print("");
   }
   Serial.println("");
   Serial.println("");   
  Serial.print("Counter is ");
 Serial.println(counter); 
   Serial.println("Run-in Ended");
   Serial.println("");
    counter = 0;
    
    inByte = Serial.read(); 
    
  } //SerialEnd
} //LoopEnd

Output on Serial Monitor

10-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1-1858585858585858585858585858585858585858585858585858585858585255

Counter is 60
Run-in Ended

Thanks.

Your second read doesn't check to see if there's any data there.

Returns

the first byte of incoming serial data available (or -1 if no data is available) - int

We can't see the declaraction of "inByte" (Big Hinttm), but if it is an "int", it unlikely to ever be equal to 0xFF

Ah... :slight_smile: Where did I get the idea that an int was 0-255?? :smiley:

Changed int to byte... upwards and onwards! :slight_smile: