使用vba将picologger记录到excel

使用vba将picologger记录到excel,excel,vba,Excel,Vba,我有一个picolog 1012数据记录器,我必须在上面采集大量数据。默认的软件使用起来非常不方便,PicoTech提供了一些简单的方法,允许在excel中记录数据。不幸的是,它只记录来自两个通道的数据,我有九个通道 我已经成功地调整了日志记录间隔和时间,但无法获得正确工作的通道数 ' Excel Macro for the PicoLog 1000 series ' Pico Technology 2009 Declare Function pl1000OpenUnit Lib "pl100

我有一个picolog 1012数据记录器,我必须在上面采集大量数据。默认的软件使用起来非常不方便,PicoTech提供了一些简单的方法,允许在excel中记录数据。不幸的是,它只记录来自两个通道的数据,我有九个通道

我已经成功地调整了日志记录间隔和时间,但无法获得正确工作的通道数

' Excel Macro for the PicoLog 1000 series
' Pico Technology 2009

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
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, 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" (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(9, "p").value = "Unit opened"
  SLegnth = pl1000GetUnitInfo(handle, S, 255, requiredSize, 3)
  Cells(10, "P").value = S
  SLegnth = pl1000GetUnitInfo(handle, S, 255, requiredSize, 4)
  Cells(11, "P").value = S
  SLegnth = pl1000GetUnitInfo(handle, S, 255, requiredSize, 1)
  Cells(12, "P").value = S

  ' No Trigger
  Call pl1000SetTrigger(handle, False, 0, 0, 0, 0, 0, 0, 0)

  ' Say that we want to take [W3] readings in [W4] s
  ' from channels 1 and 2

    'Set number of samples to read
    Dim samplenum As Integer
    samplenum = Worksheets("Sheet1").Range("W3").value 'Reads number of samples from W3

    nValues = samplenum
    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


    'Set test length
    Dim sampleInterval As Long
    Dim microsecs_for_block As Long

    Dim testlength As Integer
    testlength = Worksheets("Sheet1").Range("$w$4").value
    microsecs_for_block = testlength * 1000000
    status = pl1000SetInterval(handle, microsecs_for_block, nValues, channels(0), 2)''<changing the 2 to a nine really made the data go odd>

    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)) '''<I am not sure what this command is doing so I have tried simply expanding it into other columns. The sheet does add data into cells C-E but it appears to be duplicate data from A and B>
     Cells(i + 4, "C").value = adc_to_mv(values(2 * i + 2))
     Cells(i + 4, "D").value = adc_to_mv(values(2 * i + 3))
     Cells(i + 4, "E").value = adc_to_mv(values(2 * i + 4))
  Next i

  ' Close the unit when finished to drop the driver
  Call pl1000CloseUnit(handle)

Else
   MsgBox "Unable to open device", vbCritical
End If

End Sub
PicoLog 1000系列的Excel宏 “Pico Technology 2009 将函数pl1000OpenUnit Lib“pl1000.dll”(ByRef句柄为整数)声明为 将函数pl1000CloseUnit Lib“pl1000.dll”(ByVal句柄为整数)声明为 将函数pl1000GetUnitInfo Lib“pl1000.dll”(ByVal句柄为整数,ByVal S为字符串,ByVal lth为整数,ByRef requiredSize为整数,ByVal info为整数)声明为整数 将函数pl1000SetTrigger Lib“pl1000.dll”(ByVal句柄为整数,ByVal enabled为整数,ByVal enable_auto为整数,ByVal auto_ms为整数,ByVal channel为整数,ByVal dir为整数,ByVal thresholds为整数,ByVal delay为单)声明为整数 将函数pl1000SetInterval Lib“pl1000.dll”(ByVal句柄为整数,ByRef us_表示_块为长,ByVal理想_no_表示_样本为长,通道为整数,ByVal no_表示_通道为整数)声明为长 将函数pl1000GetValues Lib“pl1000.dll”(ByVal句柄为整数,ByRef值为整数,ByRef no_of_值为长,ByRef溢出为整数,ByRef triggerIndex为长)声明为长 将函数pl1000Run Lib“pl1000.dll”(ByVal句柄为整数,ByVal no_的_值为长,ByVal方法为整数)声明为整数 将函数pl1000Ready Lib“pl1000.dll”(ByVal句柄为整数,ByRef ready为整数)声明为Long 将函数pl1000MaxValue Lib“pl1000.dll”(ByVal句柄为整数,ByRef maxValue为整数)声明为长 暗淡的状态 作为整数的Dim句柄 整数值(200) 将通道(22)变暗为整数 暗淡的n值等于长的值 Dim ok作为整数 Dim ready作为整数 Dim requiredSize为整数 将S设置为字符串*255 作为整数的公共端口 作为整数的公共产品 将最大值设置为整数 函数adc_到_mv(值为整数)为整数 adc_至_mv=值/最大值*2500 端函数 Sub GetPl1000() “开放式设备 状态=pl1000OpenUnit(手柄) 打开=句柄0 如果开放的话 '获取此变量的最大ADC值 状态=pl1000MaxValue(句柄,maxValue) '获取单位信息 单元(9,“p”).value=“单元打开” SLegnth=pl1000GetUnitInfo(句柄,S,255,所需大小,3) 单元格(10,“P”)。值=S SLegnth=pl1000GetUnitInfo(句柄,S,255,所需大小,4) 单元格(11,“P”)。值=S SLegnth=pl1000GetUnitInfo(句柄,S,255,所需大小,1) 单元格(12,“P”)。值=S “没有触发器 调用pl1000SetTrigger(句柄,假,0,0,0,0,0,0,0) '假设我们要在[W4]中读取[W3]读数 '来自频道1和频道2 '设置要读取的样本数 作为整数的Dim samplenum samplenum=工作表(“Sheet1”)。范围(“W3”)。值“读取W3中的样本数 nValues=samplenum 通道(0)=1 通道(1)=2 频道(2)=3 频道(3)=4 频道(4)=5 频道(5)=6 频道(6)=7 频道(7)=8 频道(8)=9 '设置测试长度 暗采样间隔与长采样间隔相同 尺寸为microsecs的块,长度为 Dim testlength为整数 testlength=工作表(“Sheet1”).范围($w$4”).值 块的微秒数=测试长度*1000000 状态=pl1000SetInterval(手柄,微秒,用于块,N值,通道(0),2)' 状态=pl1000Run(句柄,n值,0) 就绪=0 准备就绪时执行=0 状态=PL1000就绪(手柄,就绪) 环 '获取100个读数块。。。 我们可以重复地称之为例行程序 '获取具有相同设置的更多块 Dim triggerIndex尽可能长 作为整数的Dim溢出 状态=pl1000GetValues(句柄、值(0)、nValues、溢出、triggerIndex) '将数据复制到电子表格中 对于i=0到n值-1 单元(i+4,“A”)。值=adc_至_mv(值(2*i)) 单元(i+4,“B”)。值=adc_至_mv(值(2*i+1))“” 单元(i+4,“C”)。值=adc_至_mv(值(2*i+2)) 单元(i+4,“D”)。值=adc_至_mv(值(2*i+3)) 单元(i+4,“E”)。值=adc_至_mv(值(2*i+4)) 接下来我 '完成后关闭装置以放下驱动器 呼叫PL1000闭合单元(手柄) 其他的 MsgBox“无法打开设备”,vbCritical 如果结束 端接头 有人能透露一些见解吗


这是任何了解VBA的人都可以解决的问题,还是您也需要具备picolog知识?显然,如果没有其他问题,则需要使用picologger来正确测试代码。

此代码功能正常,但只能记录12个通道。如果不修改代码,它不会做得更少或更多。这可能会使大型数据集的速度非常慢。它基于picotech SDK构建

' Excel Macro for the PicoLog 1000 series
' Pico Technology 2009

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
Declare Function pl1000SetInterval Lib "pl1000.dll" (ByVal handle As Integer, ByRef microsecs_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, 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" (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() As Integer 'set number of datapoints outputted. Should equal samplenum * # ofchannels
Dim channels(12) 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()

Range("P9:P14").ClearContents 'remove device data from last run so that the device data is not displayed in case of error


'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(9, "P").value = "Unit opened"
  SLegnth = pl1000GetUnitInfo(handle, S, 255, requiredSize, 3)
  Cells(10, "P").value = S
  SLegnth = pl1000GetUnitInfo(handle, S, 255, requiredSize, 4)
  Cells(11, "P").value = S
  SLegnth = pl1000GetUnitInfo(handle, S, 255, requiredSize, 1)
  Cells(12, "P").value = S

  ' No Trigger
  Call pl1000SetTrigger(handle, False, 0, 0, 0, 0, 0, 0, 0)

  ' Say that we want to take W3 samples in W4 seconds
  ' from channels 1, 2, 3

    ' HM
    Dim numChannels As Integer
    'numChannels = Worksheets("Sheet1").Range("W5").value
    numChannels = 12

    Dim samplenum As Long
        samplenum = Worksheets("Sheet1").Range("W3").value 'Reads number of samples from W3
        nValues = samplenum * numChannels 'HM

    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
    channels(10) = 11
    channels(11) = 12

    ReDim values(12 * Worksheets("Sheet1").Range("W3").value) 'allow a variable data array
    Dim sampleInterval As Long
    Dim microsecs_for_block As Long

    Dim testlength As Integer
    testlength = Worksheets("Sheet1").Range("W4").value
    microsecs_for_block = testlength * 1000000
    status = pl1000SetInterval(handle, microsecs_for_block, nValues, channels(0), 12)

    status = pl1000Run(handle, nValues, 0)


    ready = 0
    Do While ready = 0
        status = pl1000Ready(handle, ready)
    Loop
 Cells(14, "P").value = "RECORDING COMPLETE" 'indicate readiness
  ' Get a block of W3 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), samplenum, overflow, triggerIndex)

  ' Copy the data into the spreadsheet
  For i = 0 To samplenum - 1
     Cells(i + 4, "A").value = adc_to_mv(values(numChannels * i + 0))
     Cells(i + 4, "B").value = adc_to_mv(values(numChannels * i + 1))
     Cells(i + 4, "C").value = adc_to_mv(values(numChannels * i + 2))
     Cells(i + 4, "D").value = adc_to_mv(values(numChannels * i + 3))
     Cells(i + 4, "E").value = adc_to_mv(values(numChannels * i + 4))
     Cells(i + 4, "F").value = adc_to_mv(values(numChannels * i + 5))
     Cells(i + 4, "G").value = adc_to_mv(values(numChannels * i + 6))
     Cells(i + 4, "H").value = adc_to_mv(values(numChannels * i + 7))
     Cells(i + 4, "I").value = adc_to_mv(values(numChannels * i + 8))
     Cells(i + 4, "J").value = adc_to_mv(values(numChannels * i + 9))
     Cells(i + 4, "K").value = adc_to_mv(values(numChannels * i + 10))
     Cells(i + 4, "L").value = adc_to_mv(values(numChannels * i + 11))
  Next i

  ' Close the unit when finished to drop the driver
  Call pl1000CloseUnit(handle)

Else
   MsgBox "Unable to open device", vbCritical
End If

End Sub
PicoLog 1000系列的Excel宏 “Pico Technology 2009 将函数pl1000OpenUnit Lib“pl1000.dll”(ByRef句柄为整数)声明为 将函数pl1000CloseUnit Lib“pl1000.dll”(ByVal句柄为整数)声明为 将函数pl1000GetUnitInfo Lib“pl1000.dll”(ByVal句柄为整数,ByVal S为字符串,ByVal lth为整数,ByRef requiredSize为整数,ByVal info为整数)声明为整数 将函数pl1000SetTrigger Lib“pl1000.dll”(ByVal句柄为整数,ByVal enabled为整数,ByVal enable_auto为整数,ByVal auto_ms为整数,ByVal channel为整数,ByVal dir为整数,ByVal thresholds为整数,ByVal delay为单)声明为整数 将函数pl1000SetInterval Lib“pl1000.dll”(ByVal句柄为整数,ByRef microsecs_表示_块为长,ByVal理想_no_表示_样本为长,通道为整数,ByVal no_表示_通道为整数)声明为长 声明函数pl1000GetValues Lib“pl1000.dll”(ByVal句柄为整数,ByRef值