Is that correct from the K8055.DLL from velleman

Just starting out? Need help? Post your questions and find answers here.
Stephane Fonteyne
User
User
Posts: 27
Joined: Sun Sep 06, 2015 2:22 pm

Is that correct from the K8055.DLL from velleman

Post by Stephane Fonteyne »

I 'm only a beginner with PureBasic .
I would like to apply the libarary K8055 from Velleman .
I tried to declare all functions with the syntax of PureBasic .
Can someone look at once if everything is correct according to the syntax and semantic.

This my code (trial)
============

Code: Select all

Module k8055_library
  '----------------------------------------------------------------------------------
  ' K8055 VELLEMAN-kit USB EXPERIMENT INTERFACE BOARD
  ' HEADER INLCUDE FILE FOR K8055D.DLL  LIBRARY
  ' FILE NAME: K8055D.INC
  ' TRANSLATION BY JULES MARCHILDON, STEPHANE TO POWERBASIC FOR WINDOWS  PBWIN9 v9.03
  '----------------------------------------------------------------------------------

  '=================================================================================='
  'General Functions for Open and Close Library                                      '
  '==================================================================================' 
  'Declare FUNCTION OpenDevice         LIB "K8055D.DLL" ALIAS "OpenDevice" (BYVAL CardAddress As LONG) As LONG
  'Declare SUB CloseDevice             LIB "K8055D.DLL" ALIAS "CloseDevice" ()

  [b]DeclareDLL OpenDevice.l(cardaddress.l)
  DeclareDLL CloseDevice()[/b]
                                                                               
  '=================================================================================='
  'ADC Functions (Analog / Digital omzettingsprocedures  (Analog Input)              '
  '=================================================================================='
  'Declare FUNCTION ReadAnalogChannel  LIB "K8055D.DLL" ALIAS "ReadAnalogChannel"(BYVAL Channel As LONG) As LONG
  'Declare SUB      ReadAllAnalog      LIB "K8055D.DLL" ALIAS "ReadAllAnalog" (BYVAL Data1 As LONG, BYVAL Data2 As LONG) 

  [b]DeclareDLL ReadAnalogChannel.a(channel.a)
  DeclareDLL ReadAllAnalog(aData1.a,aData2.a)[/b]
  
  '=================================================================================='
  'DAC Functions (Digital / Analog omzettingsprocedures  (Analog Output)             '
  '=================================================================================='
  'Declare SUB OutputAnalogChannel     LIB "K8055D.DLL" ALIAS "OutputAnalogChannel" (BYVAL Channel As LONG, BYVAL oData As LONG)
  'Declare SUB OutputAllAnalog         LIB "K8055D.DLL" ALIAS "OutputAllAnalog" (BYVAL Data1 As LONG, BYVAL Data2 As LONG)
  'Declare SUB ClearAnalogChannel      LIB "K8055D.DLL" ALIAS "ClearAnalogChannel" (BYVAL Channel As LONG)
  'Declare SUB ClearAllAnalog          LIB "K8055D.DLL" ALIAS "ClearAllAnalog" ()
  'Declare SUB setAnalogChannel        LIB "K8055D.DLL" ALIAS "SetAnalogChannel" (BYVAL Channel As LONG )
  'Declare SUB SetAllAnalog            LIB "K8055D.DLL" ALIAS "SetAllAnalog" ()

  [b]DeclareDLL OutputAnalogChannel(aChannel.a,aData.a)
  DeclareDLL OutputAllAnalog(aData1.a, aData2.a)
  DeclareDLL ClearAnalogChannel(aChannel.a)
  DeclareDLL ClearAllAnalog()
  DeclareDLL setAnalogChannel(aChannel.a)
  DeclareDLL SetAllAnalog()
[/b]
  '=================================================================================='
  'Digital Output Functions (DO)                                                     '
  '=================================================================================='
  'Declare SUB WriteAllDigital         LIB "K8055D.DLL" ALIAS "WriteAllDigital" (BYVAL aData As LONG)
  'Declare SUB ClearDigitalChannel     LIB "K8055D.DLL" ALIAS "ClearDigitalChannel" (BYVAL Channel As LONG)
  'Declare SUB ClearAllDigital         LIB "K8055D.DLL" ALIAS "ClearAllDigital" ()
  'Declare SUB setDigitalChannel       LIB "K8055D.DLL" ALIAS "SetDigitalChannel" (BYVAL Channel As LONG)
  'Declare SUB SetAllDigital           LIB "K8055D.DLL" ALIAS "SetAllDigital" ()

  [b]DeclareDLL WriteAllDigital(aData.a)
  DeclareDLL ClearDigitalChannel(aChannel.a)
  DeclareDLL ClearAllDigital()
  DeclareDLL setDigitalChannel(aChannel.a)
  DeclareDLL SetAllDigital()
