Start the stream.
Periodically retrieve stream data in a loop.
Stop the stream.
Following is example pseudocode to configure a 2-channel stream. In addition to the stream parameters configured below, some
applications might also need to configure analog input settings such as range and resolution:
//Set the scan rate.
AddRequest (lngHandle, LJ_ioPUT_CONFIG, LJ_chSTREAM_SCAN_FREQUENCY, scanRate, 0, 0);
//Give the UD driver a 5 second buffer (scanRate * 2 channels * 5 seconds).
AddRequest (lngHandle, LJ_ioPUT_CONFIG, LJ_chSTREAM_BUFFER_SIZE, scanRate*2*5, 0, 0);
//Configure reads to wait and retrieve the desired amount of data.
AddRequest (lngHandle, LJ_ioPUT_CONFIG, LJ_chSTREAM_WAIT_MODE, LJ_swSLEEP, 0, 0);
//Define the scan list as AIN2 then AIN3.
AddRequest (lngHandle, LJ_ioCLEAR_STREAM_CHANNELS, 0, 0, 0, 0);
AddRequest (lngHandle, LJ_ioADD_STREAM_CHANNEL, 2, 0, 0, 0);
AddRequest (lngHandle, LJ_ioADD_STREAM_CHANNEL, 3, 0, 0, 0);
//Execute the requests.
GoOne (lngHandle);
Next, start the stream:
//Start the stream.
eGet(lngHandle, LJ_ioSTART_STREAM, 0, &dblValue, 0);
//The actual scan rate is dependent on how the desired scan rate divides into
//the LabJack clock. The actual scan rate is returned in the value parameter
//from the start stream command.
actualScanRate = dblValue;
actualSampleRate = 2*dblValue;
Once a stream is started, the data must be retrieved periodically to prevent the buffer from overflowing. To retrieve data, add a
request with IOType
LJ_ioGET_STREAM_DATA
. The Channel parameter should be
LJ_chALL_CHANNELS
or a specific
channel number (ignored for a single channel stream). The Value parameter should be the number of scans (all channels) or
samples (single channel) to retrieve. The x1 parameter should be a pointer to an array that has been initialized to a sufficient size.
Keep in mind that the required number of elements if retrieving all channels is number of scans * number of channels.
Data is stored interleaved across all streaming channels. In other words, if two channels are streaming, 0 and 1, and
LJ_chALL_CHANNELS
is the channel number for the read request, the data will be returned as Channel0, Channel1, Channel0,
Channel1, etc. Once the data is read it is removed from the internal buffer, and the next read will give new data.
If multiple channels are being streamed, data can be retrieved one channel at a time by passing a specific channel number in the
request. In this case the data is not removed from the internal buffer until the last channel in the scan is requested. Reading the
data from the last channel (not necessarily all channels) is the trigger that causes the block of data to be removed from the buffer.
This means that if three channels are streaming, 0, 1 and 2 (in that order in the scan list), and data is requested from channel 0,
then channel 1, then channel 0 again, the request for channel 0 the second time will return the same data as the first request. New
data will not be retrieved until after channel 2 is read, since channel 2 is last in the scan list. If the first get stream data request is for
10 samples from channel 1, the reads from channels 0 and 2 also must be for 10 samples. Note that when reading stream data
one channel at a time (not using
LJ_chALL_CHANNELS
), the scan list cannot have duplicate channel numbers.
There are three basic wait modes for retrieving the data:
LJ_swNONE
: The Go call will retrieve whatever data is available at the time of the call up to the requested amount of data.
A Get command should be called to determine how many scans were retrieved. This is generally used with a software timed
read interval. The number of samples read per loop iteration will vary, but the time per loop iteration will be pretty consistent.
Since the LabJack clock could be faster than the PC clock, it is recommended to request more scans than are expected
each time so that the application does not get behind.
LJ_swSLEEP
: This makes the Go command a blocking call. The Go command will loop until the requested amount of is
retrieved or no new data arrives from the device before timeout. In this mode, the hardware dictates the timing of the
application ... you generally do not want to add a software delay in the read loop. The time per loop iteration will vary, but the
number of samples read per loop will be the same every time. A Get command should be called to determine whether all the
data was retrieved, or a timeout condition occurred and none of the data was retrieved.
LJ_swALL_OR_NONE
: If available, the Go call will retrieve the amount of data requested, otherwise it will retrieve no data.
A Get command should be called to determine whether all the data was returned or none. This could be a good mode if
hardware timed execution is desirable, but without the application continuously waiting in SLEEP mode.
The following pseudocode reads data continuously in SLEEP mode as configured above:
//Read data until done.
while(!done)
{
//Must set the number of scans to read each iteration, as the read
//returns the actual number read.
numScans = 1000;
//Read the data. Note that the array passed must be sized to hold
//enough SAMPLES, and the Value passed specifies the number of SCANS
//to read.
eGet(lngHandle, LJ_ioGET_STREAM_DATA, LJ_chALL_CHANNELS, &numScans, array);
actualNumberRead = numScans;
//When all channels are retrieved in a single read, the data
//is interleaved in a 1-dimensional array. The following lines
//get the first sample from each channel.
channelA = array[0];
channelB = array[1];
45
Summary of Contents for UE9
Page 84: ...84 ...