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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
| Dim dataArray As Variant
Private Declare Function GetPixel Lib "gdi32" (ByVal hDC As Long, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function SetPixelV Lib "gdi32" (ByVal hDC As Long, ByVal x As Long, ByVal y As Long, ByVal Color As Long) As Byte
Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
Dim periodTimer As clsTimer
Dim theBigBuffer As String
Dim msCalibrationConst As Double
Dim wholeTime As Double
Dim continuousReadIndex As Integer
Dim REX() 'REX[ ] holds the real part of the frequency domain
Dim IMX() 'IMX[ ] holds the imaginary part of the frequency domain
Dim outputArray() As Long
Dim running As Boolean
Dim crcOK As Boolean
Dim useCRC As Boolean
Option Explicit
Private Sub Form_Load()
frmMain.Show
tmrDelay.Enabled = False
ReDim dataArray(512) As Integer
Set periodTimer = New clsTimer
timerCheckConnection_Timer
scrollVLine1.Max = UBound(dataArray)
scrollVLine2.Max = UBound(dataArray)
msCalibrationConst = 0.125
wholeTime = msCalibrationConst * UBound(dataArray)
running = True
useCRC = True
continuousReadIndex = 0
txtArraySize.Text = "512"
txtArraySize_Change
chkUseCRC_Click
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim Result As Long
Result = CloseHandle(HIDHandle)
Result = CloseHandle(ReadHandle)
End Sub
Private Sub chkContinuous_Click()
Dim i As Integer
If chkContinuous.Value = 1 Then
For i = 0 To 512 - 1
DoEvents
dataArray(i) = 0
Next
cmdGetADCData.Enabled = False
cmdGetADCDataUSMS.Enabled = False
txtGetADCms.Enabled = False
timerContinuous.Enabled = True
Else
cmdGetADCData.Enabled = True
cmdGetADCDataUSMS.Enabled = True
txtGetADCms.Enabled = True
timerContinuous.Enabled = False
End If
End Sub
Private Sub chkUseCRC_Click()
If chkUseCRC.Value = 0 Then
txtByte(0).Text = "11"
useCRC = False
cmdOnce_Click
Else
txtByte(0).Text = "10"
useCRC = True
cmdOnce_Click
End If
End Sub
Private Sub cmdOnce_Click()
Dim x As Integer
Dim theCRC As Byte
If Not MyDeviceDetected Then
MyDeviceDetected = FindTheHid
End If
If MyDeviceDetected Then
OutputReportData(0) = Val(txtByte(0).Text)
OutputReportData(1) = Val(txtByte(1).Text)
OutputReportData(2) = Val(txtByte(2).Text)
OutputReportData(3) = Val(txtByte(3).Text)
OutputReportData(4) = Val(txtByte(4).Text)
OutputReportData(5) = Val(txtByte(5).Text)
OutputReportData(6) = Val(txtByte(6).Text)
OutputReportData(7) = Val(txtByte(7).Text)
Call ReadAndWriteToDevice
lblReadData = Str$(ReadBuffer(1)) & "," & Str$(ReadBuffer(2)) & "," & Str$(ReadBuffer(3)) & "," & Str$(ReadBuffer(4)) & "," & Str$(ReadBuffer(5)) & "," & Str$(ReadBuffer(6)) & "," & Str$(ReadBuffer(7)) & "," & Str$(ReadBuffer(8))
If useCRC Then
theCRC = calc_CRC(0, ReadBuffer(1))
theCRC = calc_CRC(theCRC, ReadBuffer(2))
theCRC = calc_CRC(theCRC, ReadBuffer(3))
theCRC = calc_CRC(theCRC, ReadBuffer(4))
theCRC = calc_CRC(theCRC, ReadBuffer(5))
theCRC = calc_CRC(theCRC, ReadBuffer(6))
theCRC = calc_CRC(theCRC, ReadBuffer(7))
If theCRC = ReadBuffer(8) Then
lblReadData = lblReadData & " CRC OK"
crcOK = True
Else
lblReadData = lblReadData & " CRC BAD"
crcOK = False
End If
End If
End If
End Sub
Private Sub cmdGetData_Click()
Dim i As Integer
If chkPauseRedraw.Value = False And running And MyDeviceDetected Then
For i = 0 To 512 - 1
DoEvents
txtByte(0).Text = "2" 'usbReadRam
txtByte(1).Text = Str(Int(i / 256))
txtByte(2).Text = Str(i - Int(i / 256) * 256)
cmdOnce_Click
dataArray(i) = Val(ReadBuffer(5))
Next
debugData
End If
End Sub
Private Sub cmdGetADCData_Click()
Dim i As Integer
If chkPauseRedraw.Value = False And running And MyDeviceDetected Then
txtByte(0).Text = "5" 'usbReadADCnTimes
txtByte(1).Text = "2" 'For 512 samples
txtByte(2).Text = "0" 'For 512 samples
cmdOnce_Click
For i = 0 To 512 - 1
DoEvents
txtByte(0).Text = "2" 'usbReadRam
txtByte(1).Text = Str(Int(i / 256))
txtByte(2).Text = Str(i - Int(i / 256) * 256)
cmdOnce_Click
dataArray(i) = Val(ReadBuffer(5))
Next
debugData
txtByte(0).Text = "6" 'usbReadADCPeriod
cmdOnce_Click
msCalibrationConst = ((ReadBuffer(3) * 256 + ReadBuffer(4)) * 1 / 1500) / UBound(dataArray)
wholeTime = msCalibrationConst * UBound(dataArray)
End If
End Sub
Private Sub cmdGetADCDataUSMS_Click()
Dim i As Integer
If optionUSMS(1).Value = True Then
If Val(txtGetADCms.Text) < 1 Then
txtGetADCms.Text = 1
ElseIf Val(txtGetADCms.Text) > 65535 Then
txtGetADCms.Text = 65535
End If
Else
If Val(txtGetADCms.Text) < 250 Then
txtGetADCms.Text = 250
ElseIf Val(txtGetADCms.Text) > 65535 Then
txtGetADCms.Text = 65535
End If
End If
If chkPauseRedraw.Value = False And running And MyDeviceDetected Then
If optionUSMS(1).Value = True Then
txtByte(0).Text = "7" 'usbReadADCnTimesMS
Else
txtByte(0).Text = "12" 'usbReadADCnTimesUS
End If
txtByte(1).Text = "2" 'For 512 samples
txtByte(2).Text = "0" 'For 512 samples
txtByte(3).Text = Str(Int(Int(txtGetADCms.Text) / 256)) 'With period in ms
txtByte(4).Text = Str(Int(txtGetADCms.Text) - (Int(Int(txtGetADCms.Text) / 256) * 256))
cmdOnce_Click
For i = 0 To 512 - 1
DoEvents
txtByte(0).Text = "2" 'usbReadRam
txtByte(1).Text = Str(Int(i / 256))
txtByte(2).Text = Str(i - Int(i / 256) * 256)
cmdOnce_Click
dataArray(i) = Val(ReadBuffer(5))
Next
debugData
If optionUSMS(1).Value = True Then
msCalibrationConst = Int(txtGetADCms.Text) * 512 / UBound(dataArray)
wholeTime = msCalibrationConst * UBound(dataArray)
Else
msCalibrationConst = Int(txtGetADCms.Text) * 512 / 1000 / UBound(dataArray)
wholeTime = msCalibrationConst * UBound(dataArray)
End If
End If
End Sub
Public Function drawArray()
Dim theX, theY, oldX, oldY, i As Integer
Dim theXMult, theYMult As Double
Picture1.Cls
theXMult = Picture1.ScaleWidth / UBound(dataArray)
theYMult = Picture1.ScaleHeight / 265
Picture1.ForeColor = RGB(50, 50, 50)
Picture1.Line (0, Picture1.ScaleHeight - theYMult * 51)-(Picture1.ScaleWidth, Picture1.ScaleHeight - theYMult * 51)
Picture1.Line (0, Picture1.ScaleHeight - theYMult * 102)-(Picture1.ScaleWidth, Picture1.ScaleHeight - theYMult * 102)
Picture1.Line (0, Picture1.ScaleHeight - theYMult * 154)-(Picture1.ScaleWidth, Picture1.ScaleHeight - theYMult * 154)
Picture1.Line (0, Picture1.ScaleHeight - theYMult * 205)-(Picture1.ScaleWidth, Picture1.ScaleHeight - theYMult * 205)
Picture1.Line (0, Picture1.ScaleHeight - theYMult * 255)-(Picture1.ScaleWidth, Picture1.ScaleHeight - theYMult * 255)
Picture1.ForeColor = RGB(0, 255, 0)
oldX = 0
oldY = Picture1.ScaleHeight - theYMult * dataArray(0)
For i = 0 To UBound(dataArray) - 1
theX = theXMult * i
theY = Picture1.ScaleHeight - theYMult * dataArray(i)
Picture1.Line (oldX, oldY)-(theXMult * i, Picture1.ScaleHeight - theYMult * dataArray(i))
oldX = theX
oldY = theY
Next
If (Abs(scrollVLine1.Value - scrollVLine2.Value) * msCalibrationConst) > 0 Then
lblDeltaV.Caption = Format(Abs(scrollVLine1.Value - scrollVLine2.Value) * msCalibrationConst, "###,###.000" & "ms") & " (" & Format(1 / (Abs(scrollVLine1.Value - scrollVLine2.Value) * msCalibrationConst / 1000), "###,###.000" & "Hz") & ")"
Else
lblDeltaV.Caption = Format(Abs(scrollVLine1.Value - scrollVLine2.Value) * msCalibrationConst, "###,###.000" & "ms")
End If
Picture1.ForeColor = RGB(100, 100, 255)
Picture1.Line (theXMult * scrollVLine1.Value, 0)-(theXMult * scrollVLine1.Value, Picture1.ScaleHeight)
Picture1.Line (theXMult * scrollVLine2.Value, 0)-(theXMult * scrollVLine2.Value, Picture1.ScaleHeight)
Picture1.ForeColor = RGB(255, 100, 100)
Picture1.Line (0, Picture1.ScaleHeight - theYMult * scrollHLine1.Value)-(Picture1.ScaleWidth, Picture1.ScaleHeight - theYMult * scrollHLine1.Value)
Picture1.Line (0, Picture1.ScaleHeight - theYMult * scrollHLine2.Value)-(Picture1.ScaleWidth, Picture1.ScaleHeight - theYMult * scrollHLine2.Value)
lblDeltaH.Caption = Format(Abs(scrollHLine1.Value - scrollHLine2.Value) * (5 / 255), "0.00" & "V")
End Function
Public Sub doDFT()
Dim cnt, N, k, i As Integer
On Error Resume Next
DoEvents
'THE DISCRETE FOURIER TRANSFORM
'copyright © 1997-1999 by California Technical Publishing
'published with permission from Steven W Smith, [url]www.dspguide.com[/url]
Const PI = 3.14159265
N = Val(comboDFT.Text)
ReDim REX(N / 2 + 1)
ReDim IMX(N / 2 + 1)
ReDim outputArray(N / 2 + 1)
For k = 0 To UBound(REX) - 1
REX(k) = 0
IMX(k) = 0
For i = 1 To N
REX(k) = REX(k) + dataArray(i) * Cos(2 * PI * k * i / N)
IMX(k) = IMX(k) - dataArray(i) * Sin(2 * PI * k * i / N)
Next i
Next k
For cnt = 1 To UBound(outputArray) - 1
outputArray(cnt) = (IMX(cnt) * IMX(cnt)) + (REX(cnt) * REX(cnt))
Next cnt
drawDFT
End Sub
Public Function drawDFT()
Dim theX, theY, oldX, oldY, i As Integer
Dim theXMult, theYMult As Double
Dim maxValue As Double
Dim maxValuei As Integer
For i = 0 To UBound(outputArray) - 1
If (maxValue < outputArray(i)) Then
maxValue = outputArray(i)
maxValuei = i
End If
Next
If (maxValue = 0) Then
maxValue = 1
maxValuei = 1
labelDFT.Caption = ""
Else
labelDFT.Caption = "Bin: " & maxValuei & " f~" & Format(maxValuei * 2 ^ comboDFT.ListIndex * (1 / (wholeTime * 1 / 1000)), "###,##0.0 Hz")
End If
theXMult = Picture1.ScaleWidth / UBound(outputArray)
theYMult = Picture1.ScaleHeight / maxValue
Picture1.ForeColor = RGB(0, 0, 255)
oldX = 0
oldY = Picture1.ScaleHeight - theYMult * dataArray(0)
For i = 0 To UBound(outputArray) - 1
theX = theXMult * i
theY = Picture1.ScaleHeight - theYMult * outputArray(i)
Picture1.Line (oldX, oldY)-(theXMult * i, Picture1.ScaleHeight - theYMult * outputArray(i))
oldX = theX
oldY = theY
Next
End Function
Public Sub debugData()
Dim theText As String
Dim i As Integer
theText = "("
For i = 1 To UBound(dataArray)
theText = theText & Str(dataArray(i - 1)) & ","
Next
txtInput.Text = Mid(theText, 1, Len(theText) - 1) & ")"
End Sub
Private Sub Picture1_Click()
End Sub
Private Sub timerContinuous_Timer()
If chkPauseRedraw.Value = False And running And MyDeviceDetected Then
DoEvents
txtByte(0).Text = "4"
cmdOnce_Click
dataArray(continuousReadIndex) = Val(ReadBuffer(3))
debugData
continuousReadIndex = continuousReadIndex + 1
If continuousReadIndex >= UBound(dataArray) Then
periodTimer.StopTimer
msCalibrationConst = (periodTimer.m_StopTime - periodTimer.m_StartTime) / periodTimer.m_curFrequency * 10000 * 1000
wholeTime = Abs(msCalibrationConst)
msCalibrationConst = Abs(msCalibrationConst) / UBound(dataArray)
continuousReadIndex = 0
periodTimer.ResetTimer
End If
End If
End Sub
Private Sub timerDraw_Timer()
If UBound(dataArray) > 0 Then
DoEvents
running = False
If chkDFT Then doDFT
drawArray
If chkDFT Then drawDFT
running = True
End If
End Sub
Private Sub timerCheckConnection_Timer()
If FindTheHid Then
lblConnect.Caption = "Connected"
lblConnect.ForeColor = RGB(0, 150, 0)
Else
lblConnect.Caption = "Disconnected"
lblConnect.ForeColor = RGB(150, 0, 0)
End If
End Sub
Private Sub tmrDelay_Timer()
Timeout = True
tmrDelay.Enabled = False
End Sub
Private Sub txtArraySize_Change()
If Val(txtArraySize.Text) > 1 Then
ReDim Preserve dataArray(txtArraySize.Text) As Integer
scrollVLine1.Max = UBound(dataArray)
scrollVLine2.Max = UBound(dataArray)
comboDFT.Clear
comboDFT.AddItem UBound(dataArray)
comboDFT.AddItem Int(UBound(dataArray) / 2)
comboDFT.AddItem Int(UBound(dataArray) / 4)
comboDFT.AddItem Int(UBound(dataArray) / 8)
comboDFT.AddItem Int(UBound(dataArray) / 16)
comboDFT.AddItem Int(UBound(dataArray) / 32)
comboDFT.AddItem Int(UBound(dataArray) / 64)
comboDFT.ListIndex = 0
End If
End Sub
Private Sub txtByte_Change(Index As Integer)
End Sub |