[/b]
  '=================================================================================='
  'Digital Input Functions (DI)                                                      '
  '=================================================================================='
  'Declare FUNCTION ReadDigitalChannel LIB "K8055D.DLL" ALIAS "ReadDigitalChannel" (BYVAL Channel As LONG) As LONG
  'Declare FUNCTION ReadAllDigital     LIB "K8055D.DLL" ALIAS "ReadAllDigital" () As LONG

  [b]DeclareDLL ReadDigitalChannel.a(aChannel.a)
  DeclareDLL ReadAllDigital.a()
[/b]
  '=================================================================================='
  'Functions for Counters                                                            '
  '=================================================================================='
  'Declare FUNCTION ReadCounter        LIB "K8055D.DLL" ALIAS "ReadCounter" (BYVAL CounterNr As LONG) As LONG
  'Declare SUB ResetCounter            LIB "K8055D.DLL" ALIAS "ResetCounter" (BYVAL CounterNr As LONG)
  'Declare SUB SetCounterDebounceTime  LIB "K8055D.DLL" ALIAS "SetCounterDebounceTime" (BYVAL CounterNr As LONG, BYVAL DebounceTime As LONG)
  
  [b]DeclareDLL ReadCounter.l(iCounter.l)
  DeclareDLL ResetCounter(iCounter.l)
  DeclareDLL SetCounterDebounceTime(iCounter.l, iDebounceTime.l)[/b]
  
 [b] Global isConnected.l[/b]
  
  [b]Procedure Init_k8055(card_address.l)
    Protected h.l
    
    h = OpenDevice(card_address)
    
    Select h
      Case 0, 1, 2, 3
        ProcedureReturn(1)
      Case -1
        ProcedureReturn(0)
    EndSelect
  EndProcedure[/b]
  
  'SUB Init_K8055(BYVAL card_address As LONG)
  '  LOCAL h As LONG
  '
  '  h = OpenDevice(card_address)
  '  Select Case h
  '      Case 0,1,2,3
  '          isConnected = 1
  '      Case -1
  '          isConnected = 0
  '  End Select
  'End SUB
  
  [b]Procedure processDigitalOutput.l(channel.l, state.s)
    If(channel < 0 Or channel > 8)
      ProcedureReturn(-1)
    Else
      Select state
        Case "ON"
          setDigitalChannel(channel)
          ProcedureReturn(1)
        Case "OFF"
          setDigitalChannel(channel)
          ProcedureReturn(0)
      EndSelect
    EndIf
  EndProcedure[/b]
  
  'FUNCTION processDigitalOutput(BYVAL channel As LONG, BYVAL state As STRING) As LONG
  '  If(channel < 0 Or channel > 8) THEN   '[1..8]
  '      FUNCTION = -1
  '  Else
  '      Select Case state
  '          Case "ON"
  '              setDigitalChannel(channel)
  '              FUNCTION = 1
  '          Case "OFF"
  '              setDigitalChannel(channel)
  '              FUNCTION = 0
  '      End Select
  '  End If
  'End FUNCTION
  
 [b] Procedure processAnalogOutput.l(channel.l, value.a)
    If(channel < 0 Or channel > 2)
      ProcedureReturn(-1)
    Else
      OutputAnalogChannel(channel, value)
      ProcedureReturn(value)
    EndIf 
  EndProcedure[/b]
    
  'FUNCTION processAnalogOutput(BYVAL channel As LONG, BYREF value As LONG) As LONG
  '  If(channel < 0 Or channel > 2) THEN   '[1..2]
  '      ' Invalid channel number
  '      FUNCTION -1
  '  Else
  '      ' Valid channel number. Set the channel
  '      OutputAnalogChannel(channel, value)
  '      FUNCTION = value
  '  End If
  'End FUNCTION
  
 [b] Procedure readDigitalInput.s(channel.l)
    If(channel < 0 Or channel > 5)
      ProcedureReturn(-1)
    Else
      If(ReadAnalogChannel(channel)) = "1"
        ProcedureReturn("ON")
      Else
        ProcedureReturn("OFF")
      EndIf
    EndIf  
  EndProcedure[/b]
  
  
 'FUNCTION readDigitalInput(BYVAL channel As LONG) As STRING
 '   If(channel < 0 Or channel > 5) THEN   '[1..5]
 '       ' invalid channel number
 '       FUNCTION = -1
 '   Else
 '       ' valid channel number. Read the channel
 '       FUNCTION = IIF$(ReadDigitalChannel(channel), "ON", "OFF")
 '   End If
 'End FUNCTION  
 
 [b]Procedure readAnalogInput.l(channel.l)
    If(channel < 0 Or channel > 2)
      ProcedureReturn(-1)
    Else
      ProcedureReturn(ReadAnalogChannel(channel))
    EndIf  
  EndProcedure[/b]
 
 'FUNCTION readAnalogInput(BYVAL channel As LONG) As LONG
 '   If(channel < 0 Or channel > 2) THEN    '[1..2]
 '       ' invalid channel number
 '       FUNCTION = -1
 '   Else
 '       ' valid channel number. Read the channel
 '       FUNCTION = ReadAnalogChannel(channel)
 '   End If
 'End FUNCTION

