How to sample at 1 KHz rate in VB6?

Legacy support with Phidget21
borisg
Phidgetly
Posts: 38
Joined: Tue Jun 21, 2011 11:25 pm
Location: chaos
Contact:

How to sample at 1 KHz rate in VB6?

Postby borisg » Mon Jun 27, 2011 4:17 am

I just got the Phidgets 8/8/8 board and wrote a simple program to acquire data from analog inputs with continuous sampling but the data rate is 125 Hz, not the 1 KHz that I expected. SensorChangeTrigger was set to 0 for each of the 4 analog inputs I wanted to sample at 1 KHz.

When the OnSensorChange event occurs, I grab the data returned but note that the return value is a long, not the 8 values that the documentation states are returned every 8 msec. I need data at 1 msec intervals. How do I get this from VB6?

RobOrt
Phidgeteer!
Posts: 85
Joined: Wed Jun 17, 2009 11:39 am
Contact:

Re: How to sample at 1 KHz rate in VB6?

Postby RobOrt » Mon Jun 27, 2011 10:16 am

If you're wanting to sample data at a constant rate of 1 KHz then you're using the wrong API functions.

The methods you are currently using, SensorTriggerChange & OnSensorChange, are for EVENT driven programming. This means that the Phidget drivers will inform your program ONLY when an EVENT occurs which in this case is a change in Sensor value bigger that the TriggerChange value. Think of it this way, if a Sensor is at value 100 and remains at the value of 100 for 24 hours your program will only receive an event ONCE in that 24 hours. The advantage of this model of programming is that your own program doesn't waste clock cycles constantly checking (or polling) the phidget for a sensor value. This is called the EVENT driven model and is the way Phidgets recommend you interact with their devices.

