VM167 with HTBasic

Hi,
please find below a little HTBasic program to use VELLEMAN VM167 Board
Everything working well except 2 functions:
1/Connected : Always return 0 (Not found)
2/ReadBackInOutMode : Always return 0 ([1-8] OUTPUT)
Some ideas?

          !*****************************************************************
          ! VELLEMAN BOARD - HTBASIC Software
          ! VM167/DLL  (Mini USB Interface Board)
          !*****************************************************************
          CLEAR SCREEN
          KEY LABELS OFF
          PRINTER IS CRT
          DLL UNLOAD ALL
          !
          DIM Dll$[10]            ! DLL      version -.-.-.-
          DIM Firmware$[10] ! Firmware version -.-.-.-
          DIM Digital(1:8)        
          !
          LONG Retour          ! Retour DLL type Integer
          LONG Card_address       
          LONG Channel            
          LONG Dll                
          LONG Firmware           
          LONG Data1,Data2        
          LONG Counter            
          LONG Low,High           
          ALLOCATE LONG Buffer(0:8)
          ALLOCATE LONG Pwm(0:1)  
          !
          Tempo=1
          !
          !*****************************************************************
          ! Directory Definition : Program & DLL
          !*****************************************************************
          Disque1$=SYSTEM$("MSI")     ! Actual Directory
          MASS STORAGE IS Disque1$    
          !
          !*****************************************************************

