DrDaq Access using VB

Post your VB and VBA discussions here
Post Reply
chrissalon
Newbie
Posts: 0
Joined: Sun Nov 15, 2015 1:49 pm

DrDaq Access using VB

Post by chrissalon »

Can anyone give me an example on how to read the USBDrDaq using VB6 or VB .NET to get me started (beginner).
Thanks for any guide.

Martyn
Site Admin
Site Admin
Posts: 4491
Joined: Fri Jun 10, 2011 8:15 am
Location: St. Neots

Re: DrDaq Access using VB

Post by Martyn »

We don't have a specific VB6 or VB.Net example, however there is an excel macro as shown belowavailable in the SDK which can be downloaded from our website by selecting PicoLog Data Loggers, which should help you to get going.

Code: Select all

Declare Function UsbDrDaqOpenUnit Lib "USBDrDAQ.dll" (ByRef handle As Integer) As Long
Declare Function UsbDrDaqCloseUnit Lib "USBDrDAQ.dll" (ByVal handle As Integer) As Long
Declare Function UsbDrDaqSetInterval Lib "USBDrDAQ.dll" (ByVal handle As Integer, ByRef us_for_block As Long, ByVal ideal_no_of_samples As Long, ByRef channels As Long, ByVal no_of_channels As Integer) As Long
Declare Function UsbDrDaqRun Lib "USBDrDAQ.dll" (ByVal handle As Integer, ByVal no_of_values As Long, ByVal method As Long) As Long
Declare Function UsbDrDaqGetValues Lib "USBDrDAQ.dll" (ByVal handle As Integer, ByRef values As Integer, ByRef noOfValues As Long, ByRef overflow As Integer, ByRef triggerIndex As Long) As Long
Declare Function UsbDrDaqReady Lib "USBDrDAQ.dll" (ByVal handle As Integer, ByRef ready As Integer) As Long
Declare Function UsbDrDaqGetUnitInfo Lib "USBDrDAQ.dll" (ByVal handle As Integer, ByVal S As String, ByVal stringLength As Integer, ByRef requiredSize As Integer, ByVal info As Long) As Long

Declare Function UsbDrDaqSetDO Lib "USBDrDAQ.dll" (ByVal handle As Integer, ByVal IOChannel As Long, ByVal value As Integer) As Long



