The usb2600 module: pure Python, high-level, object-oriented interface

High-level, object-oriented Python interface for MCCDAQ’s USB2600 data acquisition devices.

Built on top of the low-level Cython usb2600.wrapper module.

It includes advanced functionalities for analog input and output scans of arbitrary length and synchronous analog input/output scanning.

Todo

Add support for retrigger and burst options

Examples:
>>> from mccdaq import usb2600
>>> daq = usb2600.USB2600()   # Initialise device
>>> daq.device.blink(10)      # blink LED 10 times
>>> daq.device.temperature()  # read temperature
>>> daq.status                # return info about AI/AO scans
Counter:
>>> daq.counter0.init()  # Initialiase Counter 0
>>> daq.counter0.read()  # Read number of detected edges since init()
Timer:
>>> daq.timer0.configure(frequency=1000, duty_cycle=0.25) #1 kHz, 0.25 duty cycle square waveform
>>> daq.timer0.start()
>>> daq.timer0.stop()
Analog input:
>>> v = daq.ai0.read()         # Single sample from AI0
>>> v = daq.ai.read((1,4,16))  # Single sample from multiple channels
>>> aiscan = daq.AIScan((0,1,2), nsamples=1000, rate=1000)  # AI scan: 1kS at 1kS/s from channels 0,1 and 2
>>> aiscan.run()           # Run the scan
>>> aiscan.data            # Acquired data (numpy array)
Analog output:
>>> daq.ao0(1)           # Set AO0 to 1V
>>> wf = waveform.sine(10, nsamples=1000, rate=1000) # Sine waveform, 10Hz, 1kS @ 1kS/s
>>> aoscan = daq.AOScan((3,), wf)  # Set up analog output of waveforms in data
>>> aoscan.run(thread=True)    # run the scan in background
>>> aoscan.run(n=10)           # output the waveform 10 times
>>> aoscan.run(n=0)           # output the waveform forever
Synchronous analog input/output:
 
>>> wf = waveform.helixscan(1, 10)         # Create 2D waveform
>>> aio = daq.AIOScan((0,1,2), (0,1), wf)  # Output waveform on AO channels 0 and 1 while acquiring on AI channels 0, 1 and 2
>>> aio.run()                              # start scan
>>> aio.data.plot()                        # visualise acquired data
Triggering analog scans:
 
>>> daq.external_trigger.configure()           # Configure external trigger
>>> daq.internal_trigger.configure(daq.dio.A7) # Configure internal trigger, source is A7
Digital input/outputs:
 
>>> daq.dio.A.direction = 0x0F  # Configure bits 7-4 as outputs, 3-0 as input on port A
>>> daq.dio.A()                 # Read whole port
>>> daq.dio.A(0xAA)             # Set all bits on port
>>> daq.dio.A0.isoutput = True  # Configure pin A0 as output
>>> daq.dio.A1.isinput = True   # Configure pin A1 as input
>>> # Several methods to set an output pin
>>> daq.dio.A0(True)          # pin is callable
>>> daq.dio.A0.set()          # set() = HIGH
>>> daq.dio.A0.reset()        # reset() = LOW
>>> daq.dio.A0.state = True   # property
>>> # Several methods to read an input pin
>>> daq.dio.A1()              # pin is callable
>>> daq.dio.A1.get()          # method
>>> daq.dio.A1.state          # property

Note

If the waveform module is not available, AO_Scan and AIO_Scan will not work.

  1. 2014 Guillaume Lepert, Imperial College London.

exception mccdaq.usb2600.usb2600.USB2600Error[source]

Alias of exceptions.RuntimeError to represent USB2600 exceptions.

class mccdaq.usb2600.usb2600.USB2600(device=None, model=2627)[source]

High-level interface for MCCDAQ USB-2600 series data acquisition device.