EndModule
Amilcar Matos
User
User
Posts: 43
Joined: Thu Nov 27, 2014 3:10 pm
Location: San Juan, Puerto Rico

Re: Is that correct from the K8055.DLL from velleman

Post by Amilcar Matos »

Hello Stephane!
Take a look at this;
http://www.purebasic.fr/english/viewtop ... =7&t=61153

The comment character in PureBasic is the semicolon.
Also there is no need for parenthesis in the procedure return statement.
I already did both changes to your code.
Would like to test the code but i can not do it now.

Code: Select all

DeclareModule k8055_library
 ; For the content of this code section see the DeclareModule page in PureBasic help file.
EndDeclareModule

Module k8055_library
  ;Amilcar says -> Here use ctrl-b to comment all the following lines.  
  ;                in PureBasic the comment character is the semicolon.
;   '----------------------------------------------------------------------------------
;   ' K8055 VELLEMAN-kit USB EXPERIMENT INTERFACE BOARD
;   ' HEADER INLCUDE FILE FOR K8055D.DLL  LIBRARY
;   ' FILE NAME: K8055D.INC
;   ' TRANSLATION BY JULES MARCHILDON, STEPHANE TO POWERBASIC FOR WINDOWS  PBWIN9 v9.03
;   '----------------------------------------------------------------------------------
; 
;   '=================================================================================='
;   'General Functions for Open and Close Library                                      '
;   '=================================================================================='
;   'Declare FUNCTION OpenDevice         LIB "K8055D.DLL" ALIAS "OpenDevice" (BYVAL CardAddress As LONG) As LONG
;   'Declare SUB CloseDevice             LIB "K8055D.DLL" ALIAS "CloseDevice" ()

  [b]DeclareDLL OpenDevice.l(cardaddress.l)
  DeclareDLL CloseDevice()[/b]
                                                                               
;   '=================================================================================='
;   'ADC Functions (Analog / Digital omzettingsprocedures  (Analog Input)              '
;   '=================================================================================='
;   'Declare FUNCTION ReadAnalogChannel  LIB "K8055D.DLL" ALIAS "ReadAnalogChannel"(BYVAL Channel As LONG) As LONG
;   'Declare SUB      ReadAllAnalog      LIB "K8055D.DLL" ALIAS "ReadAllAnalog" (BYVAL Data1 As LONG, BYVAL Data2 As LONG)

  [b]DeclareDLL ReadAnalogChannel.a(channel.a)
  DeclareDLL ReadAllAnalog(aData1.a,aData2.a)[/b]
 
;   '=================================================================================='
;   'DAC Functions (Digital / Analog omzettingsprocedures  (Analog Output)             '
;   '=================================================================================='
;   'Declare SUB OutputAnalogChannel     LIB "K8055D.DLL" ALIAS "OutputAnalogChannel" (BYVAL Channel As LONG, BYVAL oData As LONG)
;   'Declare SUB OutputAllAnalog         LIB "K8055D.DLL" ALIAS "OutputAllAnalog" (BYVAL Data1 As LONG, BYVAL Data2 As LONG)
;   'Declare SUB ClearAnalogChannel      LIB "K8055D.DLL" ALIAS "ClearAnalogChannel" (BYVAL Channel As LONG)
;   'Declare SUB ClearAllAnalog          LIB "K8055D.DLL" ALIAS "ClearAllAnalog" ()
;   'Declare SUB setAnalogChannel        LIB "K8055D.DLL" ALIAS "SetAnalogChannel" (BYVAL Channel As LONG )
;   'Declare SUB SetAllAnalog            LIB "K8055D.DLL" ALIAS "SetAllAnalog" ()

  [b]DeclareDLL OutputAnalogChannel(aChannel.a,aData.a)
  DeclareDLL OutputAllAnalog(aData1.a, aData2.a)
  DeclareDLL ClearAnalogChannel(aChannel.a)
  DeclareDLL ClearAllAnalog()
  DeclareDLL setAnalogChannel(aChannel.a)
  DeclareDLL SetAllAnalog()
[/b]
;   '=================================================================================='
;   'Digital Output Functions (DO)                                                     '
;   '=================================================================================='
;   'Declare SUB WriteAllDigital         LIB "K8055D.DLL" ALIAS "WriteAllDigital" (BYVAL aData As LONG)
;   'Declare SUB ClearDigitalChannel     LIB "K8055D.DLL" ALIAS "ClearDigitalChannel" (BYVAL Channel As LONG)
;   'Declare SUB ClearAllDigital         LIB "K8055D.DLL" ALIAS "ClearAllDigital" ()
;   'Declare SUB setDigitalChannel       LIB "K8055D.DLL" ALIAS "SetDigitalChannel" (BYVAL Channel As LONG)
;   'Declare SUB SetAllDigital           LIB "K8055D.DLL" ALIAS "SetAllDigital" ()

  [b]DeclareDLL WriteAllDigital(aData.a)
  DeclareDLL ClearDigitalChannel(aChannel.a)
  DeclareDLL ClearAllDigital()
  DeclareDLL setDigitalChannel(aChannel.a)
  DeclareDLL SetAllDigital()
