Inconsistent Sampling Rates with U3 and Python | LabJack
 

Inconsistent Sampling Rates with U3 and Python

2 posts / 0 new
Last post
Deon
Deon's picture
Inconsistent Sampling Rates with U3 and Python

I'm using Python 3.7 to stream data from a Labjack U3. The configuration and streaming is done using sample code I found in the library.

Configuration:

        # Connect to the U3 device
        try:
            self.u3Dev = u3.U3()
            self.u3Dev.getCalibrationData()
            self.u3Dev.configTimerClock(0, 0)    # 4 Mhz timer base
            self.u3Dev.configIO(TimerCounterPinOffset=6,
                                NumberOfTimersEnabled=2, FIOAnalog=0x0F)
            self.u3Dev.getFeedback(u3.Timer0Config(12))
            self.u3Dev.getFeedback(u3.Timer1Config(12))
            self.u3Dev.streamConfig(NumChannels=6, Resolution=0, PChannels=[
                                    0, 1, 2, 3, 230, 231], NChannels=[31, 31, 31, 31, 31, 31], ScanFrequency=250)
            self.u3Dev.packetsPerRequest = 5
        except u3.LabJackException as e:
            print("Unable to connect to the LabJack device")
            print(e)
            sys.exit(1)

Reading:

        # Read the data from the stream
        for packet in self.u3Dev.streamData():
            if packet is not None:
                # Process the data

I'm running into problems with the sampling rate. Most of the time, the device samples each channel at 250Hz as requested; however, every now and again, the sample rate jumps to around 440Hz and it gets stuck there until I power cycle (unplug and replug the USB) the Labjack. I can not reliably recreate the issue on demand, but it happens often enough that it's causing issues in our data logging.

Any ideas what could be causing the issue?

LabJack Support
labjack support's picture
With the current code and

With the current code and configuration, I do not see an obvious cause of the problem.

1. Make sure you are using the latest firmware on your U3:

https://labjack.com/support/firmware

In Python your can check the current firmware with code like:

import u3
d=u3.U3()  # Open first found U3
print(d.configU3()["FirmwareVersion"])

The latest non-beta firmware version for the U3-HV/LV is 1.46.

2. Make sure your code is keeping up with the 250 Hz scan rate to prevent stream errors. Minimize delays between streamData calls.

3. Check for "errors" or "missed" samples in the streamData returns. In your code those would be:

packet["errors"]  # How many StreamData errors were detected.
packet["missed"]

If you are getting errors and missed being non-zero, then a stream buffer overflow and recovery occurred. This is due to software not reading out the stream scan/samples fast enough to keep up with the scan rate, leading to a stream buffer overflow on the U3 and auto-recovery. That is discussed in the third paragraph here:

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

Note that there are no -9999 dummy samples in LabJackPython streaming API.

If you want to check the error codes when there are "errors", here's a post from our old forum where I go over that (use packet['result'][11::64] instead of r['result'][11::64] for your code):

https://forums.labjack.com/index.php?showtopic=7126&p=24083

Potentially errors could effect software timing of the scan rate.