SlipSerial and OSC, but generally reading serial across loop()s

Hi,

I created an issue for this on the OSC library page but I think it’s relevant here too: https://github.com/CNMAT/OSC/issues/97

I’m trying to read SLIPSerial from across different loops so I can multitask and do other things (like send responses) but altering the simple receive example (SerialReceive.ino, which works fine) doesn’t work in this case. This produces decoding errors in the OSC bundle and I’m unsure why, since the logic is the same? I don’t see anything special in the SlipSerial code either that would modify the serial port buffer, but I could be missing something.

The code is attached to that issue, but also as follows:

#include <OSCMessage.h>
#include <OSCBundle.h>
#include <OSCBoards.h>

#ifdef BOARD_HAS_USB_SERIAL
#include <SLIPEncodedUSBSerial.h>
SLIPEncodedUSBSerial SLIPSerial( thisBoardsSerialUSB );
#else
#include <SLIPEncodedSerial.h>
SLIPEncodedSerial SLIPSerial(Serial1);
#endif

#define UPDATE_INTERVAL_MSEC 100

int32_t next_time;
uint32_t counter = 0;
boolean LEDState = false; // off

OSCBundle bundleOut;

void setup()
{
  pinMode(13, OUTPUT); // LED

  // begin SLIPSerial just like Serial
  SLIPSerial.begin(115200);
  while (!Serial)
    continue;

  next_time = millis() + UPDATE_INTERVAL_MSEC;
}


void LEDcontrol(OSCMessage &msg)
{
  if (msg.isInt(0))
  {
    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, (msg.getInt(0) > 0) ? HIGH : LOW);
  }
  else if (msg.isString(0))
  {
    int length = msg.getDataLength(0);
    if (length < 5)
    {
      char str[length];
      msg.getString(0, str, length);
      if ((strcmp("on", str) == 0) || (strcmp("On", str) == 0))
      {
        pinMode(LED_BUILTIN, OUTPUT);
        digitalWrite(LED_BUILTIN, HIGH);
      }
      else if ((strcmp("Of", str) == 0) || (strcmp("off", str) == 0))
      {
        pinMode(LED_BUILTIN, OUTPUT);
        digitalWrite(LED_BUILTIN, LOW);
      }
    }
  }
  else {
    LEDState = !LEDState;
    digitalWrite(13, LEDState);
  }
}



void loop()
{
  OSCBundle bundleIN;
  int size;

  while (!SLIPSerial.endofPacket())
    if ( (size = SLIPSerial.available()) > 0)
    {
      while (size--)
        bundleIN.fill(SLIPSerial.read());
    }

  if (!bundleIN.hasError()) {
    bundleIN.route("/OnMouseDownX", LEDcontrol);
  }
  bundleIN.empty();

  int32_t now = millis();

  if ((int32_t)(next_time - now) <= 0)
  {
    next_time += UPDATE_INTERVAL_MSEC;

    counter = (counter + 1) % 100;

    // the message wants an OSC address as first argument
    bundleOut.add("/CubeX").add((float)counter / 50);
  }

  SLIPSerial.beginPacket();
  bundleOut.send(SLIPSerial);       // send the bytes to the SLIP stream
  SLIPSerial.endPacket();     // mark the end of the OSC Packet
  bundleOut.empty();                // free space occupied by message
}

Thanks in advance.