Serial limited to 4 bytes ???

greetings,
I want to make a program that convert the numbers I sent via serial monitor into integers, this is my code :

void setup()
{
  Serial.begin(9600);
}

void serReadInt()
{
 int i, r, x, y, z, serAva;                          
 char inputBytes [5];                
 char * inputBytesPtr = &inputBytes[0];  
 char inputBytes2 [5];               
 char * inputBytesPtr2 = &inputBytes2[0];  
 char intInit;
 char intData;
 char intData2;
   
 if (Serial.available()>0)            
 {
   delay(5);                              
   
   intInit = Serial.read();   
   if (intInit == '<')
   {
     serAva = Serial.available();
     for (i=0; i<serAva; i++)
     {
      intData = Serial.read();
     if (intData == '>')
      {
        inputBytes[i] =  '\0';
        inputBytes2[r] =  '\0';
        y = atoi(inputBytesPtr);
        z = atoi(inputBytesPtr2);
        Serial.print("y = ");
        Serial.print(y);
        Serial.print("\t z = ");
        Serial.println(z);
      }
     else if (intData == '|')
      {
         x = serAva-i;
         for (r=0; r<x; r++)
         {
          intData2 = Serial.read();
          if (intData2 == '>')
          {
            inputBytes[i] =  '\0';
            inputBytes2[r] =  '\0';
            y = atoi(inputBytesPtr);
            z = atoi(inputBytesPtr2);
            Serial.print("y = ");
            Serial.print(y);
            Serial.print("\t z = ");
            Serial.println(z);
          }
         else
          {
           inputBytes2[r] = intData2;
          } 
         }
      }
     else 
      {
       inputBytes[i] = intData;
      } 
     }
   }
 }
 else
   Serial.println("ZZZZZ");                           
}

void loop()
{
  while(Serial.available()>0)
  {
  serReadInt();
  }
}

I want to make it able to extract two integers in one input, for example, if I input <123|456>, then it means y = 123, and z = 456
the problem is, it only works if the total byte I send is less then 4, it can process <1>, <12>, <123> just right, but it won't process <1234>

I thought the problem lies in the serial.available, because after several trial and code modification, I conclude that the number of available bytes in the buffer was never above 4 bytes.....

can anyone help me ???

thanks,
Heggi

I thought the problem lies in the serial.available, because after several trial and code modification, I conclude that the number of available bytes in the buffer was never above 4 bytes.....

That's most likely because serial data transmission takes time, and you are reading the data faster than it arrives.

Those pointer variables that you have are useless. Get rid of them.

The function atoi() says that it takes a char pointer, but you can call it with an array. Of course, you probably should make the arrays a bit bigger.

Here is some code that will read data starting with < and ending with >, no matter how long it takes to get the >:

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

Where the comment "Process the packet" is, you can use strtok to parse the array, to get the part before the | and the part after it. You can pass the tokens to atoi() to convert the tokens to numbers.

And if a non-digit comes through where there should only be digits then somewhere you get to handle-or-not the error.