Sub GetData()
    Dim handle As Integer
    Dim status As Long
    Dim channels(10) As Long
    Dim ready As Integer
    Dim overflow As Integer
    Dim triggerIndex As Long
    Dim nValues As Long
    Dim values(1000) As Integer
    Dim i As Integer
    Dim Row As Integer
    Dim requiredSize As Integer
    Dim S As String * 255
    
    Range("A1:Z65536").Clear

    status = UsbDrDaqOpenUnit(handle)
    
    If status <> 0 Then
        MsgBox "Unit not opened", vbOKOnly, "Error Message"
        Cells(11, "L").value = "DrDAQ not opened"
        Exit Sub
    End If
    
        
    ' Get the unit information
    Cells(10, "L").value = "Unit opened"
    SLegnth = UsbDrDaqGetUnitInfo(handle, S, 255, requiredSize, 3)
    Cells(11, "L").value = S
    SLegnth = UsbDrDaqGetUnitInfo(handle, S, 255, requiredSize, 4)
    Cells(12, "L").value = "Serial number:"
    Cells(12, "M").value = S
    SLegnth = UsbDrDaqGetUnitInfo(handle, S, 255, requiredSize, 0)
    Cells(13, "L").value = "Driver version:"
    Cells(13, "M").value = S
    
    'Set Digital output
    
    status = UsbDrDaqSetDO(handle, 1, 1)
    
    status = UsbDrDaqSetDO(handle, 1, 0)
    
    'Collect a 1 second block containing 100 samples on all channels
    
    channels(0) = 1
    channels(1) = 2
    channels(2) = 3
    channels(3) = 4
    channels(4) = 5
    channels(5) = 6
    channels(6) = 7
    channels(7) = 8
    channels(8) = 9
    channels(9) = 10
    
    status = UsbDrDaqSetInterval(handle, 1000000, 100, channels(0), 10)
    
    If status <> 0 Then
        MsgBox "Settings error", vbOKOnly, "Error Message"
        Call UsbDrDaqCloseUnit(handle)
        Exit Sub
    End If
    
    ready = 0
    status = UsbDrDaqRun(handle, 100, 0)
    
    If status <> 0 Then
        MsgBox "Run error", vbOKOnly, "Error Message"
        Call UsbDrDaqCloseUnit(handle)
        Exit Sub
    End If
    
    Do While ready = 0
        status = UsbDrDaqReady(handle, ready)
    Loop
    
    nValues = 100
    Call UsbDrDaqGetValues(handle, values(0), nValues, overflow, triggerIndex)
    
    Call UsbDrDaqCloseUnit(handle)
        
    'Display values
    
    Cells(1, "A").value = "Ext. 1"
    Cells(1, "B").value = "Ext. 2"
    Cells(1, "C").value = "Ext. 3"
    Cells(1, "D").value = "Scope"
    Cells(1, "E").value = "PH"
    Cells(1, "F").value = "Resistance"
    Cells(1, "G").value = "Light"
    Cells(1, "H").value = "Thermistor"
    Cells(1, "I").value = "Mic. wavform"
    Cells(1, "J").value = "Mic. Level"
    
    i = 0
    For Row = 2 To nValues + 1
     Cells(Row, "A").value = values(i)
     Cells(Row, "B").value = values(i + 1)
     Cells(Row, "C").value = values(i + 2)
     Cells(Row, "D").value = values(i + 3)
     Cells(Row, "E").value = values(i + 4)
     Cells(Row, "F").value = values(i + 5)
     Cells(Row, "G").value = values(i + 6)
     Cells(Row, "H").value = values(i + 7)
     Cells(Row, "I").value = values(i + 8)
     Cells(Row, "J").value = values(i + 9)
     i = i + 10
    Next Row
    
End Sub
This is available in the SDK which can be downloaded from our website by selecting PicoLog Data Loggers, and should help to get you going.
Martyn
Technical Support Manager

chrissalon
Newbie
Posts: 0
Joined: Sun Nov 15, 2015 1:49 pm

Re: DrDaq Access using VB

Post by chrissalon »

I have seen this example. I am not that experience yet to consider all the requirements to convert to a VB program, thus not sufficiently helpful for me to get started. I haven't learned C thus not helpful there. A am sure that it is not too much trouble to include an example in VB (not only excel) as there may be many other dedicated VB users out there who may benefit from this example and may promote the product further. After all this is an educational product at large. Many other similar DAQ products at this price range or lower include a VB example.

I got this same response 2 years ago when I requested help by email.

Thanks though for responding.

Martyn
Site Admin
Site Admin
Posts: 4491
Joined: Fri Jun 10, 2011 8:15 am
Location: St. Neots

Re: DrDaq Access using VB

Post by Martyn »

I have a C# example, see attached, but nothing for VB.Net

Tools exist which can produce VB.Net code from C# although I don't have time to try this at the moment
Attachments
DrDAQRemote_v1_1_0_1.zip
(21.14 KiB) Downloaded 765 times
Martyn
Technical Support Manager

chrissalon
Newbie
Posts: 0
Joined: Sun Nov 15, 2015 1:49 pm

Re: DrDaq Access using VB

Post by chrissalon »

Thanks Martyn for giving some hope of conversion to VB. As I said earlier I only learned VB6, and the new versions have become too complex to just do simple tasks. For this reason I have moved to Xojo which is compatible with VB but only less complex and easy to program. I can interface with many such devices provided you know the proper instructions and declares and DLLs provided. I have tried it with Xojo and got response of recognition for DrDAQ but don't know the sequence of how to read single channels and proper declares. I only copied the excel ones and test to see if DrDaq is opened and worked ok. How do you declare, read a channel and place the result on a textbox? Excel VBA reads all channels using arrays and puts them on an array of cells making it less simple to understand for a beginner. I was loosing hope and was looking at other devices like Labjack U3-HV as they include VB examples and even Xojo conversion. Hopefully I can get my basic needs from DrDaq. Any programs that I develop with Xojo that may work with DrDaq I will be happy to provide it to this forum for possible use by others.

