Excel can easily use the U401 (or the U421) for data logging using Excel’s VBA language. This example Excel file will log the time that any of the inputs on port A of the U401/U421 changes. A button starts and stops the logging.
This example doesn’t have a whole lot of finesse. It bluntly grabs the first U401/U421 device that it finds, doesn’t have error checking, doesn’t allow for a change in logging rate, etc. These would be good to add, but this example stands as simple basic code.
Some of the source for the file is posted below, and the excel file itself can be downloaded from here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
<span style="color: #008000;">' Global variables Dim stopitnow As Boolean ' Run the process Private Sub StartButton_Click() ' Local variables Dim found As Boolean Dim sample As Long Dim uniquetime As Long Dim lastread As Byte Dim state As Byte stopit = False sample = 1 uniquetime = Int(Timer) ' Look for USB device found = USBm_FindDevices USBm_InitPorts 0 USBm_ReadA 0, state lastread = state If found = True Then UserForm1.StartButton.Caption = "Found device" While stopitnow = False USBm_ReadA 0, state If lastread <> state Then lastread = state Range("A1").Cells(sample, 1) = Time Range("A1").Cells(sample, 2) = state If state And &H1 Then Range("A1").Cells(sample, 3) = "Bit 0 on" Else Range("A1").Cells(sample, 3) = " " If state And &H2 Then Range("A1").Cells(sample, 4) = "Bit 1 on" Else Range("A1").Cells(sample, 4) = " " If state And &H4 Then Range("A1").Cells(sample, 5) = "Bit 2 on" Else Range("A1").Cells(sample, 5) = " " If state And &H8 Then Range("A1").Cells(sample, 6) = "Bit 3 on" Else Range("A1").Cells(sample, 6) = " " If state And &H10 Then Range("A1").Cells(sample, 7) = "Bit 4 on" Else Range("A1").Cells(sample, 7) = " " If state And &H20 Then Range("A1").Cells(sample, 8) = "Bit 5 on" Else Range("A1").Cells(sample, 8) = " " If state And &H40 Then Range("A1").Cells(sample, 9) = "Bit 6 on" Else Range("A1").Cells(sample, 9) = " " If state And &H80 Then Range("A1").Cells(sample, 10) = "Bit 7 on" Else Range("A1").Cells(sample, 10) = " " sample = sample + 1 End If DoEvents Wend End If End Sub ' Stop process Private Sub StopButton_Click() stopitnow = True End Sub</span> |
Leave a Reply