UDP to RS485 Bridge

I have an otherwise functional Opta program, and an otherwise functional RS485 network, however When I add the following code to Loop() it causes the red light to flash (indicating an RTOS problem?) LEDR is not referenced anywhere else in the program

if i just place the RS485 in to UDP out it works as expected, the problem seems to be with the transmit side... however if i just use the transmit code it doesnt cause the red light to blink but also makes no attempt to transmit on the port

any help welcome

int p = modbusUDP.parsePacket();
  if (p > 0)
  {
    RS485.beginTransmission();
    for (int x = 0; x < p; x++) RS485.write(modbusUDP.read());
    RS485.endTransmission();
    RS485.receive();
  }
  delay(50);
  p = RS485.available();
  if (p > 0)
  {
    modbusUDP.beginPacket(MBip ,81);
    for (int x = 0; x < p; x++) modbusUDP.write(RS485.read());
    modbusUDP.endPacket();
  }