Test and Measurement Forum

Declaration of dll subs and functions for TH03

Post your .Net discussions here

Declaration of dll subs and functions for TH03

Postby airalsie » Thu Mar 15, 2007 4:03 am

Does anybody have succesfully converted the VB6 declaration of the dll to .Net?

I've written the below code but when executing the th03_get_temp method I don't get a value ("ok" = false), and after exactly two calls to the method I get an error about "attempting to read or write in protected memory". The init and setup of the ports works without error.

Declare Function th03_open_unit Lib "th0332.DLL" (ByVal Port As Int16) As Boolean
Declare Sub th03_close_unit Lib "th0332.DLL" (ByVal Port As Int16)
Declare Sub th03_set_channel Lib "th0332.DLL" (ByVal Port As Int16, ByVal channel As Int16, ByVal THtype As Int16, ByVal Factor As Int16)
Declare Sub th03_poll_driver Lib "th0332.DLL" ()
Declare Function th03_get_temp Lib "th0332.DLL" (ByVal value As Int32, ByVal Port As Int16, ByVal channel As Int16, ByVal filtered As Boolean) As Boolean
Declare Function th03_set_ref_update Lib "th0332.DLL" (ByVal Port As Int16, ByVal Rate As Int16) As Boolean
Declare Function th03_get_version Lib "th0332.DLL" (ByVal value As Int16, ByVal Port As Int16) As Boolean
Declare Function th03_get_cycle Lib "th0332.DLL" (ByVal cycle As Int32, ByVal Port As Int16) As Boolean

Declare Function GetTickCount Lib "kernel32" () As Int32

Dim Port As Int16
Dim version As Int16
Dim temperature As Int32
Dim ok As Boolean
Dim Ticks As Long

Sub GetData()
Port = 1
Log("Opening the TH03 on COM" & Port)
ok = th03_open_unit(Port)
If ok Then
Log("TH03 opened")
Else
Log("Cannot open the TH03")
Exit Sub
End If

' Set Channels 1 and 2 for a standard thermistor (low temp), filter factor 10
Call th03_set_channel(Port, 1, 1, 10)
Call th03_set_channel(Port, 2, 1, 10)

' Get 20 readings at 1-second intervals
For i As Int16 = 1 To 20
Ticks = GetTickCount()
While GetTickCount() < Ticks + 1000
Application.DoEvents()
th03_poll_driver()
End While

' Get the reading
ok = th03_get_temp(temperature, Port, 1, False)
If ok Then
Log(temperature / 100)
Else
Log("****")
End If

ok = th03_get_temp(temperature, Port, 2, False)
If ok Then
Log(temperature / 100)
Else
Log("****")
End If
Next i

' Close the driver
Call th03_close_unit(Port)
End Sub

Private Sub Log(ByVal str As String)
'
End Sub





PS. I'm successfully running the Excel demo app (but this is VBA6 code anyway).

Regards,
airalsie
User
User
 
Posts: 2
Joined: Thu Mar 15, 2007 3:54 am

Postby markB » Tue Mar 27, 2007 10:17 am

You need to pass 'value' in such a way that the called procedure can change the value of a variable underlying the argument (or as a pointer in C):

Declare Function th03_get_temp Lib "th0332.DLL" (ByRef value As Int32, ByVal Port As Int16, ByVal channel As Int16, ByVal filtered As Boolean) As Boolean
markB
Site Admin
Site Admin
 
Posts: 83
Joined: Tue Mar 27, 2007 9:43 am
Location: Cambridgeshire,UK

Thanks!

Postby airalsie » Tue Mar 27, 2007 11:27 am

Of course! It solved the issue and now it's working.
airalsie
User
User
 
Posts: 2
Joined: Thu Mar 15, 2007 3:54 am


Return to .Net Languages (C#, VB.Net, .)

Who is online

Users browsing this forum: No registered users and 1 guest