However if you DO need to poll devices then (certainly in the C API) there are functions to do this. In C it's 'getSensorValue()' and I imagine it's similar in VB6 (I'm not familiar with VB6 though). You would have to set up some sort of timing loop that called getSensorCount() at the rate you wanted but be careful when you do this as you're risking chomping up clock cycles. The best way really is event driven programming...

borisg
Phidgetly
Posts: 38
Joined: Tue Jun 21, 2011 11:25 pm
Location: chaos
Contact:

Re: How to sample at 1 KHz rate in VB6?

Postby borisg » Mon Jun 27, 2011 1:48 pm

Thanks for the reply RobOrt.

Looks like I have to start disassembling Phidget21COM.dll as there has to be an 8 sample buffer somewhere in the routine. According to the Phidget's documentation, setting SensorChangeTrigger to 0 will result in continuous sampling. For the Phidgets 8/8/8 interface, DataRateMax is returned as 1 indicating 1 msec sample period. I've set DataRate to 1 for the analog inputs I want sampled at 1 KHz.

To utilize your suggestion would require a loop where I have a sleep(1) statement to get the 1 msec pause needed between samples. Timing under windoze is horrendous, even when one sets a process priority to real time. The application I'm writing was planning on using the Phidgets 8/8/8 board to serve as an accurate 1 msec timebase. Phidget's data packets can be sent only every 8 msec and there doesn't appear to be any library routines which deal with an 8 sample data packet as all the routines are single sample oriented. It appears that the event driven Phidgets routines are just providing the first A/D value and I need access to the other 7. I'll try the sleep(1), SensorRawValue loop out of curiousity but suspect that SensorRawValue will only return the first A/D sample of the 8 sample packet also.

The whole reason I went with the Phidget's board was to avoid getting into writing the application in 680x0 assembly language for the coldfire processor based NetBurner board which I started the project on 3 years ago.

User avatar
Patrick
Lead Developer
Posts: 3064
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: How to sample at 1 KHz rate in VB6?

Postby Patrick » Tue Jun 28, 2011 9:24 am

You won't be able to get your 1ms data with getSensorValue() - this is only updated every 8ms. This is because data is only sent to the PC at 8ms intervals. If you have datarate set to 1, then 8 1ms samples will be sent at once to the PC from the Phidget - you will then get 8 separate sensor change events in quick succession (assuming change trigger is 0).

After setting data rate to 1, does it read back as 1? You may have to wait ~50ms for it to come back as 1, as the message is passed to the Phidget and then it reports it's new active data rate.

-Patrick

borisg
Phidgetly
Posts: 38
Joined: Tue Jun 21, 2011 11:25 pm
Location: chaos
Contact:

Re: How to sample at 1 KHz rate in VB6?

Postby borisg » Wed Jun 29, 2011 2:20 am

Hi Patrick:
Still only get samples every 8 msec. Here's my event handler:

Private Sub phidgetIFK_OnSensorChange(ByVal Index As Long, ByVal SensorValue As Long)
'
Static ierr As Integer
Static i As Long
If (SampleFlag <> 0) And (OvfFlag = 0) Then
If (DataIndex(Index) <= DataMax) Then
SensorData(Index, DataIndex(Index)) = SensorValue
DataIndex(Index) = DataIndex(Index) + 1
TotSamples = TotSamples + 1
Else
OvfFlag = OvfFlag + 1
End If
End If
End Sub

Channels 6 and 7 were set to 1 msec DataRate and I did get a 1 reading back DataRateMax for channel 7. Channels 0 to 5 were set to 1000 with SensorChangeTrigger = 2 for each channel. Still only getting 125 Hz sampling rate. Didn't use my TSC reading subroutine to time the samples as I was concerned about execution time but rather tapped as fast as I could on a FSR connected to input #7. I can't generate taps 16 msec apart but certainly can generate inter-tap intervals of 128 msec. Made no difference if I compiled test program and ran it at highest priority.

I know that the compiled VB code executes in microseconds but the only thing I don't have a handle on is how long it takes to generate the event. System this is running on is an AMD E350 CPU with clockspeed of 1.6 GHz and, unfortunately, it has W7 installed. Still, it runs programs of mine which sample serial to USB devices at a far faster speed than the Phidgets 8/8/8 board running at 4 ksamples/sec.

This seems quite odd as the 8/8/8 board appears to be acting just like the PhidgetInterfaceKit board I bought in 2006 which I wanted to upgrade as I do need 1 KHz sampling rate for the project I'm doing.

User avatar
Patrick
Lead Developer
Posts: 3064
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: How to sample at 1 KHz rate in VB6?

Postby Patrick » Wed Jun 29, 2011 9:20 am

I know that DataRateMax is always going to return 1, I want to verify that DataRate returns what you set it to (1).

-Patrick

borisg
Phidgetly
Posts: 38
Joined: Tue Jun 21, 2011 11:25 pm
Location: chaos
Contact:

Re: How to sample at 1 KHz rate in VB6?

Postby borisg » Wed Jun 29, 2011 3:42 pm

Oops, late night programming error. Changed display to output DataRate after I set it and it comes back as 1, but the sample interval is still 8 msec.

Is there any way to grab that 8 sample packet at a lower level?

dtimer

Re: How to sample at 1 KHz rate in VB6?

Postby dtimer » Tue Jul 05, 2011 4:39 pm

I posted a modification to the VBA accelerometer code in the VBA forum. I explain in the code how to get 1ms samples.

borisg
Phidgetly
Posts: 38
Joined: Tue Jun 21, 2011 11:25 pm
Location: chaos
Contact:

Re: How to sample at 1 KHz rate in VB6?

Postby borisg » Tue Jul 05, 2011 6:53 pm

Thanks dtimer. Had a look at your code and glad it worked for you. I tried the same thing by using an array for SensorValue, but no luck. It seems the library definition is such that all parameters have to be passed ByVal and this rules out using SensorValue(). Variants didn't work either. I'll check to see if I've tried all permutations of fooling the routine so it will give me a chunk of data instead of a single value.

Just used OLE viewer to look at Phidget21 Events interfaces and for PhidgetInterfaceKit find:
HRESULT OnSensorChange(
[in] long Index,
[in] long SensorValue);

This suggests that the VB library is broken and the question is whether Phidgets will fix it.

For your application, one gets:
[id(0x00000006)]
HRESULT OnSpatialData(
[in] SAFEARRAY(PhidgetSpatial_SpatialEventData)* data,
[in] long dataCount);

which returns an array variable. I've closed the Excel VB code window with your code example so if I'm wrong about the call above my excuse is that it's been a long day.

Looks like the questions now are:
(1) is there an easy fix to Phidget21Com?
(2) is there any other way of getting at the 1 KHz data without having to either write a HID sniffer or learn Java?

User avatar
Patrick
Lead Developer
Posts: 3064
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: How to sample at 1 KHz rate in VB6?

Postby Patrick » Wed Jul 06, 2011 9:30 am

I am able to get 1000 samples per second, no problem in VB6.0.

With simple modification to the InterfaceKit-controller example:

Code: Select all

Dim WithEvents phidgetIFK As PhidgetInterfaceKit
Dim counter As Integer
....
Private Sub Form_Load()
    ....
    counter = 0
    phidgetIFK.Open
End Sub
....
Private Sub phidgetIFK_OnAttach()
    ....
    phidgetIFK.SensorChangeTrigger(0) = 0
    phidgetIFK.DataRate(0) = phidgetIFK.DataRateMax(0)
End Sub
....
Private Sub phidgetIFK_OnSensorChange(ByVal Index As Long, ByVal SensorValue As Long)
    If (Index = 0) Then counter = counter + 1
End Sub
....
Private Sub Timer1_Timer()
    eventCounter.Text = counter
    counter = 0
End Sub


Where eventCounter is a text box and Timer1 fires once per second.

The data rate per second is correctly displayed as 1000.

-Patrick


Return to “VB 6.0”

Who is online

Users browsing this forum: No registered users and 1 guest