forked from awendt/pyrelacs
87 lines
2.3 KiB
Python
87 lines
2.3 KiB
Python
import ctypes
|
|
|
|
import uldaq
|
|
from IPython import embed
|
|
import matplotlib.pyplot as plt
|
|
import numpy as np
|
|
|
|
|
|
from pyrelacs.util.logging import config_logging
|
|
|
|
log = config_logging()
|
|
|
|
|
|
class ReadWrite:
|
|
def __init__(self) -> None:
|
|
devices = uldaq.get_daq_device_inventory(uldaq.InterfaceType.USB)
|
|
log.debug(f"Found daq devices {len(devices)}, connecting to the first one")
|
|
self.daq_device = uldaq.DaqDevice(devices[0])
|
|
self.daq_device.connect()
|
|
log.debug("Connected")
|
|
# self.daq_device.enable_event(
|
|
# uldaq.DaqEventType.ON_DATA_AVAILABLE,
|
|
# 1,
|
|
# self.read_write,
|
|
# (uldaq.DaqEventType.ON_DATA_AVAILABLE, 1, 1),
|
|
# )
|
|
|
|
def read_write(self) -> None:
|
|
# event_type = callback_args.event_type
|
|
# event_data = callback_args.event_data
|
|
# user_data = callback_args.user_data
|
|
|
|
FS = 30_000.0
|
|
DURATION = 10
|
|
FREQUENCY = 100
|
|
|
|
time = np.arange(0, DURATION, 1 / FS)
|
|
data = 2 * np.sin(2 * np.pi * FREQUENCY * time)
|
|
|
|
buffer = ctypes.c_double * len(time)
|
|
data_c = buffer(*data)
|
|
buf = uldaq.create_float_buffer(1, len(time))
|
|
|
|
# Get the Ananlog In device and Analog Info
|
|
ai_device = self.daq_device.get_ai_device()
|
|
ai_info = ai_device.get_info()
|
|
|
|
# Get the Analog Out device
|
|
ao_device = self.daq_device.get_ao_device()
|
|
ao_info = ao_device.get_info()
|
|
|
|
er_ao = ao_device.a_out_scan(
|
|
0,
|
|
0,
|
|
uldaq.Range.BIP10VOLTS,
|
|
int(len(data)),
|
|
30_000.0,
|
|
uldaq.ScanOption.DEFAULTIO,
|
|
uldaq.AOutScanFlag.DEFAULT,
|
|
data_c,
|
|
)
|
|
|
|
er_ai = ai_device.a_in_scan(
|
|
1,
|
|
1,
|
|
uldaq.AiInputMode.SINGLE_ENDED,
|
|
uldaq.Range.BIP10VOLTS,
|
|
len(time),
|
|
FS,
|
|
uldaq.ScanOption.DEFAULTIO,
|
|
uldaq.AInScanFlag.DEFAULT,
|
|
data=buf,
|
|
)
|
|
ai_device.scan_wait(uldaq.WaitType.WAIT_UNTIL_DONE, timeout=-1)
|
|
log.debug("Scanning")
|
|
|
|
self.daq_device.disconnect()
|
|
self.daq_device.release()
|
|
plt.plot(buf)
|
|
plt.plot(data_c)
|
|
plt.show()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
daq_input = ReadWrite()
|
|
daq_input.read_write()
|