User`s manual

Writing Custom Applications
Handle Data Available
Once streaming is enabled and the module is triggered, data flow will commence. Samples will be accumulated into the
onboard FIFO, then they are bus-mastered to the Host PC into page-locked, driver-allocated memory following a two -word
header (data packets). Upon receipt of a data packet, Malibu signals the Stream.OnDataAvailable event. By hooking this
event, your application can perform processing on each acquired packet. Note, however, that this event is signaled from
within a background thread. So, you must not perform non-reentrant OS system calls (such as GUI updates) from within
your handler unless you marshal said processing into the foreground thread context.
//---------------------------------------------------------------------------
// ApplicationIo::HandleDataAvailable() -- Handle received packet
//---------------------------------------------------------------------------
void ApplicationIo::HandleDataAvailable(PacketStreamDataEvent & Event)
{
if (Stopped)
return;
static Buffer Packet;
//
// Extract the packet from the Incoming Queue...
Event.Sender->Recv(Packet);
IntegerDG Packet_DG(Packet);
PacketBufferHeader PktBufferHdr(Packet);
When the event is signaled, the data buffer must be copied from the system bus-master pool into an application buffer. The
preceding code copies the packet into the local Buffer called Packet. Since data sent from the hardware can be of arbitrary
type (integers, floats, or even a mix, depending on the board and the source), Buffer objects have no assumed data type and
have no functions to access the data in them. Instead, a second class called a datagram wraps the buffer, providing typed or
specialized access to the data in the buffer.
The above code associates 2 datagram classes with the packet. IntegerDG provides access to the data in the packet as if it
were an array of 32-bit integers. The PacketBufferHeader datagram class provides access to the header of the packet, and
defines access methods to the fields in the header of a Packet Stream buffer.
//
// Process the data packet
int Channel = PktBufferHdr.PeripheralId();
// Discard packets from sources other than analog devices
if (Channel >= Channels())
return;
Each Packet Stream Buffer consists of a header and a body of data. The header contains a field that specifies the source of
the data packet. This can be interrogated to provide different processing for packets from each source. In the fragment above,
packets containing peripheral IDs greater than the number of enabled channels are discarded. Consequently, alert packets are
not retained or processed.
// Calculate transfer rate in KB/s
double Period = Time.Differential();
if (Period)
FBlockRate = Packet_DG.SizeInBytes() / (Period*1.0e6);
X5-GSPS User's Manual 55