Export code from Excel 2003 to VB 2008

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • petro
    New Member
    • Sep 2009
    • 3

    Export code from Excel 2003 to VB 2008

    Hello,

    I have some trouble to export this code which works fine to VB 2008
    This code is intented to work with datalogger.
    Code:
    Declare Function pl1000CloseUnit Lib "pl1000.dll" (ByVal handle As Integer) As Long
    Declare Function pl1000GetUnitInfo Lib "pl1000.dll" (ByVal handle As Integer, ByVal S As String, ByVal lth As Integer, ByRef requiredSize As Integer, ByVal info As Integer) As Integer
    Declare Function pl1000SetTrigger Lib "pl1000.dll" (ByVal handle As Integer, ByVal enabled As Integer, ByVal enable_auto As Integer, ByVal auto_ms As Integer, ByVal channel As Integer, ByVal dir As Integer, ByVal threshold As Integer, ByVal hysterisis As Integer, ByVal delay As Single) As Integer
    Declare Function pl1000SetInterval Lib "pl1000.dll" (ByVal handle As Integer, ByRef us_for_block As Long, ByVal ideal_no_of_samples As Long, channels As Integer, ByVal No_of_channels As Integer) As Long
    Declare Function pl1000GetValues Lib "pl1000.dll" (ByVal handle As Integer, values As Integer, no_of_values As Long, overflow As Integer, triggerIndex As Long) As Long
    Declare Function pl1000Run Lib "pl1000.dll" (ByVal handle As Integer, ByVal no_of_values As Long, ByVal method As Integer) As Integer
    Declare Function pl1000Ready Lib "pl1000.dll" (ByVal handle As Integer, ByRef ready As Integer) As Long
    Declare Function pl1000MaxValue Lib "pl1000.dll" (ByVal handle As Integer, ByRef maxValue As Integer) As Long
    
    Dim status As Long
    Dim handle As Integer
    Dim values(200) As Integer
    Dim channels(22) As Integer
    Dim nValues As Long
    Dim ok As Integer
    Dim ready As Integer
    Dim requiredSize As Integer
    Dim S As String * 255
    Public port As Integer
    Public product As Integer
    Dim maxValue As Integer
    
    Function adc_to_mv(value As Integer) As Integer
      adc_to_mv = value / maxValue * 2500
    End Function
    
    Sub GetPl1000()
    
    ' Open device
        status = pl1000OpenUnit(handle)
        opened = handle <> 0
        
    If opened Then
    
      'Get the maximum ADC value for this variant
      status = pl1000MaxValue(handle, maxValue)
    
    ' Get the unit information
      Cells(6, "E").value = "Unit opened"
      SLegnth = pl1000GetUnitInfo(handle, S, 255, requiredSize, 3)
      Cells(7, "E").value = S
      SLegnth = pl1000GetUnitInfo(handle, S, 255, requiredSize, 4)
      Cells(8, "E").value = S
      SLegnth = pl1000GetUnitInfo(handle, S, 255, requiredSize, 1)
      Cells(9, "E").value = S
      
      ' No Trigger
      Call pl1000SetTrigger(handle, False, 0, 0, 0, 0, 0, 0, 0)
    
      ' Say that we want to take 100 readings in 1 s
      ' from channels 1 and 2
        nValues = 100
        channels(0) = 1
        channels(1) = 2
        Dim sampleInterval As Long
        Dim us_for_block As Long
        
        us_for_block = 1000000
        status = pl1000SetInterval(handle, us_for_block, nValues, channels(0), 2)
            
        status = pl1000Run(handle, nValues, 0)
        
        ready = 0
        Do While ready = 0
            status = pl1000Ready(handle, ready)
        Loop
    
      ' Get a block of 100 readings...
      ' we can call this routine repeatedly
      ' to get more blocks with the same settings
      Dim triggerIndex As Long
      Dim overflow As Integer
      status = pl1000GetValues(handle, values(0), nValues, overflow, triggerIndex)
      
      ' Copy the data into the spreadsheet
      For i = 0 To nValues - 1
         Cells(i + 4, "A").value = adc_to_mv(values(2 * i))
         Cells(i + 4, "B").value = adc_to_mv(values(2 * i + 1))
      Next i
    
      ' Close the unit when finished to drop the driver
      Call pl1000CloseUnit(handle)
      
    Else
      Cells(17, "E").value = "Unable to open unit"
    End If
      
    End Sub

    At line :
    Code:
     ready = 0
      Do While ready = 0
          status = pl1000Ready(handled, ready)
       Loop
    it looops for ever instead in Excel it work fine
    In order to export it to Vb 98 I have changed my declarations in

    Code:
    Declare Function pl1000OpenUnit Lib "pl1000.dll" (ByRef handle As Integer) As Long
        Declare Function pl1000CloseUnit Lib "pl1000.dll" (ByRef handle As Integer) As Long
        Declare Function pl1000GetUnitInfo Lib "pl1000.dll" (ByRef handle As Integer, ByRef S As String, ByRef lth As Integer, ByRef requiredSize As Integer, ByRef info As Integer) As Integer
        Declare Function pl1000SetTrigger Lib "pl1000.dll" (ByRef handle As Integer, ByRef enabled As Integer, ByRef enable_auto As Integer, ByRef auto_ms As Integer, ByRef channel As Integer, ByRef dir As Integer, ByRef threshold As Integer, ByRef hysterisis As Integer, ByRef delay As Single) As Integer
        Declare Function pl1000SetInterval Lib "pl1000.dll" (ByRef handle As Integer, ByRef us_for_block As Long, ByRef ideal_no_of_samples As Long, ByRef channels As Integer, ByRef No_of_channels As Integer) As Long
        Declare Function pl1000GetValues Lib "pl1000.dll" (ByRef handle As Integer, ByRef values As Integer, ByRef no_of_values As Long, ByRef overflow As Integer, ByRef triggerIndex As Long) As Long
        Declare Function pl1000Run Lib "pl1000.dll" (ByRef handle As Integer, ByRef no_of_values As Long, ByRef method As Integer) As Integer
        Declare Function pl1000Ready Lib "pl1000.dll" (ByRef handle As Integer, ByVal ready As Integer) As Long
        Declare Function pl1000MaxValue Lib "pl1000.dll" (ByRef handle As Integer, ByRef maxValue As Integer) As Long
    may be it is hard help me cause the software is too long , anyway i try to ask you
    Thanks in advance
  • petro
    New Member
    • Sep 2009
    • 3

    #2
    more info:

    That code works fine in Vb6 too
    There are some differences i dont know VBA Excel/Vb6 Vs VB2008

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      I have some trouble to export this code which works fine to VB 2008
      This code is intented to work with datalogger.
      Could you be a little more specific about the problem(s)? "Some trouble" is very vague.

      Comment

      • petro
        New Member
        • Sep 2009
        • 3

        #4
        Hello
        Declarations's module updated based on difference between vb6 and vb2008
        Code:
        Module Module1
            Declare Function pl1000OpenUnit Lib "pl1000.dll" (ByRef handle As Short) As Integer
            Declare Function pl1000CloseUnit Lib "pl1000.dll" (ByRef handle As Short) As Integer
            Declare Function pl1000GetUnitInfo Lib "pl1000.dll" (ByRef handle As Short, ByVal S As String, ByVal lth As Short, ByRef requiredSize As Short, ByVal info As Short) As Short
            Declare Function pl1000SetTrigger Lib "pl1000.dll" (ByRef handle As Short, ByVal enabled As Short, ByVal enable_auto As Short, ByVal auto_ms As Short, ByVal channel As Short, ByVal dir As Short, ByVal threshold As Short, ByVal hysterisis As Short, ByVal delay As Single) As Short
        
        Declare Function pl1000SetInterval Lib "pl1000.dll" (ByRef handle As Short, ByRef us_for_block As Integer, ByVal ideal_no_of_samples As Integer, ByVal channels As Short, ByVal No_of_channels As Short) As Integer
        
            Declare Function pl1000GetValues Lib "pl1000.dll" (ByRef handle As Short, ByVal values As Short, ByVal no_of_values As Integer, ByVal overflow As Short, ByVal triggerIndex As Integer) As Integer
            Declare Function pl1000Run Lib "pl1000.dll" (ByRef handle As Short, ByVal no_of_values As Integer, ByVal method As Short) As Short
            Declare Function pl1000Ready Lib "pl1000.dll" (ByVal handle As Short, ByRef ready As Short) As Integer
            Declare Function pl1000MaxValue Lib "pl1000.dll" (ByVal handle As Short, ByRef maxValue As Short) As Integer
        End Module
        FORM1:
        Code:
        Public Class Form1
         Dim status As Integer
            Dim handled As Short
            Dim opened As Short
            Dim values(200) As Short
            Dim channels(22) As Short
            Dim nValues As Integer
            Dim ok As Short
            Private ready As Short
            Dim requiredSize As Short
           
            Dim s As String     'in VB6 it was string*255
            Public port As Short
            Public product As Short
            Dim maxValue As Short
            Dim Slenght As String
        
         Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
            End Sub
        
            Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        
                ' Open device
                status = pl1000OpenUnit(handled)
                opened = handled <> 0
        
        
                If opened Then
        
                    status = pl1000MaxValue(handled, maxValue)
        
                    ' Slenght = pl1000GetUnitInfo(handled, s, 255, requiredSize, 3)
        
                    Call pl1000SetTrigger(handled, False, 0, 0, 0, 0, 0, 0, 0)
                    nValues = 100
                    channels(0) = 1
                    channels(1) = 2
        
                    Dim sampleInterval As Integer
                    Dim us_for_block As Integer
        
                    us_for_block = 1000000
                    status = pl1000SetInterval(handled, us_for_block, nValues, channels(0), 2)
        
                    status = pl1000Run(handled, nValues, 0)
        
        
                    ready = 0
                    Do While ready = 0
                        status = pl1000Ready(handled, ready)
                    Loop
        
                    MsgBox("ok")
                End If
            End Sub
        
            Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
                pl1000CloseUnit(handled)
        
            End Sub
        End Class
        The problem is this :
        At position
        Code:
                    ready = 0
                    Do While ready = 0
                        status = pl1000Ready(handled, ready)
                    Loop
        The var "ready" is always 0 (zero) so the loop is forever.
        This is wrong because the hardware works fine and the program works fine in vb/excel 2003 too.
        I dont know why it is always 0.
        This software comes with datalogger which works perfectly

        Follows the program for vb6/excel 2003:
        This woks fine in excel 2003/VB 6
        Code:
        Declare Function pl1000OpenUnit Lib "pl1000.dll" (ByRef handle As Integer) As Long
        Declare Function pl1000CloseUnit Lib "pl1000.dll" (ByVal handle As Integer) As Long
        Declare Function pl1000GetUnitInfo Lib "pl1000.dll" (ByVal handle As Integer, ByVal S As String, ByVal lth As Integer, ByRef requiredSize As Integer, ByVal info As Integer) As Integer
        Declare Function pl1000SetTrigger Lib "pl1000.dll" (ByVal handle As Integer, ByVal enabled As Integer, ByVal enable_auto As Integer, ByVal auto_ms As Integer, ByVal channel As Integer, ByVal dir As Integer, ByVal threshold As Integer, ByVal hysterisis As Integer, ByVal delay As Single) As Integer
        
        This declarations must be changed in "Byval" otherwhise i get an "Accessviolation exceptions.."
        [I]Declare Function pl1000SetInterval Lib "pl1000.dll" (ByVal handle As Integer, ByRef us_for_block As Long, ByVal ideal_no_of_samples As Long, channels As Integer, ByVal No_of_channels As Integer) As Long[/I]
        
        Declare Function pl1000GetValues Lib "pl1000.dll" (ByVal handle As Integer, values As Integer, no_of_values As Long, overflow As Integer, triggerIndex As Long) As Long
        Declare Function pl1000Run Lib "pl1000.dll" (ByVal handle As Integer, ByVal no_of_values As Long, ByVal method As Integer) As Integer
        Declare Function pl1000Ready Lib "pl1000.dll" (ByVal handle As Integer, ByRef ready As Integer) As Long
        Declare Function pl1000MaxValue Lib "pl1000.dll" (ByVal handle As Integer, ByRef maxValue As Integer) As Long
        
        Dim status As Long
        Dim handle As Integer
        Dim values(200) As Integer
        Dim channels(22) As Integer
        Dim nValues As Long
        Dim ok As Integer
        Dim ready As Integer
        Dim requiredSize As Integer
        Dim S As String * 255
        Public port As Integer
        Public product As Integer
        Dim maxValue As Integer
        
        Function adc_to_mv(value As Integer) As Integer
          adc_to_mv = value / maxValue * 2500
        End Function
        
        Sub GetPl1000()
        
        ' Open device
            status = pl1000OpenUnit(handle)
            opened = handle <> 0
            
        If opened Then
        
          'Get the maximum ADC value for this variant
          status = pl1000MaxValue(handle, maxValue)
        
        ' Get the unit information
          Cells(6, "E").value = "Unit opened"
          SLegnth = pl1000GetUnitInfo(handle, S, 255, requiredSize, 3)
          Cells(7, "E").value = S
          SLegnth = pl1000GetUnitInfo(handle, S, 255, requiredSize, 4)
          Cells(8, "E").value = S
          SLegnth = pl1000GetUnitInfo(handle, S, 255, requiredSize, 1)
          Cells(9, "E").value = S
          
          ' No Trigger
          Call pl1000SetTrigger(handle, False, 0, 0, 0, 0, 0, 0, 0)
        
          ' Say that we want to take 100 readings in 1 s
          ' from channels 1 and 2
            nValues = 100
            channels(0) = 1
            channels(1) = 2
            Dim sampleInterval As Long
            Dim us_for_block As Long
            
            us_for_block = 1000000
            status = pl1000SetInterval(handle, us_for_block, nValues, channels(0), 2)
                
            status = pl1000Run(handle, nValues, 0)
            
            ready = 0
            Do While ready = 0
                status = pl1000Ready(handle, ready)
            Loop
        
          ' Get a block of 100 readings...
          ' we can call this routine repeatedly
          ' to get more blocks with the same settings
          Dim triggerIndex As Long
          Dim overflow As Integer
          status = pl1000GetValues(handle, values(0), nValues, overflow, triggerIndex)
          
          ' Copy the data into the spreadsheet
          For i = 0 To nValues - 1
             Cells(i + 4, "A").value = adc_to_mv(values(2 * i))
             Cells(i + 4, "B").value = adc_to_mv(values(2 * i + 1))
          Next i
        
          ' Close the unit when finished to drop the driver
          Call pl1000CloseUnit(handle)
          
        Else
          Cells(17, "E").value = "Unable to open unit"
        End If
          
        End Sub
        Thank for any tips or help

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          The problem is this :
          At position
          Code:
                      ready = 0
                      Do While ready = 0
                          status = pl1000Ready(handled, ready)
                      Loop
          The var "ready" is always 0 (zero) so the loop is forever.
          Well... You set ready to zero, you then passed it to another method. But I see no place where you are trying to get that value back. I wouldn't have expected it to change value. Maybe I'm missing something.

          When you pass a value as a parameter the receiving method receives into a new variable. A copy of the value you send, if you will.

          I would expect you need to do something with the 'status' variable since this is returning results from the method you are calling (pl1000Ready)

          Comment

          Working...