Main Page | Data Structures | File List | Data Fields | Globals | Related Pages

How to handle packet-counter errors

Author:
Rudi Cilibrasi (cilibrar@ofb.net)
This text file describes one way of handling missing packets in the data stream passed from NeuroServer. These are detected by watching the packet counter. Of course, you could totally ignore the packet counter, but then you risk there being major communication problems (e.g. serial buffer overflows) without the front-end user having any idea that there has been a problem. On some machines, processor or graphics co-processor load can easily cause the serial buffer to overflow for brief moments, and the user needs to know about this.

In this code, the error is passed through to the rest of the app using an 'error' flag set, with zero values for the channel data. The error flag could be displayed as a red line on an oscilloscope display, for example. The code auto-detects the packet counter range, and behaves correctly even before it has completely established that range. It also handles the case where no counter is used and the field contains a constant value.

Pseudo-code for the input loop looks like this:

  packet= get_next_packet();
  pc= packet->counter;
  min= pc;
  max= pc;
  prev= pc;
  
  while (packet= get_next_packet()) {
      pc= packet->counter;
      if (pc < min) min= pc;
      if (pc > max) max= pc;
      next= (prev == max) ? min : prev+1;
      while (pc != next) {
          // Fill in a missing packet with an error packet
          packet2->chans[...]= 0;
          packet2->error= 1;
          process(packet2);
          prev= next;
          next= (prev == max) ? min : prev+1;
      }
      // Everything is fine
      packet->error= 0;
      process(packet);
      prev= pc;
  }

For output, if you are reading an EEG hardware device, you can just pass the raw packet counts through, and expect apps to deal with it.

However, if you have error flags on pre-processed packets, then the only way to pass that info through is to keep the packet counter going, but just omit the error packets. This allows the error to be detected correctly by aware apps, but has the disadvantage that unaware apps will squeeze out that error time-gap as if it didn't exist.

For example:

  pc= 0;
  
  while (packet= get_next_packet_to_output()) {
      packet->count= pc;
      pc++; pc &= 255;     // Increase counter in any case
  
      if (!packet->error) output(packet);
  }

Generated on Tue Feb 8 00:05:30 2005 for Neuroserver by doxygen 1.3.3