Using Python and the AduHid DLL module with ADU USB Data Acquisition Products (Windows)

View the ADU series of USB based Data Acquisition Products


Introduction


Communicating with USB devices via software involves a few simple steps. Unlike RS232 based devices which are connected to physical COM ports, USB devices are assigned a logical handle by operating systems when they are first plugged in. This process is known as enumeration. Once a USB device has been enumerated, it is ready for use by the host computer software. For the host application software to communicate with the USB device, it must first obtain the handle assigned to the USB device during the enumeration process. The handle can be obtained using an open function along with some specific information about the USB device. Information that can be used to obtain a handle to a USB device include, serial number, product ID, or vendor ID.

Once we obtain a USB device handle, we can read and write information to and from the USB device via our application. Once the application has finished with all communication with the USB device, the handle is closed. The handle is generally closed when the application terminates.

The sample source code outlines the basics of communicating directly with an ADU device on Windows using Python and the AduHid DLL module. Basics of opening a USB device handle, writing and reading data, as well as closing the handle is provided as an example. The aduhid module calls functions from AduHid.dll in order to interface with the devices.

All source code is provided so that you may review details that are not highlighted here.


Lets have a look at the code......


After importing the aduhid module, we can open an ADU device by product id or serial number via aduhid.open_device_by_product_id() or aduhid.open_device_by_serial_number().


from ontrak import aduhid

PRODUCT_ID = 200 # Set the product id to match your ADU device. See list here: https://www.ontrak.net/Nodll.htm

# open device by product id
device_handle = aduhid.open_device_by_product_id(PRODUCT_ID, 100)
if device_handle == None:
	print('Error opening device. Ensure that the product id is correct and that it is connected')
	exit(-1)

Now that we have successfully opened our device, we can write commands to the ADU device and read the result.

First, let's write some commands:


# Write a command to the device
result = aduhid.write_device(device_handle, 'RK0', 100)
print('Write result: %i' % result) # Should be non-zero if successful

result = aduhid.write_device(device_handle, 'SK0', 100)
print('Write result: %i' % result) # Should be non-zero if successful

In order to read from the ADU device, we can send a command that requests a return value (as defined in our product documentation). Such a command for the ADU200 is RPA. This requests the value of PORT A in binary format.

We can then use read_from_adu() to read the result. read_from_adu() returns the data read in string format on success, and None on failure. A timeout is supplied for the maximum amount of time that the host (computer) will wait for data from the read request.


result = aduhid.write_device(device_handle, 'RPK0', 100)
print('Write result: %i' % result) # Should be non-zero if successful

# Read from device
(result, value) = aduhid.read_device(device_handle, 100)	

# Result should non-zero if successful, value will contain the returned value from the device in integer form 
# If read is not successful, result is 0 and value is None
if result != 0:
	print('Read result: %i, value: %i' % (result, value)) 
else:
	print('Read failed - was a resulting command issued prior to the read?')

When finished communicating with the device it should be closed. This is generally donw when the application is closed.  IMPORTANT: In Windows USB devices with no handle open will be forced into suspend mode afer a few seconds.


aduhid.close_device(device_handle)

Further Details


If you would like to obtain the vendor id, product id and serial number belonging to each connected ADU, the aduhid.device_list() function may be used:


# Get a device list of connected ADUs. List will be empty if no devices are connected.
device_list = aduhid.device_list(100)
for device in device_list:
	print('Vendor ID: %i, Product ID: %i, Serial Number: %s' % (device.vendor_id, device.product_id, device.serial_number))

Download Python DLL Module Example File in ZIP Format