Troubles in Serial reading singed multiple digits

Hello all!

I'm making a project that connects arduino Due to Beaglebone black directly. There are no problems when sending single characters. However, I need to send signed integers with multiple digits (-315 for example) from the Beaglebone to Arduino Due. I've found some code online and modified it a little bit to adjust it to the project purpose but when printing it to the Serial monitor I'm receiving different values. For example sending a value of -13 from the beaglebone side, I'm getting it like (-13 0 0 1 3 0 0) in a periodically behaviour. The code I'm using is:

// Example of receiving numbers by Serial
// Author: Nick Gammon
// Date: 31 March 2012

const char startOfNumberDelimiter = '<';
const char endOfNumberDelimiter   = '>';

void setup ()
  { 
  Serial.begin (9600);
  Serial2.begin (9600);
  Serial.println ("Starting ...");
  Serial2.flush();
  Serial.flush();
  } // end of setup
  

  
long processInput ()
  {
  static long receivedNumber = 0;
  static boolean negative = false;
  
  byte c = Serial2.read ();
  long result;

  
  switch (c)
    {
      
    case endOfNumberDelimiter:  
      if (negative){ 
       // processNumber (- receivedNumber); 
       receivedNumber *= -1;
      }
      else{
      //  processNumber (receivedNumber); 
      receivedNumber *= 1;
      }
      break;
    // fall through to start a new number
    case startOfNumberDelimiter: 
      receivedNumber = 0; 
      negative = false;
      break;
      
    case '0' ... '9': 
      receivedNumber *= 10;
      receivedNumber += c - '0';
      break;
      
    case '-':
      negative = true;
      break;
      
    } // end of switch  
    result = receivedNumber;
    return result;
  }  // end of processInput
  
void loop ()
  {
  while(Serial2.available()){
    delay(5);
  
  if (Serial2.available() >0 ){
    delay(40);
    Serial.println(processInput());
   
  }
  }
  // do other stuff here
  } // end of loop

Would anybody help me with this problem!?

  Serial2.flush();
  Serial.flush();

You don't know what flush() does, do you? Why are you calling methods that you do not understand?

long processInput ()
  {
  static long receivedNumber = 0;
  static boolean negative = false;
 
  byte c = Serial2.read ();
  long result;

Read even if there is nothing to read. Hmmm...

How many characters are you reading, each time processInput is called?

Would anybody help me with this problem!?

The problem is that the code is doing exactly what you told it to do. Do you need help with that?

You really need to make processInput() return true (the end of the packet arrived) or false, and return the value as a reference argument or put the value in a global variable.

Also, you might want to read about Serial.available() as well as Robin's Serial Input Basic that's part of the tutorials in the first post at the top of this Forum.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example.

...R