Kind regards
Chris

Martyn
Site Admin
Site Admin
Posts: 4491
Joined: Fri Jun 10, 2011 8:15 am
Location: St. Neots

Re: DrDaq Access using VB

Post by Martyn »

If you are able to open the DrDaq using OpenUnit from Xojo, then that is a good step forward.

I would concentrate on the first part of reading the device info and putting this into text boxes

Code: Select all

Declare Function UsbDrDaqOpenUnit Lib "USBDrDAQ.dll" (ByRef handle As Integer) As Long
Declare Function UsbDrDaqCloseUnit Lib "USBDrDAQ.dll" (ByVal handle As Integer) As Long
Declare Function UsbDrDaqGetUnitInfo Lib "USBDrDAQ.dll" (ByVal handle As Integer, ByVal S As String, ByVal stringLength As Integer, ByRef requiredSize As Integer, ByVal info As Long) As Long

    Dim handle As Integer
    Dim status As Long
    Dim S As String * 255
    
   status = UsbDrDaqOpenUnit(handle)
    
    If status <> 0 Then
        MsgBox "Unit not opened", vbOKOnly, "Error Message"
        Cells(11, "L").value = "DrDAQ not opened"
        Exit Sub
    End If
    
        
    ' Get the unit information
    Cells(10, "L").value = "Unit opened"
    SLegnth = UsbDrDaqGetUnitInfo(handle, S, 255, requiredSize, 3)
    Cells(11, "L").value = S
    SLegnth = UsbDrDaqGetUnitInfo(handle, S, 255, requiredSize, 4)
    Cells(12, "L").value = "Serial number:"
    Cells(12, "M").value = S
    SLegnth = UsbDrDaqGetUnitInfo(handle, S, 255, requiredSize, 0)
    Cells(13, "L").value = "Driver version:"
    Cells(13, "M").value = S
With the VBa code it puts data directly into cells on the spreadsheet. If you think of these as text boxes then converting to Xojo would just mean changing the lines like

Code: Select all

Cells(11, "L").value = S
to something like

Code: Select all

TextField1.Text = S
Martyn
Technical Support Manager

chrissalon
Newbie
Posts: 0
Joined: Sun Nov 15, 2015 1:49 pm

Re: DrDaq Access using VB

Post by chrissalon »

Hi Martyn,
Thanks for guiding me through this. My programming intention is to continue with Xojo ( found it more fun for my age).
I know most of the basics of Xojo so user interface issues are not a problem. I have managed to output the status open and close from the declares but had to change the Long to int32 and worked ok.
When it came to the declare for UnitInfo I had problems with the conversions-

Declare Function UsbDrDaqGetUnitInfo Lib "USBDrDAQ.dll" (ByVal handle As Integer, ByVal S As String, ByVal stringLength As Integer, ByRef requiredSize As Integer, ByVal info As Int32) As Int32

The int32 is ok as it worked for the other declares.

The S As String gave an error -Exernal Function cannot use ordinary strings as parameters use CString, PString,WString or CFStringRef instead.

Dim S As String*255 gave a syntax error.

requiredSize in the SLegnth = UsbDrDaqGetUnitInfo(handle, S, 255, requiredSize, 3) statement says this item does not exist.

I have tried various conversions with no success.

If you can give me the code to just output the temperature channel to the textfield1.text it will help me to see if DrDAQ will work with Xojo for now and I will try the harder conversions down the track.
Maybe other Xojo programmers may become interested in Xojo if I end up getting some results. I don't want to give up too easily.

Thanks again,
chrissalon

chrissalon
Newbie
Posts: 0
Joined: Sun Nov 15, 2015 1:49 pm

Re: DrDaq Access using VB

Post by chrissalon »