[/b]
;   '=================================================================================='
;   'Digital Input Functions (DI)                                                      '
;   '=================================================================================='
;   'Declare FUNCTION ReadDigitalChannel LIB "K8055D.DLL" ALIAS "ReadDigitalChannel" (BYVAL Channel As LONG) As LONG
;   'Declare FUNCTION ReadAllDigital     LIB "K8055D.DLL" ALIAS "ReadAllDigital" () As LONG

  [b]DeclareDLL ReadDigitalChannel.a(aChannel.a)
  DeclareDLL ReadAllDigital.a()
[/b]
;   '=================================================================================='
;   'Functions for Counters                                                            '
;   '=================================================================================='
;   'Declare FUNCTION ReadCounter        LIB "K8055D.DLL" ALIAS "ReadCounter" (BYVAL CounterNr As LONG) As LONG
;   'Declare SUB ResetCounter            LIB "K8055D.DLL" ALIAS "ResetCounter" (BYVAL CounterNr As LONG)
;   'Declare SUB SetCounterDebounceTime  LIB "K8055D.DLL" ALIAS "SetCounterDebounceTime" (BYVAL CounterNr As LONG, BYVAL DebounceTime As LONG)
 
  [b]DeclareDLL ReadCounter.l(iCounter.l)
  DeclareDLL ResetCounter(iCounter.l)
  DeclareDLL SetCounterDebounceTime(iCounter.l, iDebounceTime.l)[/b]
 
 [b] Global isConnected.l[/b]
 
  [b]Procedure Init_k8055(card_address.l)
    Protected h.l
   
    h = OpenDevice(card_address)
   
    Select h
      Case 0, 1, 2, 3
  ;Amilcar says -> No need for parenthesis in the procedure return statement.
        ProcedureReturn 1
      Case -1
        ProcedureReturn 0
    EndSelect
  EndProcedure[/b]
 
;   'SUB Init_K8055(BYVAL card_address As LONG)
;   '  LOCAL h As LONG
;   '
;   '  h = OpenDevice(card_address)
;   '  Select h
;   '      Case 0,1,2,3
;   '          isConnected = 1
;   '      Case -1
;   '          isConnected = 0
;   '  End Select
;   'End SUB
 
  [b]Procedure processDigitalOutput.l(channel.l, state.s)
    If(channel < 0 Or channel > 8)
      ProcedureReturn -1
    Else
      Select state
        Case "ON"
          setDigitalChannel(channel)
          ProcedureReturn 1
        Case "OFF"
          setDigitalChannel(channel)
          ProcedureReturn 0
      EndSelect
    EndIf
  EndProcedure[/b]
 
;   'FUNCTION processDigitalOutput(BYVAL channel As LONG, BYVAL state As STRING) As LONG
;   '  If(channel < 0 Or channel > 8) THEN   '[1..8]
;   '      FUNCTION = -1
;   '  Else
;   '      Select Case state
;   '          Case "ON"
;   '              setDigitalChannel(channel)
;   '              FUNCTION = 1
;   '          Case "OFF"
;   '              setDigitalChannel(channel)
;   '              FUNCTION = 0
;   '      End Select
;   '  End If
;   'End FUNCTION
 
 [b] Procedure processAnalogOutput.l(channel.l, value.a)
    If(channel < 0 Or channel > 2)
      ProcedureReturn -1
    Else
      OutputAnalogChannel(channel, value)
      ProcedureReturn value
    EndIf
  EndProcedure[/b]
   
;   'FUNCTION processAnalogOutput(BYVAL channel As LONG, BYREF value As LONG) As LONG
;   '  If(channel < 0 Or channel > 2) THEN   '[1..2]
;   '      ' Invalid channel number
;   '      FUNCTION -1
;   '  Else
;   '      ' Valid channel number. Set the channel
;   '      OutputAnalogChannel(channel, value)
;   '      FUNCTION = value
;   '  End If
;   'End FUNCTION
 
 [b] Procedure readDigitalInput.s(channel.l)
    If(channel < 0 Or channel > 5)
      ProcedureReturn -1
    Else
      If(ReadAnalogChannel(channel)) = "1"
        ProcedureReturn "ON"
      Else
        ProcedureReturn "OFF"
      EndIf
    EndIf 
  EndProcedure[/b]
 
 
;  'FUNCTION readDigitalInput(BYVAL channel As LONG) As STRING
;  '   If(channel < 0 Or channel > 5) THEN   '[1..5]
;  '       ' invalid channel number
;  '       FUNCTION = -1
;  '   Else
;  '       ' valid channel number. Read the channel
;  '       FUNCTION = IIF$(ReadDigitalChannel(channel), "ON", "OFF")
;  '   End If
;  'End FUNCTION 
 
 [b]Procedure readAnalogInput.l(channel.l)
    If(channel < 0 Or channel > 2)
      ProcedureReturn -1
    Else
      ProcedureReturn ReadAnalogChannel(channel)
    EndIf 
  EndProcedure[/b]
  