Dll1: ! LOAD DLL VELLEMAN
!*****************************************************************
Dll$=“VM167”
DISP “—> DLL VELLEMAN “;Dll$;” Loading in progress …”
DLL LOAD Dll$
WAIT Tempo
DISP
!
!*****************************************************************
Menu: ! Program TASK
!*****************************************************************
!---------------------------------------------------------
! CONFIGURATION commands
!---------------------------------------------------------
GOSUB Dll
GOSUB Connected ! Connected => NOT CORRECT !!! Always 0
GOSUB Open
GOSUB Firmware
!
!---------------------------------------------------------
! DIGITAL commands
!---------------------------------------------------------
GOSUB Clr_all_digital ! ClearAllDigital
WAIT .5
!
Low=1 ! [1-4]=Input
High=1 ! [5-8]=Input
GOSUB Inout_mode ! InOutMode
GOSUB Rd_back_iom ! ReadBackInOutMode => NOT CORRECT !!!"
!
Low=0 ! [1-4]=Output
High=0 ! [1-8]=Output
GOSUB Inout_mode ! InOutMode
GOSUB Rd_back_iom ! ReadBackInOutMode
!
FOR J=1 TO 2
FOR I=1 TO 8
Digit=I
GOSUB Set_digital_ch ! SetDigitalChannel
WAIT Tempo/10
NEXT I
GOSUB Clr_all_digital ! ClearAllDigital
WAIT Tempo/10
!
FOR I=8 TO 1 STEP -1
Digit=I
GOSUB Set_digital_ch ! SetDigitalChannel
WAIT Tempo/10
NEXT I
GOSUB Clr_all_digital ! ClearAllDigital
WAIT Tempo/10
NEXT J
!
WAIT Tempo
GOSUB Set_all_digital ! SetAllDigital
WAIT Tempo
GOSUB Clr_all_digital ! ClearAllDigital
WAIT Tempo
!
Value=139 ! Value [0-255] to program
GOSUB Out_all_digital ! OutputAllDigital
WAIT Tempo
!
GOSUB Rd_all_digital ! ReadAllDigital
Channel=1 ! [1-8]
GOSUB Rd_digital_ch ! ReadDigitalChannel
GOSUB Clr_digital_ch ! ClearDigitalChannel
GOSUB Rd_digital_ch ! ReadDigitalChannel
!
!---------------------------------------------------------
! PWM commands
!---------------------------------------------------------
FOR I=1 TO 2
Channel=I
FOR Dty=0 TO 100 STEP 25
GOSUB Set_pwm
WAIT Tempo/10
NEXT Dty
Dty=0
GOSUB Set_pwm
WAIT Tempo/10
NEXT I
!
Channel=1 ! Output Channel [1-2]
Dty=25 ! [0-255] Duty Cycle = 0 --> 0%
GOSUB Set_pwm ! SetPWM 255 --> 100%
!
Channel=2 ! Output Channel [1-2]
Dty=75 ! [0-255] Duty Cycle = 0 --> 0%
GOSUB Set_pwm ! SetPWM 255 --> 100%
!
GOSUB Rd_back_pwm ! ReadBackPWMOut
!
!---------------------------------------------------------
! ANALOG commands
!---------------------------------------------------------
GOSUB Rd_all_analog ! ReadAllAnalog
Channel=1
GOSUB Rd_analog_ch ! ReadAnalogChannel
!
!---------------------------------------------------------
! COUNTER commands
!---------------------------------------------------------
Channel=1
GOSUB Clr_digital_ch ! ClearDigitalChannel
WAIT Tempo
GOSUB Reset_counter ! ResetCounter
GOSUB Rd_counter ! ReadCounter
Clock=10
WHILE Counter<>Clock
GOSUB Set_digital_ch ! SetDigitalChannel
WAIT Tempo/10
GOSUB Rd_counter ! ReadCounter
GOSUB Clr_digital_ch ! ClearDigitalChannel
WAIT Tempo/10
END WHILE
!
WAIT Tempo
DISP
!
!---------------------------------------------------------
Exit: ! EXIT
!---------------------------------------------------------
GOSUB Clr_all_digital ! ClearAllDigital
Dty1=0
Dty2=0
GOSUB Out_all_pwm ! OutputAllPWM
GOSUB Close
PRINT
GOSUB Liste
GOTO End
!
!
!---------------------------------------------------------
Liste: ! List DLL loaded
!---------------------------------------------------------
LIST DLL
RETURN
!
!
!---------------------------------------------------------
Dll: ! DLL software VERSION number
!---------------------------------------------------------
DISP “—> VERSION DLL …”
!
ON ERROR GOSUB Er_dll
DLL GET “STDCALL LONG VM167:VersionDLL” AS “Version_dll”
OFF ERROR
!
!------------------------------------
! Response = 32 bit integer
! DLL = 4 digits [-.-.-.-]
! 1 digit = 1 byte [12345678]
!------------------------------------
Dll=-1000
Dll=FNVersion_dll
!
Data=Dll
GOSUB Decod
!
Dll$=VAL$(Dec1)&"."&VAL$(Dec2)&"."&VAL$(Dec3)&"."&VAL$(Dec4)
!
PRINT Digit1$,"->";Dec1
PRINT Digit2$,"->";Dec2
PRINT Digit3$,"->";Dec3
PRINT Digit4$,"->";Dec4
PRINT “DLL Version # : “;Dll$
PRINT
!
RETURN
!
!
!---------------------------------------------------------
Open: ! OPEN Device
!---------------------------------------------------------
DISP “—> OPEN DEVICE …”
!
ON ERROR GOSUB Er_dll
DLL GET “STDCALL LONG VM167:OpenDevices” AS “Open_device”
OFF ERROR
!
!------------------- Jumpers on J167 Board
! J5 : Card Address
!-------------------
! ON : # 0
! OFF : # 1
!------------------
!
Retour=-1000
Retour=FNOpen_device
!
SELECT Retour
CASE 0
PRINT “VM167 VELLEMAN Card OPEN error !!!”
GOTO End
CASE 1
Card_address=0
PRINT “VM167 VELLEMAN Board #”;Card_address;”: OPEN”
CASE 2
Card_address=1
PRINT “VM167 VELLEMAN Board #”;Card_address;": OPEN"
CASE 3
Card_address=12
PRINT “VM167 VELLEMAN Boards #1 and #2 OPEN”
CASE -1
PRINT “No VM167 VELLEMAN Card found !!!”
GOTO End
END SELECT
PRINT
!
RETURN
!
!
!---------------------------------------------------------
Firmware: ! FIRMWARE version
!---------------------------------------------------------
DISP “—> VERSION FIRMWARE …”
!
ON ERROR GOSUB Er_dll
DLL GET “STDCALL LONG VM167:VersionFirmware” AS “Firmware”
OFF ERROR
!
SELECT Card_address
CASE 0,1,12
Firmware=-1000
IF Card_address=12 THEN Card_address=0 ! The first one …
!
Firmware=FNFirmware((Card_address))
!
Data=Firmware
GOSUB Decod
!
Firmware$=VAL$(Dec1)&"."&VAL$(Dec2)&"."&VAL$(Dec3)&"."&VAL$(Dec4)
!
PRINT Digit1$,"->";Dec1
PRINT Digit2$,"->";Dec2
PRINT Digit3$,"->";Dec3
PRINT Digit4$,"->";Dec4
PRINT "Firmware Version # : ";Firmware$
PRINT
!
END SELECT
!
RETURN
!
!
!---------------------------------------------------------
Rd_analog_ch: ! ReadAnalogChannel
!---------------------------------------------------------
DISP “—> READ ANALOG CHANNEL …”
!
ON ERROR GOSUB Er_dll
DLL GET “STDCALL LONG VM167:ReadAnalogChannel” AS “Rd_analog_ch”
OFF ERROR
!
Retour=-1000
Retour=FNRd_analog_ch((Card_address),(Channel)) ! [1-5]
!
Data=INT((5/1024)Retour) ! [0-1024] 11[11111111] [0,+5v]
PRINT “ANALOG#”;Channel;"=";Data;" ("&VAL$(Retour)&")"
PRINT
!
RETURN
!
!
!---------------------------------------------------------
Rd_all_analog: ! ReadAllAnalog
!---------------------------------------------------------
DISP “—> READ ALL ANALOG …”
!
ON ERROR GOSUB Er_dll
DLL GET “STDCALL VOID VM167:ReadAllAnalog” AS “Rd_all_analog”
OFF ERROR
!
! Buffer : Pointer to an Array of Five 32-bit integers where the data will be read
! [0-1023] = 0 +5v
!
DEALLOCATE Buffer(
)
ALLOCATE LONG Buffer(1:5)
!
FOR I=1 TO 5
Buffer(I)=-1000
NEXT I
!
Rd_all_analog((Card_address),Buffer(1))
!
FOR I=1 TO 5
Data=INT((5/1024)Buffer(I))
PRINT “ANALOG#”;I;":";Data;" ("&VAL$(Buffer(I))&")"
NEXT I
PRINT
!
RETURN
!
!
!---------------------------------------------------------
Clr_digital_ch: ! ClearDigitalChannel
!---------------------------------------------------------
DISP “—> CLEAR DIGITAL CHANNEL …”
!
ON ERROR GOSUB Er_dll
DLL GET “STDCALL VOID VM167:ClearDigitalChannel” AS “Clr_digital_ch”
OFF ERROR
!
Clr_digital_ch((Card_address),(Channel)) ! [1-8]
!
RETURN
!
!
!---------------------------------------------------------
Clr_all_digital: ! ClearAllDigital
!---------------------------------------------------------
DISP “—> CLEAR ALL DIGITAL …”
!
ON ERROR GOSUB Er_dll
DLL GET “STDCALL VOID VM167:ClearAllDigital” AS “Clr_all_digital”
OFF ERROR
!
Clr_all_digital((Card_address))
!
RETURN
!
!
!---------------------------------------------------------
Set_digital_ch: ! SetDigitalChannel
!---------------------------------------------------------
DISP “—> SET DIGITAL CHANNEL …”
!
ON ERROR GOSUB Er_dll
DLL GET “STDCALL VOID VM167:SetDigitalChannel” AS “Set_digital_ch”
OFF ERROR
!
Channel=Digit ! [1-8] Channel ON
Set_digital_ch((Card_address),(Channel))
!
RETURN
!
!
!---------------------------------------------------------
Set_all_digital: ! SetAllDigital
!---------------------------------------------------------
DISP “—> SET ALL DIGITAL …”
!
ON ERROR GOSUB Er_dll
DLL GET “STDCALL VOID VM167:SetAllDigital” AS “Set_all_digital”
OFF ERROR
!
Set_all_digital((Card_address))! All Output Channels ON
!
RETURN
!
!
!---------------------------------------------------------
Rd_digital_ch: ! ReadDigitalChannel
!---------------------------------------------------------
DISP “—> READ DIGITAL CHANNEL …”
!
ON ERROR GOSUB Er_dll
DLL GET “STDCALL LONG VM167:ReadDigitalChannel” AS “Rd_digital_ch”
OFF ERROR
!
Retour=-1000
Retour=FNRd_digital_ch((Card_address),(Channel)) ! [1-8]
!
SELECT Retour
CASE 0
PRINT “CHANNEL#”;Channel;": OFF"
CASE 1
PRINT “CHANNEL#”;Channel;": ON"
END SELECT
PRINT
!
RETURN
!
!
!---------------------------------------------------------
Rd_all_digital: ! ReadAllDigital
!---------------------------------------------------------
DISP “—> READ ALL DIGITAL …”
!
ON ERROR GOSUB Er_dll
DLL GET “STDCALL LONG VM167:ReadAllDigital” AS “Rd_all_digital”
OFF ERROR
!
Retour=-1000
Retour=FNRd_all_digital((Card_address))
!
Valeur$=VAL$(Retour)
Type$=“DEC”
CALL Convert(Type$,Valeur$,Dec,Hex$,Bin$)
!
Digital(1)=VAL(Bin$[32,32]) ! Digital Channel 1
Digital(2)=VAL(Bin$[31,31]) ! — 2
Digital(3)=VAL(Bin$[30,30]) ! — 3
Digital(4)=VAL(Bin$[29,29]) ! — 4
Digital(5)=VAL(Bin$[28,28]) ! — 5
Digital(6)=VAL(Bin$[27,27]) ! — 6
Digital(7)=VAL(Bin$[26,26]) ! — 7
Digital(8)=VAL(Bin$[25,25]) ! — 8
!
FOR I=1 TO 8
Bit=Digital(I)
IF Bit=1 THEN PRINT “DIGITAL#”;I;": ON"
IF Bit=0 THEN PRINT “DIGITAL#”;I;": OFF"
NEXT I
PRINT
!
RETURN
!
!
!---------------------------------------------------------
Set_pwm: ! SetPWM
!---------------------------------------------------------
DISP “—> SET PWM …”
!
ON ERROR GOSUB Er_dll
DLL GET “STDCALL VOID VM167:SetPWM” AS “Set_pwm”
OFF ERROR
!
Data1=INT((255/100)Dty) ! Dty [0-255] 0 --> 0%
IF Data1>255 THEN Data1=255 ! 255 --> 100% (maximum)
!
Frequency1=2929.68 ! Hz
Frequency2=11718.75 ! Hz
Frequency3=46875 ! Hz
Frequency=Frequency3 ! Frequency Choice
!
SELECT Frequency
CASE Frequency1
Data2=1
CASE Frequency2
Data2=2
CASE Frequency3
Data2=3
END SELECT
!
Set_pwm((Card_address),(Channel),(Data1),(Data2))
!
RETURN
!
!
!---------------------------------------------------------
Out_all_pwm: ! OutputAllPWM
!---------------------------------------------------------
DISP “—> OUTPUT ALL PWM …”
!
ON ERROR GOSUB Er_dll
DLL GET “STDCALL VOID VM167:OutputAllPWM” AS “Output_all_pwm”
OFF ERROR
!
Dty=ABS(Dty1) ! [0-255] Duty Cycle = 0 --> 0%
Data1=INT((255/100)Dty) ! 255 --> 100% (maximum)
IF Data1>255 THEN Data1=255
!
Dty=ABS(Dty2) ! [0-255] Duty Cycle = 0 --> 0%
Data2=INT((255/100)Dty )! 255 --> 100% (maximum)
IF Data2>255 THEN Data2=255
!
Output_all_pwm((Card_address),(Data1),(Data2))
!
RETURN
!
!
!---------------------------------------------------------
Reset_counter: ! ResetCounter
!---------------------------------------------------------
DISP “—> RESET COUNTER …”
!
ON ERROR GOSUB Er_dll
DLL GET “STDCALL VOID VM167:ResetCounter” AS “Reset_counter”
OFF ERROR
!
Reset_counter((Card_address))
!
RETURN
!
!
!---------------------------------------------------------
Rd_counter: ! ReadCounter
!---------------------------------------------------------
DISP “—> READ COUNTER …”
!
ON ERROR GOSUB Er_dll
DLL GET “STDCALL LONG VM167:ReadCounter” AS “Read_counter”
OFF ERROR
!
Counter=-1000
Counter=FNRead_counter((Card_address))
!
PRINT “Counter=”;Counter
!
RETURN
!
!
!---------------------------------------------------------
Connected: ! Connected
!---------------------------------------------------------
DISP “—> CONNECTED …”
!
ON ERROR GOSUB Er_dll
DLL GET “STDCALL LONG VM167:Connected” AS “Connected”
OFF ERROR ! Function DON’T WORK : Always Retour=0
!
Retour=-1000
Retour=FNConnected
!
SELECT Retour
CASE 0
PRINT “No VM167 VELLEMAN Board Found”
CASE 1
PRINT “VM167 VELLEMAN Board Address#0 Found”
CASE 2
PRINT “VM167 VELLEMAN Board Address#1 Found”
CASE 3
PRINT “VM167 VELLEMAN Boards Address#0 and Address#1 Found”
END SELECT
!
RETURN
!
!
!---------------------------------------------------------
Out_all_digital: ! OutputAllDigital
!---------------------------------------------------------
DISP “—> OUTPUT ALL DIGITAL …”
!
ON ERROR GOSUB Er_dll
DLL GET “STDCALL VOID VM167:OutputAllDigital” AS “Out_all_digital”
OFF ERROR
!
Valeur$=VAL$(Value)
Type$=“DEC”
CALL Convert(Type$,Valeur$,Dec,Hex$,Bin$)
PRINT “DIGITAL OUTPUT : “;Bin$[25,32];” Dec=”;Valeur$
PRINT
!
Data1=Dec
Out_all_digital((Card_address),(Data1))
!
RETURN
!
!
!---------------------------------------------------------
Inout_mode: ! InOutMode
!---------------------------------------------------------
DISP “—> IN OUT MODE …”
!
ON ERROR GOSUB Er_dll
DLL GET “STDCALL VOID VM167:InOutMode” AS “In_out_mode”
OFF ERROR
!
In_out_mode((Card_address),(High),(Low))
!
! Low=0 : The digital I/O terminals 1-4 are Outputs
! Low=1 : The digital I/O terminals 1-4 are Inputs
! High=0 : The digital I/O terminals 5-8 are Outputs
! High=1 : The digital I/O terminals 5-8 are Inputs
!
PRINT “DIGITAL I/O terminals : [1-4][5-8]”
PRINT " Low High "
PRINT " “;Low;” ";High
PRINT
!
RETURN
!
!
!---------------------------------------------------------
Rd_back_pwm: ! ReadBackPWMOut
!---------------------------------------------------------
DISP “—> READ BACK PWM OUT …”
!
ON ERROR GOSUB Er_dll
DLL GET “STDCALL VOID VM167:ReadBackPWMOut” AS “Rdb_pwm_out”
OFF ERROR
!
! Buffer : Pointer to an Array of Two 32-bit integers where the data will be read
!
DEALLOCATE Pwm(
)
ALLOCATE LONG Pwm(0:1)
Pwm(0)=1000
Pwm(1)=1000
!
Rdb_pwm_out((Card_address),Pwm(0))
!
FOR I=0 TO 1
Dty=INT((100/255)Pwm(I))
PRINT “DTY=”;Dty;"% ("&VAL$(Pwm(I))&")"
NEXT I
PRINT
!
RETURN
!
!
!---------------------------------------------------------
Rd_back_iom: ! ReadBackInOutMode
!---------------------------------------------------------
DISP “—> READ BACK IN OUT MODE …”
!
ON ERROR GOSUB Er_dll
DLL GET “STDCALL LONG VM167:ReadBackInOutMode” AS “Rdb_in_out”
OFF ERROR ! Function DON’T WORK : Always Retour=0
!
Retour=-1000
Retour=Rdb_in_out((Card_address))
!
SELECT Retour
CASE 0
PRINT “DIGITAL I/O terminals : [1-8] OUTPUT”
CASE 1
PRINT “DIGITAL I/O terminals : [1-4] INPUT / [5-8] OUTPUT”
CASE 2
PRINT “DIGITAL I/O terminals : [1-4] OUTPUT / [5-8] INPUT”
CASE 3
PRINT “DIGITAL I/O terminals : [1-8] INPUT”
END SELECT
PRINT
!
RETURN
!
!
!---------------------------------------------------------
Close: ! CLOSE Device
!---------------------------------------------------------
DISP “—> CLOSE DEVICE …”
!
ON ERROR GOSUB Er_dll
DLL GET “STDCALL VOID VM167:CloseDevices” AS “Close_device”
OFF ERROR
!
Close_device
!
RETURN
!
!
!
*************************************************************
Decod: ! Decodage Data --> 4 Digits
!*****************************************************************
Type$=“DEC”
Valeur$=VAL$(Data)
CALL Convert(Type$,Valeur$,Dec,Hex$,Bin$)
!
PRINT “Decimal =”;Dec
PRINT "Hexadecimal = ";Hex$
PRINT "Binary = ";Bin$
!
Digit1$=Bin$[1,8]
Digit2$=Bin$[9,16]
Digit3$=Bin$[17,24]
Digit4$=Bin$[25,32]
!
Type$=“BIN”
Valeur$=Digit1$
CALL Convert(Type$,Valeur$,Dec,Hex$,Bin$)
Dec1=Dec
!
Type$=“BIN”
Valeur$=Digit2$
CALL Convert(Type$,Valeur$,Dec,Hex$,Bin$)
Dec2=Dec
!
Type$=“BIN”
Valeur$=Digit3$
CALL Convert(Type$,Valeur$,Dec,Hex$,Bin$)
Dec3=Dec
!
Type$=“BIN”
Valeur$=Digit4$
CALL Convert(Type$,Valeur$,Dec,Hex$,Bin$)
Dec4=Dec
!
RETURN
!
!
!*****************************************************************
Er_dll: ! Error Checking : DLL loading Verification
!*****************************************************************
SELECT ERRN
CASE 1104 ! Function already loaded
ERROR RETURN
CASE ELSE
PRINT ERRM$,ERRN
GOTO Close
END SELECT
!
!
!*****************************************************************
End: ! END of PROGRAM
!*****************************************************************
KEY LABELS ON
DISP
PRINT
PRINT “END OF PROGRAM”
END
!
!
SUB Convert(Type$,Valeur$,Dec,Hex$,Bin$)!*********************************
Convert: ! Convertion DEC/HEX/BIN
!*********************************
ALLOCATE B$[32]
IF Type$=“DEC” THEN GOTO D
IF Type$=“HEX” THEN GOTO H
IF Type$=“BIN” THEN GOTO B
!
D: Dec=VAL(Valeur$) ! DECIMAL
Hex$=DVAL$(Dec,16) ! DEC2HEX [8]
Bin$=DVAL$(Dec,2) ! DEC2BIN [32]
GOTO Exit
!
H: Hex$=Valeur$ ! HEXADECIMAL
Bin$=DVAL$(DVAL(Hex$,16),2) ! HEX2BIN [32]
Dec=DVAL(Hex$,16) ! HEX2DEC [8]
GOTO Exit
!
B: Bin$=Valeur$ ! BINAIRE
B$=Bin$
IF LEN(Bin$)<16 THEN B$=RPT$(“0”,16-LEN(Bin$))&Bin$
IF VAL(B$)>10000001 THEN B$=RPT$(“1”,15)&B$
Dec=DVAL(B$,2) ! BIN2DEC
Hex$=DVAL$(DVAL(Bin$,2),1)) ! BIN2HEX [8]
Hex$=Hex$[5,8]
GOTO Exit
!
Exit: !
SUBEND

Please try calling the function “OpenDevices” first and then the function “Connected”.

Hi, thanks for response.
It’s ok now with these configuration commands order
Open / Connected / Dll / Firmware

The last issue is ReadBackInOutMode function
Always return the same result=0 ([1-8] OUTPUT) after programming I/O in the InOutMode function with the data Low and High to 0,0 or 0,1 or 1,0 or 1,1

Just the “FN” seems to be missing…
Retour=FNRdb_in_out((Card_address))

Hi, oops! It was my fault!!! :woozy_face:
Excuse me for the syntax error. Thank you for the correction. All functions are working perfectly now :+1:

No problem. I’m glad the issue is fixed now.