Hi Martyn,
Thanks to your tips I had some progress with accessing DrDAQ with Xojo. I am able to get values from all the channels including the DO 1 to 4. I am still having a problem with getting the device info as Xojo declares with string are slightly different but I will get there. I mow trying to control the RGB LED. I need you help if you could to see what I am doing wrong.
All the declares so far are not a problem and work OK. Here is the code for the RGB LED which although don't give me any errors the LED does not turn blue. Am I declare it wrong? If so How should it be?
When the application is significantly developed I will post it so others may find it useful. Xojo can be downloaded free from http://www.xojo.com/download/ good for education, only pay for licence if one wants to compile the program.

Here is my code to control the RGB LED. Executes with no errors but LRD does not turn Blue. Am I doing anything wrong?

Thanks for your continuing support. Now I am having fun with my DrDAQ unit.
chrissalon.

chrissalon
Newbie
Posts: 0
Joined: Sun Nov 15, 2015 1:49 pm

Re: DrDaq Access using VB

Post by chrissalon »

Hi Martyn,

Forgot to paste the code here it is

Declare Function UsbDrDaqOpenUnit Lib "USBDrDAQ.dll" (ByRef handle As Integer) As Int16
Declare Function UsbDrDaqCloseUnit Lib "USBDrDAQ.dll" (ByVal handle As Integer) As Int16
Declare Function UsbDrDaqEnableRGBLED Lib "USBDrDAQ.dll" (ByVal handle As Integer, ByVal enabled As Int16) As Int16
Declare Function UsbDrDaqSetRGBLED Lib "USBDrDAQ.dll" (ByVal handle As Integer, ByVal red As Int16, ByVal green As Int16, ByVal blue As Int16) As Int16

Dim handle As Integer
Dim status As Int32
Dim enabled As Int16
Dim red As Int16
Dim green As Int16
Dim blue As Int16

status = UsbDrDaqOpenUnit(handle)

If status <> 0 Then
MsgBox ("Unit not opened")
TextField2.Text = "DrDAQ not opened"
Else
TextField2.Text = "DrDAQ is opened"
End If

Call UsbDrDaqEnableRGBLED(handle, 1)
Call UsbDrDaqSetRGBLED(handle, 0, 0, 255)

Call UsbDrDaqCloseUnit(handle)

Martyn
Site Admin
Site Admin
Posts: 4491
Joined: Fri Jun 10, 2011 8:15 am
Location: St. Neots

Re: DrDaq Access using VB

Post by Martyn »

When you close the unit the LED will revert to the default state, can you pause after the set statement and see what happens.
Martyn
Technical Support Manager

chrissalon
Newbie
Posts: 0
Joined: Sun Nov 15, 2015 1:49 pm

Re: DrDaq Access using VB

Post by chrissalon »

Thanks Martyn that works. The DO stay on thus can switch on and off. How can I keep the RGBLED on so I can use a scrollbar to vary the colour? This is done using the Pico scope. It will be a good demonstration in may example App.
Currently I used a loop to change from 0 to 255 for the red, green and blue which is not much of a user control.
Pleased to see the functions are working so far.

Martyn
Site Admin
Site Admin
Posts: 4491
Joined: Fri Jun 10, 2011 8:15 am
Location: St. Neots

Re: DrDaq Access using VB

Post by Martyn »

If XOJO has a slider object, create one for RED, one for GREEN, and one for BLUE, and use these to set a value between 0 and 255. Then in your loop you would need to read the values of these sliders and pass them into the UsbDrDaqSetRGBLED call.
Martyn
Technical Support Manager

chrissalon
Newbie
Posts: 0
Joined: Sun Nov 15, 2015 1:49 pm

Re: DrDaq Access using VB

Post by chrissalon »

Thanks to Martyn (Above) and John A from Xojo forum I got the help to convert the excel VBA to Xojo (VB6 alternative) https://www.xojo.com and attached an example if anyone is interested. Xojo is free if you don't want to compile the program. Keep both USBDrDAQ.dll and USBDrDAQExample.xojo_binary_project in the same directory for the program to work and DrDAQ connected via USB before running the program. Maybe others may want to add further features.
Attachments
DrDAQ.zip
(189.64 KiB) Downloaded 731 times

Post Reply