Scaling K8055 Analog inputs?

I have a problem when I try to read the voltage of my K8055 analog inputs on labels in Visual Basic.

When I execute the ReadAnalogChannel (1) then 2 in the code it puts a long number into my label starting with 7 and going to about 8 decimal places. I’m guessing this is the value in memory?

I noticed that that there is only a 255 difference between teh minimum (0V) and the maximum (5V), but the large number and it changing everytime is where the problem lies.

I’m trying to read 0-5V through my labels, but the only way I can currently accomplish this is through a “Calibrate Low” button where the pot is adjusted to 0 ohms and the value is then put in a variable allowing me to then to accomplish viewing of 0-5V on the labels.

This way works but is combersome everytime, and I was wondering if there is a way to actually scale the numbers down to 0-5V or to get the number down to the 0-255 values.

I know later on with something like a thermistor connected to the input the large value could create a problem with getting the actual °C output read on the screen.

Thanks in advance,

Chris

If you want to get both at the same time try ReadAllAnalog (byref) to save you a little bit of time.

As for the value, it should return 0-255 nothing else. Are you sure you are not doing any calculations on the value before it is returned? examples of your code?

The value does jump around quite a lot, best thing to do is just soften this down. You will be doing such with changing it to 0-5 anyway, less decimal places less shifting.

As for 0-255>0-5 it’s simple maths. 255 / 5 is 51, so / 51 from whatever you want. And then round it down to the amount of decimal places you want.

[quote=“drsquirrel”]If you want to get both at the same time try ReadAllAnalog (byref) to save you a little bit of time.

As for the value, it should return 0-255 nothing else. Are you sure you are not doing any calculations on the value before it is returned? examples of your code?

The value does jump around quite a lot, best thing to do is just soften this down. You will be doing such with changing it to 0-5 anyway, less decimal places less shifting.

As for 0-255>0-5 it’s simple maths. 255 / 5 is 51, so / 51 from whatever you want. And then round it down to the amount of decimal places you want.[/quote]

I know the math and the code necessary, I’m just saying that it wasn’t returning values from 0-255 (it was exceptionally large numbers like around 700 billion or so). Now the actual range was 0-255. I’ll post Screenshots later on.

Here’s my code:

[code]Public Class Form1
Dim D1 As Long
Dim D2 As Long
Private Declare Function OpenDevice Lib “k8055d.dll” (ByVal CardAddress As Long) As Long
Private Declare Sub CloseDevice Lib “k8055d.dll” ()
Private Declare Function ReadAnalogChannel Lib “k8055d.dll” (ByVal Channel As Long) As Long
Private Declare Sub ReadAllAnalog Lib “k8055d.dll” (ByVal Data1 As Long, ByVal Data2 As Long)
Private Declare Sub OutputAnalogChannel Lib “k8055d.dll” (ByVal Channel As Long, ByVal Data As Long)
Private Declare Sub OutputAllAnalog Lib “k8055d.dll” (ByVal Data1 As Long, ByVal Data2 As Long)
Private Declare Sub ClearAnalogChannel Lib “k8055d.dll” (ByVal Channel As Long)
Private Declare Sub SetAllAnalog Lib “k8055d.dll” ()
Private Declare Sub ClearAllAnalog Lib “k8055d.dll” ()
Private Declare Sub SetAnalogChannel Lib “k8055d.dll” (ByVal Channel As Long)
Private Declare Sub WriteAllDigital Lib “k8055d.dll” (ByVal Data As Long)
Private Declare Sub ClearDigitalChannel Lib “k8055d.dll” (ByVal Channel As Long)
Private Declare Sub ClearAllDigital Lib “k8055d.dll” ()
Private Declare Sub SetDigitalChannel Lib “k8055d.dll” (ByVal Channel As Long)
Private Declare Sub SetAllDigital Lib “k8055d.dll” ()
Private Declare Function ReadDigitalChannel Lib “k8055d.dll” (ByVal Channel As Long) As Boolean
Private Declare Function ReadAllDigital Lib “k8055d.dll” () As Long
Private Declare Function ReadCounter Lib “k8055d.dll” (ByVal CounterNr As Long) As Long
Private Declare Sub ResetCounter Lib “k8055d.dll” (ByVal CounterNr As Long)
Private Declare Sub SetCounterDebounceTime Lib “k8055d.dll” (ByVal CounterNr As Long, ByVal DebounceTime As Long)

Private Sub VB_DigIO_Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim CardAddress As Long
    Dim h As Long
    CardAddress = 0
    h = OpenDevice(CardAddress)
End Sub

Private Sub Startb_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Startb.Click
    Do
        Application.DoEvents()
        D1 = ReadAnalogChannel(1)
        D2 = ReadAnalogChannel(2)
        lblA1.Text = D1
        lblA2.Text = D2
    Loop
End Sub

End Class
[/code]