This top-level container provides access to:
  • analog inputs ai0, ai1, ... are instances of AI_Single.
  • ai represents several analog input channels to be read simultaneously: see AI_Multichannel.
  • analog outputs ao0, ao1, ... are instances of AO_Single.
  • digital input/output ports dio.A, dio.B, ..., are instances of DIO_Port. Each contains 8 pin0, pin1, etc. , instances of DIO_Pin.
  • counters counter0, counter1, ... are instances of Counter.
  • timers timer0, timer1, ... are instances of Timer.

Create analog scans (AI, AO and synchronous AIO) with the methods AIScan(), AOScan() and AIOScan().

Blink the LED with blink().

Read the device temperature, serial_number, and status.

Parameters:
  • device – a usb2600.wrapper.USB2600_Wrapper instance. If None, creates a new instance.
  • model (int) – the model of the device to look for (2623, 2627, 2633 or 2637)
serial_number

Return the device’s serial number.

temperature

Return the device internal temperature, in Celsius.

Blink the LED n times.

status

Return the device status as a dictionary.

AIScan(channels, nsamples, rate, trigger=False, retrigger=1, burst=True, packet_size=240, tf=0.5, verbose=False)[source]

Returns an Analog Input object (AI_Scan)

Parameters:
  • channels – tuple of integers indicatings the analog input channels.
  • nsamples – number of samples to acquire
  • rate – sampling frequency (Hz)
  • trigger – if True, start the scan on XTTLTRG.
AOScan(channels, waveform, rate=None, trigger=False, retrigger=1, packet_size=240, tf=0.5, verbose=False)[source]

Returns an Analog Output object (see AO_Scan)

Parameters:
  • channels – tuple of integers indicatings the analog output channels.
  • waveform – a Waveform instance containing the data. Must have the same number of rows as channels
  • rate – sampling frequency (Hz). if not given, use that of the waveform.
  • trigger – if True, start the scan on XTTLTRG.
AIOScan(AIchans, AOchans, AOwaveform, rate=None, packet_size=240, tf=0.5, verbose=False)[source]

Return a Synchronous Analog Input/Output scan object (AIO_Scan).

Parameters:
  • AIchans – tuple of integers indicatings the analog input channels.
  • AOchans – tuple of integers indicatings the analog ouput channels.
  • AOdataWaveform object

Note

  • The sampling frequency is taken from the AOdata waveform.
  • The trigger’s digital output must be connected to the external trigger input XTTLTRG.
class mccdaq.usb2600.usb2600.Timer(device, timer)[source]

Each timer on the device is an instance of this class.

Parameters:
  • device – the USB2600 instance
  • timer (int) – timer index on the device
status(read_configuration=False)[source]

Check the timer status (Enable, Running).

Optionally query the current configuration (Period, Pulse width, Delay, Count) and writes to the corresponding attributes.

configure(frequency=None, period=None, duty_cycle=0.5, pulse_width=None, counts=0, delay=0)[source]

