Using ctypes with National Instruments nilibddc.dll

aewindhorn at gmail.com aewindhorn at gmail.com
Thu Mar 26 22:25:33 CET 2009


I would like to use Python ctypes with this "DiaDEM Connectivity
Library", but it only has examples for C and Matlab.  I have got a
couple of things to work by trial and error, but it is slow.  Has
anyone here used this library with Python?

Here's what I have so far:

--------------------------------------------------
# Module to handle *.tdm and *.tdms National Instruments TDM
# format files
#
from ctypes import *

#Define short names for required dll functions
DDCOpen = windll.nilibddc.DDC_OpenFile
DDCNumGroups = windll.nilibddc.DDC_GetNumChannelGroups
DDCReadGroups = windll.nilibddc.DDC_GetChannelGroups
DDCGroupStrPropLen =
windll.nilibddc.DDC_GetChannelGroupStringPropertyLength
DDCGroupProperty = windll.nilibddc.DDC_GetChannelGroupProperty

# File handles are pointers to 32 bit integer
czero = c_int(0)
FileHandle = pointer(czero)

# Retrieve a handle to the file
DDCErrCode = DDCOpen('Test_0919.tdms', 'TDMS', FileHandle)
print(DDCErrCode)  # Should be zero
myfile = FileHandle.contents  #The actual file handle

# Find number of groups
grpnum = c_int(0)
numptr = pointer(grpnum)
DDCErrCode = DDCNumGroups(myfile, numptr)
print(DDCErrCode)
# if DDCErrCode == 0: print(numptr.contents, grpnum)

# Read the groups  [UNTESTED]
groups = c_int(0)*grpnum
grpptr = pointer(groups)  # Needs to be a pointer to memory to hold
grpnum
                          #   of DDCChannelGrouphandle fields
DDCErrCode = DDCReadGroups(myfile, groups, grpnum)
print(DDCErrCode)
if DDCErrCode == 0: print(groups)

# Length of group name  [UNTESTED]
namelen = c_int(0)
nlenptr = pointer(name)
DDCErrCode = DDCGroupStrPropLen(groups[0], 'name', namelen)
print(DDCErrCode)
if DDCErrCode == 0: print(namelen)

#Make a buffer to hold the name, then retrieve it  [UNTESTED]
grpname = create_string_buffer(namelen+1)
nameptr = pointer(grpname)
DDCErrCode = DDCGroupProperty(groups[0], 'name', nameptr, namelen+1)
print(DDCErrCode)
if DDCErrCode == 0: print(grpname.value)

# Read the channels in the group and get the names  [UNTESTED]

# Read the channel # of data values  [UNTESTED]

# Read the channel data values  [UNTESTED]

# Process the data values  [UNTESTED]

# Write the processed data to a file in a different format  [UNTESTED]
--------------------------------------------------

The "UNTESTED" sections are pseudocode of a sort.  I have figured out
how to get back (integer) file and object pointers, but I don't know
yet how to get a pointer to an array of strings, which is the next
step.

I can supply a copy of the library if you want to try it, or it is
available from ni.com.  Thanks in advance for any help.

Regards,
Allen
--
Allen Windhorn, P.E. (MN)  (507) 345-2782
Kato Engineering
P.O. Box 8447, N. Mankato, MN  56002



More information about the Python-list mailing list