OSC messages are sent ONLY in response to messages received

I hope someone can lend some insight.

I have an Arduino happily attached to OpenFrameworks. I can send messages back and forth. On startup the Arduino sends OF an identifier, OF sends an acknowledgment back, and the Arduino then starts monitoring some pins.

However, in this monitoring state, the Arduino will BUFFER the osc messages and only send them, one by one, in response to receiving OF messages. I'm sure this problem is on the Arduino side.

Some code (for brevity I'm just including the loop, the SLIPSerial setup is all correct as it works in other cases):

void loop() {
  if (!identified) {
  // This sends an identification message:
    OSCMessage ident("/identification");
    ident.add(deviceId);
    SLIPSerial.beginPacket();
    ident.send(SLIPSerial);
    ident.empty();

    // This waits for a response:    
    OSCBundle incoming;
    int size;
    while (!SLIPSerial.endofPacket())
      if ((size = SLIPSerial.available()) > 0) {
    while (size--)
      incoming.fill(SLIPSerial.read());
    }
    if(!incoming.hasError()) {
        incoming.dispatch("/identificationACK", identificationHandler); // This works, it just toggles an LED if the ack is received, and switches identified to TRUE.
    }

  } else {

    // Read some pins, and if there is a change, send an OSC message:

    OSCMessage ping("/some/data");
    ping.add(0).add(1);

    SLIPSerial.beginPacket();
    ping.send(SLIPSerial);
    SLIPSerial.endPacket();
    ping.empty();

    // Also, be listening for incoming messages:

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

      if (!comingIn.hasError()) {
        comingIn.dispatch("/blink", blinkFunction); // The only messages that are incoming tell the Arduino to blink an LED, which is done in another function
      }
  } 
}

If I am only checking pins and sending messages for changes, it works. It's when I'm also trying to listen for OF messages that the outgoing messages get buffered, and will only fire, one by one, when some message is received from OF. Weird!

I have tried alternating between reading the pins and listening for messages (with a toggle variable back and forth) but that didn't improve anything.

Is there a special technique for sending and listening at the same time? Am I missing something really obvious?

Thanks so much for reading, and for any ideas! :slight_smile:

Try changing the while to an if.

while(!SLIPSerial.endofPacket())

Thanks gdsports, just came back to report that I fixed this. The problem is here:

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

     if (!comingIn.hasError()) {
       comingIn.dispatch("/blink", blinkFunction); // The only messages that are incoming tell the Arduino to blink an LED, which is done in another function
     }

I enclosed all this in an if statement that checks if there is anything to read:

    if(SLIPSerial.available()) { // This is the important part!
      OSCBundle mainBundleIN;
      int size;
      while(!SLIPSerial.endofPacket())
          if ((size =SLIPSerial.available()) > 0)
          {
             while(size--)
                mainBundleIN.fill(SLIPSerial.read());
          }
  
      if(!mainBundleIN.hasError())
      {
         mainBundleIN.dispatch("/blink", blinkFunction);
      }
    }

It seems that the problem was that this would go to the serial buffer and wait until something was sent, at which point it would release control of the buffer and everything that had been stored up would send.

Leaving this here in case someone else has this problem, as I couldn't find anything that related to this problem in the OF or Arduino forums. Hope it helps someone else.