Configure the timer (either frequency (in Hz) or period (in units of the 64MHz clock cycles) can be specified.

Parameters:
  • frequency (float) – in Hz
  • period (int) – in units of the 64MHz clock
  • duty_cycle (float) – [0...1] (default 0.5)
  • pulse_width (int) – in units of the 64MHz clock
  • count (int) – number of pulses to output (0=forever, default)
  • delay (int) – delay before starting timer, in units of clock cycles.
start(invert=False)[source]

Start the timer.

stop()[source]

Stop the timer.

class mccdaq.usb2600.usb2600.Counter(device, counter)[source]

Each counter on the device will be an instance of this class.

Parameters:
  • device – the USB2600 instance
  • counter (int) – counter index on the device
init()[source]

Initialise the counter.

read()[source]

Return the number of counts since the counter was initialised.

class mccdaq.usb2600.usb2600.DIO(device)[source]

Container for DIO ports and pins.

class mccdaq.usb2600.usb2600.DIO_Port(device, port)[source]

Controls a DIO port.

Parameters:
  • device – the USB2600 instance
  • port (int) – port index on the device
state

Set or query the port state.

direction

Query or configure the port direction (0xFF all inputs, 0x00 all ouputs, or a mixture: 0x01, etc.)

read()[source]

Read the port state.

write(byte)[source]

Write byte to the port.

class mccdaq.usb2600.usb2600.DIO_Pin(port, pin)[source]

Controls a single bit of a DIO port.

Parameters:
  • port – a DIO_Port instance
  • pin (int) – the target pin on port
get()[source]

Return the pin state.

set()[source]

Set the pin to High.

reset()[source]

Reset the pin to Low.

state

Set or query the pin state.

isinput

Query whether the pin is configured as input, or configure it so by setting it to True.

isoutput

Query whether the pin is configured as output, or configure it so by setting it to True.

class mccdaq.usb2600.usb2600.ExternalTrigger(device, mode='Edge', polarity='Rising')[source]

Represents an external trigger (external digital output connected to XTTLTRG).

Parameters:
  • mode – ‘Edge’ or ‘Level’.
  • polarity – in ‘Edge’ mode: ‘Rising’ or ‘Falling’. In ‘Level’ mode : ‘Low’ or ‘High’.
configure(mode='Edge', polarity='Rising')[source]
Parameters:
  • mode – ‘Edge’ or ‘Level’.
  • polarity – in ‘Edge’ mode: ‘Rising’ or ‘Falling’. In ‘Level’ mode : ‘Low’ or ‘High’.
class mccdaq.usb2600.usb2600.InternalTrigger(dio_pin, mode='Edge', polarity='Rising')[source]

Internal trigger by connection XTTLTRG to a digital output pin.

Parameters:
  • dio_pin – a DIO_Pin instance, physically connected to XTTLTRG.
  • mode – ‘Edge’ or ‘Level’.
  • polarity – in ‘Edge’ mode: ‘Rising’ or ‘Falling’. In ‘Level’ mode : ‘Low’ or ‘High’.
trigger()[source]

Fire the trigger.

reset()[source]

Reset the trigger so it can be fired again.

class mccdaq.usb2600.usb2600.AI_Single(device, channel)[source]

Single sample, single channel analog input.

>>> volts = daq.ai0.read()
>>> volts = daq.ai0()      # callable.
read(nsamples=1, dt=1)[source]

Read analog input samples.

Parameters:
  • nsamples – number of samples to acquire
  • dt – time (in s, default 1) between samples.
class mccdaq.usb2600.usb2600.AI_Multichannel(device)[source]

Single sample, multiple channel acquisition: out = daq.ai.read((ch1, ..., chn)).

read(channels)[source]

Single sample, single channel analog input.

class mccdaq.usb2600.usb2600.AI_Scan(device, channels, nsamples, rate, trigger=False, retrigger=1, burst=True, packet_size=240, tf=0.5, verbose=True)[source]

Analog Input scan.

Acquired data in data.

TO DO: return data as a Waveform object.

Parameters:
  • deviceUSB2600 instance
  • channels – tuple of integers indicatings the analog input channels.
  • nsamples – number of samples to acquire
  • rate – sampling frequency (Hz). To use an external clock source (connected to AI_CLK_IN on the board), set rate=0.
  • trigger – Whether to wait for a trigger on XTTLTRG to start acquiring. If False, acquisition starts when calling run()
  • retrigger – How often
  • burst – burst mode (UNTESTED)
  • packet_size – number of bytes in each USB read operation.
  • tf – USB read time factor. If getting AI_FIFO_OVERRUN, try decreasing tf to pop data from the FIFO faster.
  • verbose – if True, print info to console while scan is running.
config()[source]

Configure the AI scan.

start()[source]

Start the AI scan.

read_basic(nsamples)[source]

Read nsamples per channel from FIFO.

read()[source]

Read a single packet of data from FIFO.

stop()[source]

Stop the AI scan.

clear()[source]

Clear the AI FIFO. Data will be lost!

flush()[source]

Read all remaining elements in FIFO.

It may be necessary to call flush() before an AI or AIO scan to remove old, unread samples from AI FIFO.

run(thread=False)[source]

Start the scan in main or background thread.

In a background thread, call stop() to stop the scan before completion. In the main thread, use Ctl+C.

fifo_overrun

Return True if the AI FIFO is full.

running

Return True while samples are being read.

done

Return True once all samples have been read.

class mccdaq.usb2600.usb2600.AO_Single(device, channel)[source]

Single sample, single channel analog ouput.

>>> daq.ao0.write(0.56) # output 0.56 volts to AO0
write(voltage)[source]

Set the AO voltage.

read()[source]

Read the curreny AO voltage.

class mccdaq.usb2600.usb2600.AO_Scan(device, channels, waveform, rate=None, trigger=False, retrigger=1, packet_size=240, tf=0.5, verbose=False)[source]

Single- or multi-channel analog output scan.

To start the scan:

>>> run(n=0, thread=True|False)
  * n: output the waveform n times (0=infinity)
  * thread: whether to run in a background thread (call stop() to stop it)
    or in the main thread (Ctl+C to stop)
Parameters:
  • deviceUSB2600 instance
  • channels – tuple of integers indicatings the analog output channels.
  • waveform – a Waveform instance containing the data. Must have the same number of rows as channels
  • rate – sampling frequency (Hz). if not given, use waveform.data.
  • trigger – a Trigger instance to use as external trigger.
  • packet_size – number of bytes in each USB read operation.
  • tf – USB read time factor. If getting AO_FIFO_UNDERRUN error, try decreasing tf to push data to the FIFO faster.
  • verbose – if True, print info to console while scan is running.
get_waveform_data(waveform)[source]

Return a flattened and binary version of the waveform data.

start(repeat=1, continuous=False)[source]

Start the AO scan.

Parameters:
  • continuous – True for continuous operation (output until FIFO underruns). False to output a fixed number of samples, determined by the data waveform and the repeat parameter.
  • repeat – number of time to output the waveform. 0 if forever.
write(n=1)[source]

Write n packet of samples to the AO FIFO.

stop()[source]

Stop the AO scan.

clear()[source]

Clear the AO FIFO. Data will be lost!

print_status()[source]

Pretty-print scan status on a single line.

write_waveform_to_fifo(n=1)[source]

Write the whole waveform to FIFO n times (forever if n=0).

wait_until_output_done()[source]

Return after all samples have been written to output.

run(n=1, wait=False, thread=False)[source]

Start the scan.

Parameters:
  • n – number of waveform repeats. 0=forever.
  • thread – whether to run the scan in a background thread. if True, call stop() to interrupt the scan, other wise use Ctrl+C
  • wait – if True, block until the scan is complete. If False, return as soon as all data has been written to the FIFO.
fifo_underrun

Return True if the AO FIFO is empty.

running

Return True while samples are being output.

done

Return True once all samples have been ouput.

class mccdaq.usb2600.usb2600.AIO_Scan(device, AIchans, AOchans, AOwaveform, rate=None, packet_size=240, tf=0.5, verbose=False)[source]

Synchronous analog input and output scan.

To start the scan:

>>> run(thread=True|False)
  * thread: whether to run in a background thread (set running=False to stop it)
    or in the main thread (Ctl+C to stop).

The acquired analog input data is in the data attribute.

Parameters:
  • deviceUSB2600_Wrapper instance
  • AIchans – tuple of integers indicating the analog input channels.
  • AOchans – tuple of integers indicating the analog output channels.
  • AOwaveform – a Waveform instance containing the analog output data. Must have the same number of rows as AOchans
  • rate – sampling frequency (Hz). If not given, use AOwaveform.rate.
  • packet_size – number of bytes in each USB read/write operation.
  • tf – USB read time factor. If getting AO_FIFO_UNDERRUN or AI_FIFO_OVERRUN errors, try decreasing tf to pop/push data to the FIFO faster.
  • verbose – if True, print info to console while scan is running.
print_status()[source]

Pretty-print scan status to console.

stop()[source]

Stop the scan.

run(thread=False)[source]

Start the scan.

Parameters:thread – whether to run the scan in a background thread. if True, set running = False to interrupt the scan, otherwise use Ctrl+C