Here is where I see the LARGE numbers associated with ReadAnalogChannel(1) and ReadAnalogChannel(2).
Note: These are with both pots set to ZERO Ohms.

When I turn them to maximum the difference is still 255. On the picture below i meant to turn both to maximum (100K ohm) but only did one:

Okay when I try to use this code:

[code]Public Class Form1
Dim D1 As Long
Dim D2 As Long
Private Declare Function OpenDevice Lib “k8055d.dll” (ByVal CardAddress As Long) As Long
Private Declare Sub CloseDevice Lib “k8055d.dll” ()
Private Declare Function ReadAnalogChannel Lib “k8055d.dll” (ByVal Channel As Long) As Long
Private Declare Sub ReadAllAnalog Lib “k8055d.dll” (ByVal Data1 As Long, ByVal Data2 As Long)
Private Declare Sub OutputAnalogChannel Lib “k8055d.dll” (ByVal Channel As Long, ByVal Data As _
Long)
Private Declare Sub OutputAllAnalog Lib “k8055d.dll” (ByVal Data1 As Long, ByVal Data2 As _
Long)
Private Declare Sub ClearAnalogChannel Lib “k8055d.dll” (ByVal Channel As Long)
Private Declare Sub SetAllAnalog Lib “k8055d.dll” ()
Private Declare Sub ClearAllAnalog Lib “k8055d.dll” ()
Private Declare Sub SetAnalogChannel Lib “k8055d.dll” (ByVal Channel As Long)
Private Declare Sub WriteAllDigital Lib “k8055d.dll” (ByVal Data As Long)
Private Declare Sub ClearDigitalChannel Lib “k8055d.dll” (ByVal Channel As Long)
Private Declare Sub ClearAllDigital Lib “k8055d.dll” ()
Private Declare Sub SetDigitalChannel Lib “k8055d.dll” (ByVal Channel As Long)
Private Declare Sub SetAllDigital Lib “k8055d.dll” ()
Private Declare Function ReadDigitalChannel Lib “k8055d.dll” (ByVal Channel As Long) As _
Boolean
Private Declare Function ReadAllDigital Lib “k8055d.dll” () As Long
Private Declare Function ReadCounter Lib “k8055d.dll” (ByVal CounterNr As Long) As Long
Private Declare Sub ResetCounter Lib “k8055d.dll” (ByVal CounterNr As Long)
Private Declare Sub SetCounterDebounceTime Lib “k8055d.dll” (ByVal CounterNr As Long, ByVal _
DebounceTime As Long)

Private Sub VB_DigIO_Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim CardAddress As Long
    Dim h As Long
    CardAddress = 0
    h = OpenDevice(CardAddress)
End Sub

Private Sub Startb_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Startb.Click
    Do
        Application.DoEvents()
        ReadAllAnalog(D1, D2)
        lblA1.Text = D1
        lblA2.Text = D2
    Loop
End Sub

End Class
[/code]

Here’s Images of the Error it gives me:

More zoomed out to show point:

Regards,

Chris

[/code]

Okay problem fixed, I didn’t realize that ByVal couldn’t be used on the ReadAnalog function because it will send you to the memory address? But anyways, I took a look at the demo’s source code and just put this in and it works perfect.

Do Application.DoEvents() ReadAllAnalog(Data1, Data2) lblA1.Text = Data1 / 51 lblA2.Text = Data2 / 51 Loop

Thanks drsquirrel, that’s what you were trying to tell me, but I had the ReadAllAnalog defined wrong thus preventing it from actually giving me the referenced value to the memory address. Instead it would just give me the actual memory address!

I don’t know if anyone still reads these old posts but I had exactly the same issue. I looked at the source code for the demo and noticed that the header declares the ReadAllAnalog procedure as follows:

Private Declare Sub ReadAllAnalog Lib “k8055d.dll” (Data1 As Long, Data2 As Long)

I copied and pasted into my code… but my system (Vista & VB 2008 Express Edition) forces it to:

Private Declare Sub ReadAllAnalog Lib “k8055d.dll” ([color=#BF0000]ByVal[/color] Data1 As Long, [color=#BF0000]ByVal[/color] Data2 As Long)

It was only when I changed the [color=#BF0000]ByVal[/color] to [color=#BF0000]ByRef[/color] that it starts to read 0 -255.

Can someone explain to me why this is please???

Now. even though it works it still throws up this message in the error list:

“A first chance exception of type ‘System.AccessViolationException’ occurred in K8055_AnIns.exe”


Public Class Form1

    Dim DoNothing As Boolean
    Dim n As Integer
    Private Declare Function OpenDevice Lib "k8055d.dll" (ByVal CardAddress As Long) As Long
    Private Declare Sub CloseDevice Lib "k8055d.dll" ()
    Private Declare Function ReadAnalogChannel Lib "k8055d.dll" (ByVal Channel As Long) As Long
    Private Declare Sub ReadAllAnalog Lib "k8055d.dll" (ByRef Data1 As Long, ByRef Data2 As Long)
    Private Declare Sub OutputAnalogChannel Lib "k8055d.dll" (ByVal Channel As Long, ByVal Data As Long)
    Private Declare Sub OutputAllAnalog Lib "k8055d.dll" (ByVal Data1 As Long, ByVal Data2 As Long)
    Private Declare Sub ClearAnalogChannel Lib "k8055d.dll" (ByVal Channel As Long)
    Private Declare Sub SetAllAnalog Lib "k8055d.dll" ()
    Private Declare Sub ClearAllAnalog Lib "k8055d.dll" ()
    Private Declare Sub SetAnalogChannel Lib "k8055d.dll" (ByVal Channel As Long)
    Private Declare Sub WriteAllDigital Lib "k8055d.dll" (ByVal Data As Long)
    Private Declare Sub ClearDigitalChannel Lib "k8055d.dll" (ByVal Channel As Long)
    Private Declare Sub ClearAllDigital Lib "k8055d.dll" ()
    Private Declare Sub SetDigitalChannel Lib "k8055d.dll" (ByVal Channel As Long)
    Private Declare Sub SetAllDigital Lib "k8055d.dll" ()
    Private Declare Function ReadDigitalChannel Lib "k8055d.dll" (ByVal Channel As Long) As Boolean
    Private Declare Function ReadAllDigital Lib "k8055d.dll" () As Long
    Private Declare Function ReadCounter Lib "k8055d.dll" (ByVal CounterNr As Long) As Long
    Private Declare Sub ResetCounter Lib "k8055d.dll" (ByVal CounterNr As Long)
    Private Declare Sub SetCounterDebounceTime Lib "k8055d.dll" (ByVal CounterNr As Long, ByVal DebounceTime As Long)

    



    Private Sub Connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Connect.Click

        Dim CardAddress As Long
        Dim h As Long
        CardAddress = 0
        h = OpenDevice(CardAddress)
        Timer1.Enabled = True

    End Sub

    Private Sub SetAnalogOut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SetAnalogOut.Click

        SetAllAnalog()

    End Sub

    Private Sub ClearAnalogOut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearAnalogOut.Click

        ClearAllAnalog()

    End Sub

    Private Sub Disconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Disconnect.Click

        CloseDevice()

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        Timer1.Enabled = False

        Dim Data1 As Long
        Dim Data2 As Long

        ReadAllAnalog(Data1, Data2)

        Label2.Text = Data1
        Label4.Text = Data2

        Timer1.Enabled = True

    End Sub
End Class

Can anyone tell me why this might be happening??

Thanks

Great Forum. I looked a few posts down and found the post on K8055 & Excel.

I have downloaded and installed the latest K8055D.dll (4.0) and all is working perfectly.

At the same time, I want to log data so I am going to follow that post more closely… :smiley:

It’s good to see you got everything working fine with the new DLL v4.0.

[quote]I looked at the source code for the demo and noticed that the header declares the ReadAllAnalog procedure as follows:

Private Declare Sub ReadAllAnalog Lib “k8055d.dll” (Data1 As Long, Data2 As Long)

I copied and pasted into my code… but my system (Vista & VB 2008 Express Edition) forces it to:

Private Declare Sub ReadAllAnalog Lib “k8055d.dll” (ByVal Data1 As Long, ByVal Data2 As Long)

It was only when I changed the ByVal to ByRef that it starts to read 0 -255.

Can someone explain to me why this is please???[/quote]The explanation to this is the “Default Passing Mechanism”.
In Visual Basic 6.0, if you do not specify ByVal or ByRef for a procedure parameter, the passing mechanism defaults to ByRef.

When you declare a procedure in Visual Basic 2008, the passing mechanism defaults to ByVal for every parameter.

More detailed description is here:
http://msdn.microsoft.com/en-us/library/41zywfyc(VS.90).aspx

See also:

Thanks for that. Just a quick question so that I will know more for different platforms, does the DLL 4.0 apply for VB2008 or Vista/Windows 7? Or can it be used on any platform now? (i.e. is it backwards compatible?)

Also, why is it not available on the Velleman website under K8055?? :slight_smile:

[quote]Just a quick question so that I will know more for different platforms, does the DLL 4.0 apply for VB2008 or Vista/Windows 7? Or can it be used on any platform now? (i.e. is it backwards compatible?)[/quote]The version 4.0 is applicable to Windows 2000 and newer versions (XP, Vista, Windows7). Can run on both 32-bit and 64-bit operating systems. VB2008 and VB2010 are OK to use.
For older Windows version you have to use the previous DLL versions 2 or 3.

[quote]Also, why is it not available on the Velleman website under K8055??[/quote]The version 4.0 is still “under construction”. Will be put soon on the Velleman website.