Measurement Studio for VB6

cancel
Showing results for 
Search instead for 
Did you mean: 

Reading TDMS 2.0 files with VBA

Hello, refering to https://forums.ni.com/t5/Measurement-Studio-for-VB6/Read-Write-tdm-and-tdms-in-VB6/m-p/636902

I'm attempting something similair but in VBA Excel 32bit. 

 

I want to read data in TDMS 2.0 files however i'm unsure of the best approach to do this in visual basic. I am aware of the TDM Excel add in, however this process is too slow for the application needed since some TDMS files we have are over 60mb.

I'm trying to use the cvitdms.dll with TDM Streaming library functions without much luck other then being able to crash my excel when i simply want to get som channels for a channelgroup

 

here is my current module, hoping someone with better knowledge then me can help me in the right direction.


Option Explicit
'-- Win32 APIs for TDMS
Private Declare Function TDMS_OpenFile Lib "cvitdms" (ByVal FilePath As String, ByVal readOnly As Long, ByRef fileHandle As Long) As Long
Private Declare Function TDMS_GetChannels Lib "cvitdms" (ByVal channelGroup As Long, ByRef channels As Long, ByRef numChannels As Long) As Long
Private Declare Function TDMS_GetChannelGroupByName Lib "cvitdms" (ByVal fileHandle As Long, ByVal groupName As String, ByRef groupHandle As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef Destination As Any, ByVal Source As Long, ByVal Length As Long)
Private Declare Function TDMS_CloseFile Lib "cvitdms" (ByVal fileHandle As Long) As Long

Sub GetTDMSInfo()
Dim fileHandle As Long
Dim errorCode As Long
Dim FilePath As String
Dim InstVal As String
Dim channelGroupHandle As Long
Dim channelsPtr As Long
Dim numChannels As Long
Dim channelHandle As Long
Dim i As Long

' Declarations
InstVal = "InstActualValues"
FilePath = "C:\temp\PTQ4932_9594054_MCP_ProcessLog_20250702.tdms"

' Open the TDMS file
errorCode = TDMS_OpenFile(FilePath, 0, fileHandle)
If errorCode < 0 Or fileHandle = 0 Then
Debug.Print "Error opening file: "; errorCode
Exit Sub
End If

' Get channel group by name
errorCode = TDMS_GetChannelGroupByName(fileHandle, InstVal, channelGroupHandle)
If errorCode < 0 Then
Debug.Print "Error getting group by name: "; errorCode
TDMS_CloseFile fileHandle
Exit Sub
End If

Debug.Print "File Handle: "; fileHandle

' Get channels for the group
errorCode = TDMS_GetChannels(channelGroupHandle, channelsPtr, numChannels)
If errorCode < 0 Then
Debug.Print "Error getting channels: "; errorCode
TDMS_CloseFile fileHandle
Exit Sub
End If

Debug.Print "Found "; numChannels; " channels"
If numChannels > 0 And channelsPtr <> 0 Then
For i = 0 To numChannels - 1
CopyMemory channelHandle, ByVal (channelsPtr + i * 4), 4
Debug.Print "Channel "; i + 1; ": Handle = "; channelHandle
Next i
End If

' Clean up
TDMS_CloseFile fileHandle
End Sub

0 Kudos
Message 1 of 1
(103 Views)