Skip to main content
Skip table of contents

InitializeAperiodicStreamOut [LJM User's Guide]

Requires LJM 1.2100 or later.

Initializes all device registers and stream out objects necessary to start an aperiodic stream-out.

Syntax

LJM_ERROR_RETURN LJM_InitializeAperiodicStreamOut(
int Handle,
int StreamOutIndex,
int TargetAddr,
double ScanRate)

Parameters

Handle [in]

A device handle. The handle is a connection ID for an active device. Generate a handle with LJM_Open or LJM_OpenS.

StreamOutIndex [in]

The number assigned to this stream-out. See the Stream Out section of the T-series datasheet for more information.

TargetAddr [in]

The target register to send stream-out data to. See the Stream Out section of the T-series datasheet for a list of potential targets.

ScanRate [in]

The desired number of scans per second. Should be the same value as set in LJM_eStreamStart. Keep in mind that data rate limits are specified in Samples/Second which is equal to NumAddresses * Scans/Second or NumAddresses * ScanRate.

Returns

LJM errorcodes or 0 for no error.

Performing Aperiodic Stream-Out

For each waveform being streamed out:

  1. Choose which target channel will output the waveform

  2. Call LJM_InitializeAperiodicStreamOut

  3. Start stream with STREAM_OUT#(0:3) in the scan list

  4. Stream Data: call LJM_WriteAperiodicStreamOut as necessary

  5. Stop stream

Target Selection

The aperiodic stream-out functions use the same targets as listed on the stream-out page.

  • DAC0

  • DAC1

  • FIO_STATE

  • FIO_DIRECTION

  • EIO_STATE

  • EIO_DIRECTION

  • CIO_STATE

  • CIO_DIRECTION

  • MIO_STATE

  • MIO_DIRECTION

Call LJM_InitializeAperiodicStreamOut

Call LJM_InitializeAperiodicStreamOut with the selected target.

LJM_WriteAperiodicStreamOut can be called before starting stream to preemptively queue up data for the device stream out buffer.

Start Stream

Next, start stream with STREAM_OUT#(0:3) in the scan list. The stream-out index must match the index value passed to LJM_InitializeAperiodicStreamOut.

The order of STREAM_OUT#(0:3) in the scan list determines when the target updated. For example, if STREAM_OUT3 is before STREAM_OUT0 in the scan list, STREAM_OUT3_TARGET will be updated before STREAM_OUT0_TARGET.

Stream Data:

Read from stream using LJM_eStreamRead, if there are stream-in channels.

Call LJM_WriteAperiodicStreamOut to add data points to stream out.

Stop Stream

To stop stream, use LJM_eStreamStop.

Remarks

LJM_InitializeAperiodicStreamOut allocates a 20 second buffer in LJM (according to the stream rate).

LJM_InitializeAperiodicStreamOut sets the device stream out buffer (STREAM_OUT#(0:3)_BUFFER_ALLOCATE_NUM_BYTES) to 16384.

Configuration

This function writes to the following registers, which should not be set manually when using LJM_InitializeAperiodicStreamOut for a given stream index:

  • STREAM_OUT#(0:3)_TARGET

  • STREAM_OUT#(0:3)_BUFFER_ALLOCATE_NUM_BYTES

  • STREAM_OUT#(0:3)_LOOP_NUM_VALUES

  • STREAM_OUT#(0:3)_ENABLE

  • STREAM_OUT#(0:3)_BUFFER_U16

  • STREAM_OUT#(0:3)_SET_LOOP

Example

Aperiodic Stream Out Example

C
int handle;
int err = 0;
double scanRate = 1000;
const int NUM_WRITES = 9;
enum { NUM_SCAN_ADDRESSES = 1 };
const char * scanList[NUM_SCAN_ADDRESSES] = { "STREAM_OUT0" };
int targetAddr = 1000; // DAC0
int streamOutIndex = 0;
int samplesToWrite = 512;

// Make an arbitrary waveform that increases voltage linearly from 0-2.5V
double * values = new double[samplesToWrite];
double increment = double(1) / samplesToWrite;
for (int i = 0; i < samplesToWrite; i++) {
    double sample = 2.5 * increment * i;
    values[i] = sample;
}

// Open first available LabJack device
err = LJM_Open(LJM_dtANY, LJM_ctANY, "LJM_idANY", &handle);
ErrorCheck(err, "LJM_Open");
PrintDeviceInfoFromHandle(handle);
ErrorCheck(err, "PrintDeviceInfoFromHandle");

err = LJM_InitializeAperiodicStreamOut(
    handle,
    streamOutIndex,
    targetAddr,
    scanRate
);
ErrorCheck(err, "LJM_InitializeAperiodicStreamOut");
printf("\n");
int queueVals;
// Write some values to the device buffer before starting the stream
err = LJM_WriteAperiodicStreamOut(
    handle,
    streamOutIndex,
    samplesToWrite,
    values,
    &queueVals
);
ErrorCheck(err, "LJM_WriteAperiodicStreamOut");

int scansPerRead = scanRate / 2;
int aScanList[NUM_SCAN_ADDRESSES];
int aTypes[NUM_SCAN_ADDRESSES];
int deviceScanBacklog;
int ljmScanBacklog;
err = LJM_NamesToAddresses(
    NUM_SCAN_ADDRESSES,
    scanList,
    aScanList,
    aTypes
);
ErrorCheck(err, "LJM_NamesToAddresses scan list");

int startTime = GetCurrentTimeMS();
err = LJM_eStreamStart(
    handle,
    scansPerRead,
    NUM_SCAN_ADDRESSES,
    aScanList,
    &scanRate
);
ErrorCheck(err, "LJM_eStreamStart");

for (int i = 0; i < NUM_WRITES; i++) {
    err = LJM_WriteAperiodicStreamOut(
        handle,
        streamOutIndex,
        samplesToWrite,
        values,
        &queueVals
    );
    ErrorCheck(err, "LJM_WriteAperiodicStreamOut in loop");
}

int runTime = GetCurrentTimeMS() - startTime;
// 512 samples * 10 writes = 5120 samples. scan rate = 1000
// samples/sec, so it should take 5.12 seconds to write all data out
int streamOutMS = 1000 * samplesToWrite * (NUM_WRITES + 2) / scanRate;
if (runTime < streamOutMS) {
    MillisecondSleep(streamOutMS - runTime);
}
err = LJM_eStreamStop(handle);
ErrorCheck(err, "Problem closing stream");
err = LJM_Close(handle);
ErrorCheck(err, "Problem closing device");

delete[] values;
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.