Arduino reading a packet of known length over a Serial port

The device accepts Serial API packets of known length (25 bytes).) It is necessary to write the entire packet to the byte array buf_command[25] for further analysis, tried like this

while(Serial.available())
{
    byte inBuf = Serial.read();     //прочитать байт по последовательному порту
    Serial.println();
      if (i_byte<25){           //если байт не последний 25-й
          buf_command[i_byte] = inBuf;   //добавить полученный байт в массив 
          i_byte++;
      }
      else {                              //иначе
            i_byte = 0;                  //обнулить счетчик                
              }
           }

But it turned out some nonsense (only the first byte was normally written to the entire array) How do I make the entire one-time package fit in an array??

Author: Клаус, 2018-05-10

1 answers

I would try to do this:

for (char n = 0; n < 25; ++n)
{
   while( ! Serial.available()) ; // Ждём, данные получим
   buf_command[n] = Serial.read();
}
 0
Author: Vanyamba Electronics, 2018-05-10 16:00:15