Examples
This page contains practical examples demonstrating common usage patterns with the DeviceAccess Python bindings. The actual example code, and the map and dmap files used in this example can be found in the tests folder of the source distribution. The content is also listed in the Used Map Files section section below.
Basic Scalar Register Access
Reading and writing a single register value:
import deviceaccess as da
da.setDMapFilePath("documentationExamples/someCrate.dmap")
device = da.Device("someDummyDevice")
device.open()
# Read a value
temperature = device.getScalarRegisterAccessor(float, "SENSORS.TEMPERATURE")
temperature.read()
print(f"Current temperature: {float(temperature)} °C")
# Write a value
setpoint = device.getScalarRegisterAccessor(float, "SENSORS.SET_POINT")
setpoint.set(25.0)
setpoint.write()
# Verify the write
setpoint.read()
print(f"Setpoint is now: {float(setpoint)} °C")
Working with 1D Accessors
Reading and processing array data:
import deviceaccess as da
import numpy as np
da.setDMapFilePath("documentationExamples/someCrate.dmap")
device = da.Device("someDummyDevice")
device.open()
# Get 1D accessor
waveform = device.getOneDRegisterAccessor(int, "SENSORS.WAVEFORM")
waveform.read()
# Access like a list:
[print(f"Waveform element {i}: {waveform[i]}") for i in range(len(waveform))]
# Scale all values by a factor of 2
waveform *= 2
# Write back the modified waveform
waveform.write()
# Read back to verify
waveform.read()
print("Modified waveform data:", waveform) # accessor can be used as a numpy array or python list
# slicing also works
print("First 3 elements:", waveform[:3])
print("Last 2 elements:", waveform[-2:])
print("Elements 2 to 4:", waveform[1:4])
print("Every other element:", waveform[::2])
Using Different Device Backends
The ChimeraTK library supports multiple backends. Configure them in your device map file:
# Device map example
AMC_PCIe (xdma:xdma/slot6?map=device.map)
SCPI_Dev (CommandBasedTCP:lab_dev?map=hw_prep.json&port=50000)
OPCUADev (opcua:192.168.1.101?port=16664)
DummyDev (dummy?map=device.map)
Then use them the same way in your Python code:
import deviceaccess
# Open different backend devices with same API
dummy = deviceaccess.Device("DummyDev")
pcie = deviceaccess.Device("AMC_PCIe")
scpi = deviceaccess.Device("SCPI_Dev")
opcua = deviceaccess.Device("OPCUADev")
# All use the same accessor interface
for device in [dummy, pcie, scpi, opcua]:
value = device.getScalarRegisterAccessor(int, "MEASUREMENT")
value.read()
print(f"Value: {float(value)}")
Used Map Files
Example Crate dMap File
# DEVICE_LABEL backend_specification
someDummyDevice (dummy?map=./someDummyModule.map)
someSharedDummyDevice (sharedMemoryDummy?map=someDummyModule.map)
Example Module Map File
# name nr of elements address size bar width fracbits signed access
SENSORS.TEMPERATURE 0x01 0x00000000 0x00000004 0x0 32 4 1 RW
SENSORS.SET_POINT 0x01 0x00000004 0x00000004 0x0 32 4 1 RW
SENSORS.WAVEFORM 0x08 0x00000008 0x00000020 0x0 32 0 1 RW
See Also
User Guide for in-depth explanations
API Reference for complete API documentation
Frequently Asked Questions for common questions