« Close

Datasheets and User Guides

App Notes

Software & Driver

 

5.2.12 - StreamConfig

Stream mode operates on a table of channels that are scanned at the specified scan rate. Before starting a stream, you need to call this function to configure the table and scan clock.

Table 5.1.12-1. StreamConfig Command Response

Command:      
Byte      
0 Checksum8    
1 0xF8    
2 NumChannels + 4    
3 0x11    
4 Checksum16 (LSB)    
5 Checksum16 (MSB)    
6 NumChannels    
7 ResolutionIndex    
8 SamplesPerPacket (1-25)   
9 Reserved    
10 SettlingFactor    
11 ScanConfig    
    Bit 3: Internal stream clock frequency 
      b0: 4 MHz
      b1: 48 MHz
    Bit 1: Divide Clock by 256 
12-13 Scan Interval (1-65535)   
14 ChannelNumber (Positive)   
15 ChannelOptions    
    Bit 7: Differential  
    Bits 4-5: GainIndex  
       
Repeated 14-15 for each channel     
       
Response:      
Byte      
0 Checksum8    
1 0xF8    
2 0x01    
3 0x11    
4 Checksum16 (LSB)    
5 Checksum16 (MSB)    
6 Errorcode    
7 0x00    
    • NumChannels: This is the number of channels you will sample per scan (1-25).
    • ResolutionIndex: Resolution index setting of 0-8. 0 corresponds to 1, and is the most common resolution used in stream mode since the data returned is 16-bits per sample. See Section 3.2 for related information. Stream mode is not supported on the hi-res converter (resolutions 9-12 not supported in stream).
    • SamplesPerPacket: Specifies how many samples will be pulled out of the U6 FIFO buffer and returned per data read packet. For faster stream speeds, 25 samples per packet are required for data transfer efficiency. A small number of samples per packet would be desirable for low-latency data retrieval. Note that this parameter is not necessarily the same as the number of channels per scan. Even if only 1 channel is being scanned, SamplesPerPacket will usually be set to 25, so there are usually multiple scans per packet.
    • SettlingFactor: Specifies the settling time for all stream channels in 10us increments. If SettlingFactor is set to zero the U6 will attempt to determine the minimum required settling time based on gain and resolution settings. Valid settings are 0 to 255. When set to 0 the U6 will select the minimum required settling time based on gain and resolution settings.
    • ScanConfig: Has bits to specify the stream clock.
    • ScanInterval: (1-65535) This value divided by the clock frequency defined in the ScanConfig parameter, gives the interval (in seconds) between scans. While streaming the SPC terminal is toggled to provide information about the stream timing: low at start of scan, high at start of sample, low at end of sample, ..., high at end of scan.
    • ChannelNumber: This is the positive channel number. 0-143 for analog input channels or 193-224 for digital/timer/counter channels.
    • ChannelOptions: If bit 7 is set, a differential reading is done rather than single-ended. Bits 4-5 specify the gain:

Table 5.2.12-2. 

  GainIndex Gain Max V Min V
Bipolar b00 (d0) 1 10.1 -10.6
Bipolar b01 (d1) 10 1.01 -1.06
Bipolar b10 (d2) 100 0.101 -0.106
Bipolar b11 (d3) 1000 0.0101 -0.0106

13 comments

For ScanConfig, why would you choose one stream clock frequency over the other? (i.e., what is the downside of the faster stream clock?)

When calculating ScanInterval, do you divide by the stream clock rate in Hz or MHz? And if Hz, is it 4e6 or 2^22? For example, to achieve a desired sample rate (in Hz) of "sampleRateHz", is it:

<code>ScanInterval = 4 / sampleRateHz</code>

or

<code>ScanInterval = 2^22 / sampleRateHz</code>

? thanks,

The faster clock source will produce higher resolution while the slower source allows some very slow stream rates. I would start with 48MHz and use 4 if necessary.

The description says that the period between scans is Code / ClockBase. Just invert for frequency. As for MHz vs Hz you just need to keep your units straight. In this case the math is simple; whatever unit you plug in is what you will get out. eg:

48MHz/48000 = 0.001MHz = 1kHz.

48000000Hz/9600 = 5000Hz = 5kHz.

 

ok, thanks. So this means that the minimum possible scan rate is:

4 MHz/65535 = 61 Hz

and so for sample rates lower than 60 Hz, we should use command/response mode. Is that correct, or is there a way to configure slower stream rates?

Correct, stream is intended for 500Hz+.

A few people have wanted the hardware timing for still slower frequencies, so we added the divide by 256 option in byte 11. With that option you can go as low as 0.238Hz.

Great, thanks for your help.

Can the U6-Pro stream 24-bit analog data? Do you need to do anything special besides set the Resolution Index to 9+ and interpret the results as 3-byte values? Thanks,

No, the hi-res converter does not work in stream mode.  Check out forum topic #5381.

Question on the Numchannel parameter:

What is a channel defined to be?  Is it a port such as FIO or is it a bit like FIO0?  How does one know the crossreference between 1-25 and the port / pin you want to monitor?

Is it possible to stream the following combination AIN0, AIN1, FIO0, FIO2, FIO3,FIO4?

Thanks..

The channels in stream mode are specifically analog input channel numbers.  However, there are some special analog input channels numbers that will do what you want as described in Section 3.2.1.  You would stream 0, 1, and 193.

Hello, I have a U6 and I'm using it to measure force with a load-cell. I need a sampling frequency of 60 Hz, but because of the noise I tried using stream mode and then averaging the data. But I don't know how to set the stream parameters in order to get the frequency I want. Can someone help?
Thanks in advance.

1.  Too much noise from a load-cell?  I suggest you start a forum topic so we can help troubleshoot, determine where the noise is coming from, and how best to handle it.

