Skip to main content
added 153 characters in body
Source Link
Delta_G
  • 3.4k
  • 2
  • 13
  • 24

Yes, if you send 5 bytes to the Arduino then they will arrive one at a time. When the 4th byte arrives then the if (Serial.available() == 4 ) statement is true and it runs that code. That code reads from the buffer which means that the part that checks for 5 bytes will never be able to run. There will always be 4 before there is 5.

A better idea would be to send your bytes with a start and end marker. The program on the Arduino will read bytes and collect them in an array until it sees the end marker. Then you can count how many bytes you have received if you want to take different actions depending on that number.

Here's a nice thread on handling Serial data: https://forum.arduino.cc/t/serial-input-basics-updated/382007

Yes, if you send 5 bytes to the Arduino then they will arrive one at a time. When the 4th byte arrives then the if (Serial.available() == 4 ) statement is true and it runs that code.

A better idea would be to send your bytes with a start and end marker. The program on the Arduino will read bytes and collect them in an array until it sees the end marker. Then you can count how many bytes you have received if you want to take different actions depending on that number.

Here's a nice thread on handling Serial data: https://forum.arduino.cc/t/serial-input-basics-updated/382007

Yes, if you send 5 bytes to the Arduino then they will arrive one at a time. When the 4th byte arrives then the if (Serial.available() == 4 ) statement is true and it runs that code. That code reads from the buffer which means that the part that checks for 5 bytes will never be able to run. There will always be 4 before there is 5.

A better idea would be to send your bytes with a start and end marker. The program on the Arduino will read bytes and collect them in an array until it sees the end marker. Then you can count how many bytes you have received if you want to take different actions depending on that number.

Here's a nice thread on handling Serial data: https://forum.arduino.cc/t/serial-input-basics-updated/382007

Source Link
Delta_G
  • 3.4k
  • 2
  • 13
  • 24

Yes, if you send 5 bytes to the Arduino then they will arrive one at a time. When the 4th byte arrives then the if (Serial.available() == 4 ) statement is true and it runs that code.

A better idea would be to send your bytes with a start and end marker. The program on the Arduino will read bytes and collect them in an array until it sees the end marker. Then you can count how many bytes you have received if you want to take different actions depending on that number.

Here's a nice thread on handling Serial data: https://forum.arduino.cc/t/serial-input-basics-updated/382007