;  'FUNCTION readAnalogInput(BYVAL channel As LONG) As LONG
;  '   If(channel < 0 Or channel > 2) THEN    '[1..2]
;  '       ' invalid channel number
;  '       FUNCTION = -1
;  '   Else
;  '       ' valid channel number. Read the channel
;  '       FUNCTION = ReadAnalogChannel(channel)
;  '   End If
;  'End FUNCTION

EndModule
User avatar
spikey
Enthusiast
Enthusiast
Posts: 747
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: Is that correct from the K8055.DLL from velleman

Post by spikey »

In a word no but don't be disheartened, you've gone wrong in a logical way!

In PureBasic the DeclareDLL and ProcedureDLL commands are used to *export* a function to a library not to import one.
The commands you need to read up on are Prototype, OpenLibrary and GetFunction, they will do what you want.

http://www.purebasic.com/documentation/ ... types.html
http://www.purebasic.com/documentation/ ... brary.html
http://www.purebasic.com/documentation/ ... ction.html

OpenLibrary will need the full path to the library file at runtime not just the file name. It won't search for it automatically.
You'll need to allow for this unless it will be installed to the system folder.

You also need to double check your usage of the .a datatype. This isn't large enough to hold a long only a byte.

The returntype of a procedure is declared after the Procedure keyword not the function name.
So, for example, your line

Code: Select all

Procedure processDigitalOutput.l(channel.l, state.s)
should read

Code: Select all

Procedure.l processDigitalOutput(channel.l, state.s)
Additionally if you want to return a value you must declare it.
So your line

Code: Select all

Procedure Init_k8055(card_address.l)
should read

Code: Select all

Procedure.L Init_k8055(card_address.l)


Incidentally ProcedureReturn doesn't require brackets around the value, although there's nothing to stop you if you prefer it.

This would be how a few of the function imports would look - it should point you in the right direction...

Code: Select all

Prototype.l ProtoOpenDevice(CardAddress.l)
Prototype ProtoCloseDevice()
Prototype.l ProtoReadAnalogChannel(Channel.l)

Global OpenDevice.ProtoOpenDevice
Global CloseDevice.ProtoCloseDevice
Global ReadAnalogChannel.ProtoReadAnalogChannel

If OpenLibrary(0, "\path\K8055D.DLL")
  OpenDevice = GetFunction(0, "OpenDevice")
  CloseDevice = GetFunction(0, "CloseDevice")
  ReadAnalogChannel = GetFunction(0, "ReadAnalogChannel")
EndIf
Last edited by spikey on Sat Sep 19, 2015 1:17 pm, edited 2 times in total.
infratec
Always Here
Always Here
Posts: 7575
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Is that correct from the K8055.DLL from velleman

Post by infratec »

Hi,

there is a simple way to check the syntax:

Press the compile button.

Or, from the menu, 'Compiler'->'Syntax check'
Stephane Fonteyne
User
User
Posts: 27
Joined: Sun Sep 06, 2015 2:22 pm

Re: Is that correct from the K8055.DLL from velleman

Post by Stephane Fonteyne »

infratec wrote:Hi,

there is a simple way to check the syntax:

Press the compile button.

Or, from the menu, 'Compiler'->'Syntax check'
Hi,

Can you please repley to my post with my example of SPI example:
http://www.purebasic.fr/english/viewtop ... 13&t=63248

Thanks
Stephane
infratec
Always Here
Always Here
Posts: 7575
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Is that correct from the K8055.DLL from velleman

Post by infratec »

Stephane Fonteyne wrote:Can you please repley to my post with my example of SPI example:
http://www.purebasic.fr/english/viewtop ... 13&t=63248
Hi,

no, I don't want to reply.
I'm helping nearly always and on most problems if I think I have enough knowledge to do it.
But at some point I have to stop.
My time is limited, and I'm not someones personal software coder.

To implement the SPI was a simple task especially when you have already the example of I²C.
I don't know why you didn't done it on your own.
Also you gave never replies if the code is/was working or not.
So I was nearly blind.

You can get help here if you show us code which has a problem inside.
But the stuff above is an other example why I'm not willing to help anymore.
A simple compile shows you enough hint's that and why this is never working in PureBasic.
If you don't have the time and the interest to solve the basics, than I and others have also no interest.

I wish you the best for your project (what ever it is) and hope you'll find your way to PureBasic.


Bernd
Stephane Fonteyne
User
User
Posts: 27
Joined: Sun Sep 06, 2015 2:22 pm

Re: Is that correct from the K8055.DLL from velleman

Post by Stephane Fonteyne »

infratec wrote:
Stephane Fonteyne wrote:Can you please repley to my post with my example of SPI example:
http://www.purebasic.fr/english/viewtop ... 13&t=63248
Hi,