2.  This page is for the low-level StreamConfig function.  Are you doing low-level streaming on Linux or Max?  Have you found an example to start from?

heroux.martin's picture

I would like to be able to collect analog data at a known, stable rate for a predetermined amount of time. I am using a U6 on a Linux machine with Python.  Please excuse my lack of technical expertise; I am not an engineer by training and my only DAQ experience is with NI and LabView as well as CED and Spike2.


GENERAL STREAMING FREQUENCY.

  • When using the exodriver, only the low-level commands are available. I have been able to successfully run 'StreamTest.py' to collect data from 2 analog channels and update a matplotlib figure as the packets are received. The documentation for u6.U6.streamconfig() states that: "On a decent machine, you can expect to stream a range of 0.238 Hz to 15 Hz. Without the conversion, you can get up to 55 Hz." May I ask what these rates relate to? In the original question asked on this page, there is mention that the divide by 256 option was added to allow rates as low as 0.238Hz, but that streaming is intended for rates >500Hz. If streaming is intended for higher rates, why does the Python documentation mention 0.238 Hz to 15 Hz?
  • I was able to run 'StreamTest.py' with scanFrequency set to 2000Hz. However, at 5000Hz data/packets starts to be missed. Should I be able to collect 2 analog channels with a scan frequency of 5000Hz?
  • The U6 spec sheet mentions "Hardware timed input streaming (supported with hi-speed converter only) has a maximum rate that varies with resolution from 4 ksamples/s at 18 bits to 50 ksamples/s at 16 bits." How does one achieve these sampling rates with the U6 on a linux machine? Sorry for my ignorance.


SPECIFIC STREAMING FREQUENCY.

  • Is the scanFrequency exact? i.e., if I set the frequency to 1000Hz, is the data scanned at 1000Hz? I ask because 'StreamTest.py' provides values at the end of a run that do not perfectly match the scanFrequency. Is this because things are being calculated based on datetime.datetime.now() ? Here is an example:
    10 requests with 40.0 packets per request with 25 samples per packet = 10000 samples total.
    0 samples were lost due to errors.
    Adjusted number of samples = 10000
    The experiment took 5.390722 seconds.
    Scan Rate : 5000 scans / 5.390722 seconds = 927.519541909 Hz
    Sample Rate : 10000 samples / 5.390722 seconds = 1855.03908382 Hz 

SUMMARY. I apologize if my question is not clear or sounds off the mark. I simply want to figure out how to sample several analog channels at a set rate (e.g., 2000Hz) for a predetermined amount of time (e.g. 30s, 1min, 5 min, etc). Is this possible with these low-level stream commands? If not, how could I achieve this?

Thanks in advance.

Martin 

LabJack Support's picture

GENERAL STREAMING FREQUENCY.

1. In the development version of LabJackPython, and a future stable release, the documentation that states:

"On a decent machine, you can expect to stream a range of 0.238 Hz to 15 Hz. Without the conversion, you can get up to 55 Hz."

Has been removed. I'm not sure of it's original intent, but it may have been a typo that meant 15 kHz and 55 kHz, though the documented max. is 50k samples/second. Stream mode is intended for higher data rates, but some users like to use it at lower scan rates since scan times are controlled by the U6 hardware.

2 and 3. This will depend on the speed of your computer and USB connection, but in most cases 10 kHz is possible (2 channels at 5000 Hz) in Python. Make sure you have a USB high-high connection as mentioned in note 2 here:

https://labjack.com/support/datasheets/u6/operation/stream-mode

On slower computers Python is not fast enough for reading and processing higher stream mode speeds, and in some cases has issues with slower speeds. For example, the Raspberry Pi 2 running LabJackPython has limited stream mode speeds. I believe it had issues with streaming over 2000 Hz. You could try either the threading method of streaming and see if that helps (look the streamTest-threading.py example), or try making a C/C++ language application instead which is faster than Python. A C/C++ application can do 50k stream speeds on a Raspberry Pi 2. Also, you may want to try the development version of LabJackPython on Github and see if there are Python speed improvements:

https://github.com/labjack/LabJackPython

SPECIFIC STREAMING FREQUENCY.

On the U6 hardware the scan rate/frequency is exact or close. As seen on this page, the hardware scan interval is a 16-bit value and is calculated like so in the streamConfig call.

            if ScanFrequency < 1000:
                if ScanFrequency < 25:
                    SamplesPerPacket = ScanFrequency
                DivideClockBy256 = True
                ScanInterval = 15625 // ScanFrequency
            else:
                DivideClockBy256 = False
                ScanInterval = 4000000 // ScanFrequency


So 1000 Hz will be a scan interval of 4000, so the scan rate will be exact.

Keep in mind that stream mode is hardware timed. The application is reading the buffered samples from the U6 hardware. "Missed" samples occur when the stream buffer overflows and autorecovery mode kicks in, and this is caused by the application being able to read from this stream buffer at a fast enough rate. Likely the software timed scan and sample frequency/rate will be slower than the exact hardware timed rates due to software timing inaccuracy (datetime.now()) and overhead, but it does give a good indication on how fast the software is readings scans/samples from the U6.

What you want is achievable using low-level commands (the UD driver uses low-level function command-responses for stream mode). Keep in mind the various scan rate limitations based on resolution and range mentioned in the stream-mode link, and a resolution index of 1 and +/-10V range allows the maximum scan rate of 50k Hz with one channel. If you configure a scan rate of 2000 Hz, and want say 30 seconds of data, you will read 60000 scans (scan rate * time) of samples in stream mode. If streaming 2 channels, the 60000 scans will be 120000 samples of data total (scans * #Channels).