Why Serial1.available() always true

hello,
i am working with mega2560. in my program, i want to read data from an angle sensor which is connected to Serial 1 and monitor what have been received by printing it to Serial3. i get strange result. i tried to find why and, step by step, i found the Serail3.available() always true without a reason even i left the TX pin (pin 19) desconnected.
is there any body can help to figure out why. appreciated!

Please post the sketch that exhibits the problem and a schematic of your project. A 'photo of a hand drawn circuit is good enough

Please control your what you wrote. Pin 19 is RX1 not TX3 and available() is related to RX (input), not TX (output).

/////////////////////////////////////// read levelers ///////////////////////////

//read leveler 1
//sent 03 cmd
byte bytes[] = { 0x01,0x03,0x00,0x66,0x00,0x02,0x24,0x14 };
digitalWrite(MAX485_LEVEL_DE_RE, 1);
Serial1.write(bytes, 8);
Serial1.flush();
digitalWrite(MAX485_LEVEL_DE_RE, 0);
delay(50);

bool dataReceived = 0;
//receive data from Serial1
while (Serial1.available()) {
	uart_rec_buf[uart_rec_buf_index] = Serial1.read();
	uart_rec_buf_index++;
	dataReceived = 1;
}

if (dataReceived) {
	//debug message
	digitalWrite(MAX485_DEBUG_DE_RE, 1);
	Serial3.print("data received:");
	Serial3.print(uart_rec_buf[0], HEX);
	Serial3.print(uart_rec_buf[1], HEX);
	Serial3.print(uart_rec_buf[2], HEX);
	Serial3.print(uart_rec_buf[3], HEX);
	Serial3.print(uart_rec_buf[4], HEX);
	Serial3.print(uart_rec_buf[5], HEX);
	Serial3.print(uart_rec_buf[6], HEX);
	Serial3.print(uart_rec_buf[7], HEX);
	Serial3.print(uart_rec_buf[8], HEX);
	Serial3.print(uart_rec_buf[9], HEX);
	Serial3.println("");
	Serial3.flush();
	digitalWrite(MAX485_DEBUG_DE_RE, 0);

	if (uart_rec_buf_index == 9) {
		//Serial.print("crc=");
		//Serial.println(crc16(uart_rec_buf, 7), HEX);
		uint16_t crcCalculated = crc16(uart_rec_buf, 7);
		uint16_t crcReceived = uart_rec_buf[7];
		crcReceived = ((crcReceived << 8)) | (uart_rec_buf[8]);

		if (crcCalculated == crcReceived) {
			if (uart_rec_buf[1] == 03) {
				xAngle1 |= (uart_rec_buf[3] & 0x00ff);
				xAngle1 = (xAngle1 << 8) | (uart_rec_buf[4]);
				xAngle1 = (xAngle1 << 8) | (uart_rec_buf[5]);
				xAngle1 = (xAngle1 << 8) | (uart_rec_buf[6]);
				xAngle1_f = xAngle1 * 0.001;
				dtostrf(xAngle1_f, 2, 3, xAngle1_c);

				digitalWrite(MAX485_DEBUG_DE_RE, 1);
				Serial3.print("xAngle1 = ");
				Serial3.println(xAngle1_f, 3);
				Serial3.flush();
				digitalWrite(MAX485_DEBUG_DE_RE, 0);
			}//if (uart_rec_buf[1] == 03)
			else {
				digitalWrite(MAX485_DEBUG_DE_RE, 1);
				Serial3.println("[err]: not 03 cmd received !");
				Serial3.flush();
				digitalWrite(MAX485_DEBUG_DE_RE, 0);
			}
		}//if (crcCalculated == crcReceived) 
		else {
			digitalWrite(MAX485_DEBUG_DE_RE, 1);
			Serial3.println("[err]: crc failed !");
			Serial3.print("[err]: crc calculated = ");
			Serial3.println(crcCalculated);
			Serial3.print("[err]: crc received = ");
			Serial3.println(crcReceived);
			Serial3.flush();
			digitalWrite(MAX485_DEBUG_DE_RE, 0);
		}
	}//if (uart_rec_buf_index == 9) 
	else {
		digitalWrite(MAX485_DEBUG_DE_RE, 1);
		Serial3.println("[err]: data len wrong !");
		Serial3.print("[err]: data len = ");
		Serial3.println(uart_rec_buf_index);
		Serial3.flush();
		digitalWrite(MAX485_DEBUG_DE_RE, 0);
	}
	dataReceived = 0;
}

uart_rec_buf_index = 0;
uart_rec_buf[10] = { 0 };

/////////////////////////////////////// read levelers end ///////////////////////////

Serial1.available should return the RX buffer size.

hint 1: don't check for Serial1.available() but for Serial1.available()>0
hint 2: check what you really get from Serial1.available
int result = Serial1.available();
Serial.print(F("Result=")); Serial.println(result);
if (result > 0)
{
//do something

Spot the difference ?

Things will be quite different when you use the correct pins.

yes, my typo mistake, i left Pin 19 (RX1) disconnected, however the Serial1.available() returns true (not zero).

well, i corrected .availalbe() to be (.availalbe()>) and is trying to run now
let's see

everything other than Zero is true.
You don't get true or false but an INTEGER.
so print out what you get to be able to do more analysis.
After the first read the number should differ...

Do not leave input pins open. Open pins can receive ambient noise.

If you only receive from your sensor and only write to your PC then you can leave the RS485 drivers in that (input or output) mode.

1 Like

Why ?

1 Like

because the return value is not a boolean but an integer described as:
The number of bytes available to read.

Using the API as described might help the analysis of his problem.

That's not a special requirement. Input can be read while available() is not zero (false). The value of available() is important only if one waits for a certain number of characters received before processing the input.

Here it's okay to read expected input from the sensor on Serial1. The confusion results from

where Serial3 is connected to the PC. The presented code never checks Serial3.available(). And if true it indicates that the PC has sent something.

I was being a little provocative when asking why, and I guessed what your answer might be, and what you say is true. However, in practice the test works and will continue to work unless some very unlikely changes are made to the C/C++ language

A much better reason for using if (Serial1.available() > 0) is that it is much easier to understand what is going on when reading the code

1 Like

A change to the API is more probable but still unlikely. If the result
type is a signed integer (I didn't find it in the documentation), a negative value might indicate EOF or the like.

P.S.: Stream.available() is documented as int thus signed.

Dear DrDiettrich,
my typo fault, i receive data from Serial1 and print to Serial3. unfortunately, my Serial1.availabe always return something un-expected. "FF" is the most case

BTW, do i need to apply vcc to MAX 485 module.

i tried also to connect +5v and GND to the module. but the problem is still there.
a couple of days ago, i worked with this module without vcc applied, it worked. that was why came my question.



the MAX485 module i hired