no, I don't want to reply.
I'm helping nearly always and on most problems if I think I have enough knowledge to do it.
But at some point I have to stop.
My time is limited, and I'm not someones personal software coder.

To implement the SPI was a simple task especially when you have already the example of I²C.
I don't know why you didn't done it on your own.
Also you gave never replies if the code is/was working or not.
So I was nearly blind.

You can get help here if you show us code which has a problem inside.
But the stuff above is an other example why I'm not willing to help anymore.
A simple compile shows you enough hint's that and why this is never working in PureBasic.
If you don't have the time and the interest to solve the basics, than I and others have also no interest.

I wish you the best for your project (what ever it is) and hope you'll find your way to PureBasic.


Bernd
Your SPI code works. But I'm so busy at other works for electronics. When I have time I repley your posts.
Stephane Fonteyne
User
User
Posts: 27
Joined: Sun Sep 06, 2015 2:22 pm

Re: Is that correct from the K8055.DLL from velleman

Post by Stephane Fonteyne »

Hi,

I rewrite the code with the syntax of PureBasic. Iqs that correct.
How can I use the functions from the library k8055.dll

Code: Select all


;Import "k8055.lib"
  Prototype.l _openDevice(cardaddress.l)
  Prototype _closeDevice()
  
  Prototype.a _readAnalogChannel(channel.a)
  Prototype.a _readAllAnalog(aData1.a,aData2.a)
  
  Prototype _outputAnalogChannel(aChannel.a,aData.a)
  Prototype _outputAllAnalog(aData1.a, aData2.a)
  Prototype _clearAnalogChannel(aChannel.a)
  Prototype _clearAllAnalog()
  Prototype _setAnalogChannel(aChannel.a)
  Prototype _setAllAnalog()
  
  Prototype _writeAllDigital(aData.a)
  Prototype _clearDigitalChannel(aChannel.a)
  Prototype _clearAllDigital()
  Prototype _setDigitalChannel(aChannel.a)
  Prototype _setAllDigital()
  
  Prototype.a _readDigitalChannel(aChannel.a)
  Prototype.a _readAllDigital()
  
  Prototype.l _readCounter(iCounter.l)
  Prototype _resetCounter(iCounter.l)
  Prototype _setCounterDebounceTime(iCounter.l, iDebounceTime.l)
  
  Global openDevice._openDevice
  Global closeDevice._closeDevice
  
  Global readAnalogChannel._readAnalogChannel
  Global readAllAnalog._readAllAnalog
  
  Global outputAnalogChannel._outputAnalogChannel
  Global outputAllAnalog._outputAllAnalog
  Global clearAnalogChannel._clearAnalogChannel
  Global clearAllAnalog._clearAllAnalog
  Global setAnalogChannel._setAnalogChannel
  Global setAllAnalog._setAllAnalog
  
  Global writeAllDigital._writeAllDigital
  Global clearDigitalChannel._clearDigitalChannel
  Global clearAllDigital._clearAllDigital
  Global setDigitalChannel._setDigitalChannel
  Global setAllDigital._setAllDigital
  
  Global readDigitalChannel._ReadDigitalChannel
  Global readAllDigital._readAllDigital
  
  Global readCounter._readCounter
  Global resetCounter._resetCounter
  Global setCounterDebounceTime._setCounterDebounceTime
  
  If OpenLibrary(0,"k8055.dll")
    openDevice  = GetFunction(0,"openDevice")
    closeDevice = GetFunction(0,"closeDevice")
    readAnalogChannel = GetFunction(0,"readAnalogChannel")
    readAllAnalog = GetFunction(0,"readAllAnalog")
    outputAnalogChannel = GetFunction(0,"outputAnalogChannel")
    outputAllAnalog = GetFunction(0,"outputAllAnalog")
    clearAnalogChannel = GetFunction(0,"clearAnalogChannel")
    clearAllAnalog = GetFunction(0,"clearAllAnalog")
    setAnalogChannel = GetFunction(0,"setAnalogChannel")
    setAllAnalog = GetFunction(0,"setAllAnalog")
    writeAllDigital = GetFunction(0,"writeAllDigital")
    clearDigitalChannel = GetFunction(0,"clearDigitalChannel")
    clearAllDigital = GetFunction(0,"clearAllDigital")
    setDigitalChannel = GetFunction(0,"setDigitalChannel")
    setAllDigital = GetFunction(0,"setAllDigital")
    readDigitalChannel = GetFunction(0,"readDigitalChannel")
    readAllDigital = GetFunction(0,"readAllDigital")
    readCounter = GetFunction(0,"readCounter")
    resetCounter = GetFunction(0,"resetCounter")
    setCounterDebounceTime = GetFunction(0,"setCounterDebounceTime")
  EndIf
  
;EndImport

Global isConnected.l
  
Procedure.l Init_k8055(card_address.l)
  Protected h.l
    
  h = OpenDevice(card_address)
    
  Select h
    Case 0, 1, 2, 3
      ProcedureReturn(1)
    Case -1
      ProcedureReturn(0)
  EndSelect
