Serial within function

Hello,
I have a program that reads some data from a machine trough serial RS232.
Everything works as intended, reading is ok if done in main loop, but in my program i have more cases when i need to read from this device, so instead of writing the same code for serial reading multiple times i decided to make a function with it and call it when i need reading.
The problem is when the program try to call the function arduino resets (mega 2560).

The function i made for reading is:

void ReadDX(){
  Serial3.write(0x48);
  Serial3.write(0x42);
  Serial3.write(0x01);
  Serial3.write(0x20);
  Serial3.write(0x01);
  Serial3.write(0x20);
  unsigned long starttime;
  starttime = millis();
  byte in_bytes[26];
  byte fl;
  while ( (Serial3.available()<27) && ((millis() - starttime) < 1000) )
  {      
  }
  if (Serial3.available() != 27) {
     starttime = millis();
     while (Serial3.available()>0 && ((millis() - starttime) < 1000)){
      fl = Serial3.read();
     }
  } else {
    for(int n=0; n<27; n++) {
      in_bytes[n] = Serial3.read();
    }
   fpwr = (float)(in_bytes[9]*256 + in_bytes[10])*20/3500;
   rflpwr = (float)(in_bytes[11]*256 + in_bytes[12])*20/3500;
   supcurr = (float)(in_bytes[13]*256 + in_bytes[14])*250/3500;
   supvolt = (float)(in_bytes[15]*256 + in_bytes[16])*300/3500;
   rfdrive = (float)(in_bytes[17]*256 + in_bytes[18])*10/3500;
   bpvswr = (float)(in_bytes[19]*256 + in_bytes[20])*10/3500;
   antvswr = (float)(in_bytes[21]*256 + in_bytes[22])*10/3500;
  }
}

… and i call it with: ReadDX();

Even if i put ReadDX(); the first thing in the main loop, the arduino restarts when calls.
If i copy the code from the function and put it directly in loop, everything works as intended.
The result variables are global, and the function should update them.

What can be the problem!?
Thanks.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.

The problem is you have declared in_bytes as a 26 byte array but then you are writing off the end of the array here:

    for (int n = 0; n < 27; n++) {
      in_bytes[n] = Serial3.read();
    }

Because the array is zero indexed the end of the array is at in_bytes[25].

Writing off the end of an array can cause different behavior from case to case, thus your different results with the code in loop() but it’s never a good idea.

A nifty trick for automatically determining the number of elements in an array is this:

sizeof(in_bytes) / sizeof(in_bytes[0])

In this case you could just do sizeof(in_bytes) because a byte is one byte but that wouldn’t work with a different type, thus the need for determining the size of an array element.

Indeed, increasing in_bytes[26]; to in_bytes[27]; fixed the problem,
I confused declaring in_bytes[26] to be from in_bytes[0] to in_bytes[26] -> 27 bytes.

Thank you for your support.