EndProcedure

Procedure.l processDigitalOutput(channel.l, state.s)
  If(channel < 0 Or channel > 8)
    ProcedureReturn(-1)
  Else
    Select state
      Case "ON"
        setDigitalChannel(channel)
        ProcedureReturn(1)
      Case "OFF"
        setDigitalChannel(channel)
        ProcedureReturn(0)
    EndSelect
  EndIf
EndProcedure

Procedure.l processAnalogOutput(channel.l, value.a)
  If(channel < 0 Or channel > 2)
    ProcedureReturn(-1)
  Else
    OutputAnalogChannel(channel, value)
    ProcedureReturn(value)
  EndIf 
EndProcedure

;Procedure.s readDigitalInput(channel.l)
;  If(channel < 0 Or channel > 5)
;    ProcedureReturn(-1)
;  Else
;    If(ReadAnalogChannel(channel)) = "1"
;      ProcedureReturn("ON")
;    Else
;      ProcedureReturn("OFF")
;    EndIf
;  EndIf  
;EndProcedure

Procedure.l readAnalogInput(channel.l)
  If(channel < 0 Or channel > 2)
    ProcedureReturn(-1)
  Else
    ProcedureReturn(ReadAnalogChannel(channel))
  EndIf  
EndProcedure
How can I implement this string and long datatype?

Procedure readDigitalInput.s(channel.l)
If(channel < 0 Or channel > 5)
ProcedureReturn(-1)
Else
If(ReadAnalogChannel(channel)) = "1"
ProcedureReturn("ON")
Else
ProcedureReturn("OFF")
EndIf
EndIf
EndProcedure


;FUNCTION readDigitalInput(BYVAL channel As LONG) As STRING
; If(channel < 0 Or channel > 5) THEN ;[1..5]
; ; invalid channel number
; FUNCTION = -1
; Else
; ; valid channel number. Read the channel
; FUNCTION = IIF$(ReadDigitalChannel(channel), "ON", "OFF")
; End If
;End FUNCTION
User avatar
blueb
Addict
Addict
Posts: 1111
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: Is that correct from the K8055.DLL from velleman

Post by blueb »

Code: Select all

Procedure.s ReadAnalogChannel(channel.l)
     
     ProcedureReturn "1"
    ; ProcedureReturn "not 1"            ;<<<<<<<<<< test here by 
EndProcedure

Procedure.s readDigitalInput (channel.l)
    
     If(channel < 0 Or channel > 5)
          Debug "out of range"
          ProcedureReturn
 
     Else
           If ReadAnalogChannel(channel) = "1"
                    ProcedureReturn "ON"
           Else
                    ProcedureReturn "OFF"
           EndIf
      EndIf 
 EndProcedure
 
 channel.l = 5                                     ;<<<<<<<<<< test and here
 
Debug readDigitalInput (Channel)

NOTE: I complied with your wishes.. but I would use .i instead of .l so there won't be any surprises with 64-bit versions.

by the way... this took less than 2 minutes....
asking questions does no good, if you won't do the work. 8)
- It was too lonely at the top.

System : PB 6.21(x64) and Win 11 Pro (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
User avatar
W4GNS
User
User
Posts: 31
Joined: Tue Jan 04, 2011 5:51 am
Location: Virginia

Re: Is that correct from the K8055.DLL from velleman

Post by W4GNS »

blueb wrote:

Code: Select all

 by the way... this took less than 2 minutes.... 
 asking questions does no good, if you won't do the work.  8)[/quote]

 His time problem in part is due to the amount of compilers he is using, two others not counting Purebasic, that I'm aware of. And he is active on those compiler forums expecting others to do his coding.  :shock: 
 Obviously, if he has incorrectly commented his source, reading the Purebasic Manual (which is detailed enough to get anyone running with PB) never happened.  :(
Stephane Fonteyne
User
User
Posts: 27
Joined: Sun Sep 06, 2015 2:22 pm

Re: Is that correct from the K8055.DLL from velleman

Post by Stephane Fonteyne »

Hi bleeb

When I try to compile it give me a syntax error:

"Line119: Prototype variabele already declared readAnalogChannel"

Code: Select all


;Import "k8055.lib"
  Prototype.l _openDevice(cardaddress.l)
  Prototype _closeDevice()
  
  Prototype.a _readAnalogChannel(channel.a)
  Prototype.a _readAllAnalog(aData1.a,aData2.a)
  
  Prototype _outputAnalogChannel(aChannel.a,aData.a)
  Prototype _outputAllAnalog(aData1.a, aData2.a)
  Prototype _clearAnalogChannel(aChannel.a)
  Prototype _clearAllAnalog()
  Prototype _setAnalogChannel(aChannel.a)
  Prototype _setAllAnalog()
  
  Prototype _writeAllDigital(aData.a)
  Prototype _clearDigitalChannel(aChannel.a)
  Prototype _clearAllDigital()
  Prototype _setDigitalChannel(aChannel.a)
  Prototype _setAllDigital()
  
  Prototype.a _readDigitalChannel(aChannel.a)
  Prototype.a _readAllDigital()
  
  Prototype.l _readCounter(iCounter.l)
  Prototype _resetCounter(iCounter.l)
  Prototype _setCounterDebounceTime(iCounter.l, iDebounceTime.l)
  
  Global openDevice._openDevice
  Global closeDevice._closeDevice
  
  Global readAnalogChannel._readAnalogChannel
  Global readAllAnalog._readAllAnalog
  
  Global outputAnalogChannel._outputAnalogChannel
  Global outputAllAnalog._outputAllAnalog
  Global clearAnalogChannel._clearAnalogChannel
  Global clearAllAnalog._clearAllAnalog
  Global setAnalogChannel._setAnalogChannel
  Global setAllAnalog._setAllAnalog
  
  Global writeAllDigital._writeAllDigital
  Global clearDigitalChannel._clearDigitalChannel
  Global clearAllDigital._clearAllDigital
  Global setDigitalChannel._setDigitalChannel
  Global setAllDigital._setAllDigital
  
  Global readDigitalChannel._ReadDigitalChannel
  Global readAllDigital._readAllDigital
  
  Global readCounter._readCounter
  Global resetCounter._resetCounter
  Global setCounterDebounceTime._setCounterDebounceTime
  
  If OpenLibrary(0,"k8055.dll")
    openDevice  = GetFunction(0,"openDevice")
    closeDevice = GetFunction(0,"closeDevice")
    readAnalogChannel = GetFunction(0,"readAnalogChannel")
    readAllAnalog = GetFunction(0,"readAllAnalog")
    outputAnalogChannel = GetFunction(0,"outputAnalogChannel")
    outputAllAnalog = GetFunction(0,"outputAllAnalog")
    clearAnalogChannel = GetFunction(0,"clearAnalogChannel")
    clearAllAnalog = GetFunction(0,"clearAllAnalog")
    setAnalogChannel = GetFunction(0,"setAnalogChannel")
    setAllAnalog = GetFunction(0,"setAllAnalog")
    writeAllDigital = GetFunction(0,"writeAllDigital")
    clearDigitalChannel = GetFunction(0,"clearDigitalChannel")
    clearAllDigital = GetFunction(0,"clearAllDigital")
    setDigitalChannel = GetFunction(0,"setDigitalChannel")
    setAllDigital = GetFunction(0,"setAllDigital")
    readDigitalChannel = GetFunction(0,"readDigitalChannel")
    readAllDigital = GetFunction(0,"readAllDigital")
    readCounter = GetFunction(0,"readCounter")
    resetCounter = GetFunction(0,"resetCounter")
    setCounterDebounceTime = GetFunction(0,"setCounterDebounceTime")
  EndIf
  
;EndImport

Global isConnected.l
  
Procedure.l Init_k8055(card_address.l)
  Protected h.l
    
  h = OpenDevice(card_address)
    
  Select h
    Case 0, 1, 2, 3
      ProcedureReturn(1)
    Case -1
      ProcedureReturn(0)
  EndSelect
EndProcedure

Procedure.l processDigitalOutput(channel.l, state.s)
  If(channel < 0 Or channel > 8)
    ProcedureReturn(-1)
  Else
    Select state
      Case "ON"
        setDigitalChannel(channel)
        ProcedureReturn(1)
      Case "OFF"
        setDigitalChannel(channel)
        ProcedureReturn(0)
    EndSelect
  EndIf
EndProcedure

Procedure.l processAnalogOutput(channel.l, value.a)
  If(channel < 0 Or channel > 2)
    ProcedureReturn(-1)
  Else
    OutputAnalogChannel(channel, value)
    ProcedureReturn(value)
  EndIf 
EndProcedure


Procedure.s readAnalogChannel(channel.l)
  ProcedureReturn("1")
EndProcedure  

Procedure.s readDigitalInput(channel.l)
  If(channel < 0 Or channel > 5)
    Debug "Out of range"
    ProcedureReturn
  Else
    If(ReadAnalogChannel(channel)) = "1"
      ProcedureReturn("ON")
    Else
      ProcedureReturn("OFF")
    EndIf
  EndIf  
EndProcedure

channel.l = 5
Debug readDigitalInput(channel)


Procedure.l readAnalogInput(channel.l)
  If(channel < 0 Or channel > 2)
    ProcedureReturn(-1)
  Else
    ProcedureReturn(ReadAnalogChannel(channel))
  EndIf  
EndProcedure

What's wrong here?
User avatar
spikey
Enthusiast
Enthusiast
Posts: 747
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: Is that correct from the K8055.DLL from velleman

Post by spikey »

Stephane Fonteyne wrote:When I try to compile it give me a syntax error:"Line119: Prototype variabele already declared readAnalogChannel"
You're trying to define a Procedure with the same name as a Prototype. A prototype is an imported procedure so you're trying to have two procedures with the same name in the same scope - no can do! One of them will need to change name...
Post Reply