Here are new procedures : **SetLed **(could make a PushButton led to blink, etc.) and **SwitchRelay **(put the relay ON or OFF)
Fill free to play with these procedures
Parser to analyse received Velbus frame is on going
[code]; ******************************************************************************
; Velbus communication *
; Author : D. Roumanet Date : 14/09/2009 *
; e-mail : golfy @ free . fr Ver. : 1.0 (Purbasic : 4.x) *
; -----------------------------------------------------------------------------*
Enumeration
#CanalCOM
#Window_0
#Button_0
#String_0
#Button_1
#Editor_0
#Combo_0
EndEnumeration
Global *Buffer = AllocateMemory(2048)
Global *BufOut = AllocateMemory(1024)
Procedure Open_Window_0()
If OpenWindow(#Window_0, 216, 0, 500, 300, “Velbus via VelbusLink”, #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar )
ButtonGadget(#Button_0, 20, 20, 70, 20, “Bouton”,#PB_Button_Toggle)
StringGadget(#String_0, 160, 20, 50, 20, “”)
ButtonGadget(#Button_1, 230, 20, 70, 20, “Relais”,#PB_Button_Toggle)
EditorGadget(#Editor_0, 20, 60, 470, 140)
ComboBoxGadget(#Combo_0, 20, 240, 280, 20)
EndIf
EndProcedure
Procedure.s Hexa2(*B,longueur)
; This procedure transform ASCII string in Hexadecimal string (for human). SPACE car. is separator.
r$ = “”
For t = 0 To longueur-1
; Read the buffer from 0 to lengh
a$ = RSet(Hex(PeekB(*B+t) & $FF),2,“0”)+" " ; Rset use to have ‘$0F’ in place of ‘$F’
r$ = r$ + a$
Next t
r$ = r$ + Chr(13)+Chr(10) ; Add CR+LF characters
ProcedureReturn r$
EndProcedure
Procedure LecturePort()
; ******* NOT FINAL PROCEDURE : JUST HELP TO READ BUS AND SEE MESSAGES ********
Resultat = AvailableSerialPortInput(#CanalCOM)
If Resultat
longueur = ReadSerialPortData(#CanalCOM, *Buffer, Resultat)
Commande$ = Hexa2(*Buffer, longueur)
;CommandParser(Commande$)
Debug "Rcv => "+Commande$
Debug “________________”
SetGadgetText(#Editor_0, GetGadgetText(#Editor_0)+Commande$)
EndIf
EndProcedure
Procedure.i CheckSum(*B,longueur.i)
somme=0 ; Initialize Counter
For t=0 To longueur ; Loop from 0 to checksum byte -1
Somme=Somme+PeekB(*B+t) ; Adding each byte from the packet
Next t
Somme = (Somme & $FF) ! $FF ; As PureBasic as signed integer, need to remove higher value
Somme = Somme + 1 ; (AND operation) and inverse with XOR (!), them add ‘1’
ProcedureReturn Somme ; Return the checksum value
EndProcedure
Procedure.i SwitchRelay(*B, Amodule.i, Arelay.i, StatusRelay.i)
PokeB(*B+0,$0F) ; Start
PokeB(*B+1,$F8) ; Priority
PokeB(*B+2,Amodule) ; Address module(peek an existing address !)
PokeB(*B+3,$02) ; Data = 2 bytes (no RTR)
Select StatusRelay
Case 0 ; Relay OFF
PokeB(*B+4,$02) ; OFF
Default ; Relay ON
PokeB(*B+4,$01) ; ON
EndSelect
PokeB(*B+5,Arelay) ; Relay Number (1,2,4,8 else it’s binary : 3 = 1+2 ?)
PokeB(*B+6,CheckSum(*B,5) ) ; Checksum from h0F to Checksum (not included of course)
PokeB(*B+7,$04) ; ETX (End of Transmission)
Commande$ = Hexa2(*B, 8)
Debug "Send => "+Commande$
ProcedureReturn WriteSerialPortData(#CanalCOM, *B, 8)
EndProcedure
Procedure.i SetLed(*B, Amodule.i, Aled.i, StatusLed.i)
; OFF=F5, ON=F6, BlinkSlow=F7, BlinkFast=F8, BlinkVeryFast=F9
;
PokeB(*B+0,$0F) ; Start
PokeB(*B+1,$FB) ; Priority
PokeB(*B+2,Amodule) ; Address module(peek an existing address !)
PokeB(*B+3,$02) ; Data = 2 bytes (no RTR)
Select StatusLed
Case 0 ; Clear
PokeB(*B+4,$F5) ;
Case 1 ; Always ON
PokeB(*B+4,$F6) ;
Case 2 ; Blink Slowly
PokeB(*B+4,$F7) ;
Case 3 ; Blink Fast
PokeB(*B+4,$F8) ;
Default ; Blink Very Fast
PokeB(*B+4,$F9) ;
EndSelect
PokeB(*B+5,Aled) ; LED number
PokeB(*B+6,CheckSum(*B,5) ) ; Checksum from h0F to Checksum (not included of course)
PokeB(*B+7,$04) ; ETX (End of Transmission)
Commande$ = Hexa2(*B, 8)
Debug "Send => "+Commande$
ProcedureReturn WriteSerialPortData(#CanalCOM, *B, 8)
EndProcedure
If OpenSerialPort(#CanalCOM, “COM9”, 9600, #PB_SerialPort_NoParity, 8, 1, #PB_SerialPort_NoHandshake, 1024, 1024)
Open_Window_0()
If IsWindow(#WIndow_0) = 0
CloseSerialPort(#CanalCOM)
End
EndIf
Repeat
EventID = WaitWindowEvent(20)
LecturePort()
If EventID = #PB_Event_Gadget
Select EventGadget()
Case #Button_0
Debug "Hello_____________________"
SendButton(*BufOut, 1,2,1)
SetLed(*BufOut, 1, 3, 2)
SetLed(*BufOut, 2, 4, 3)
Case #Button_1
If GetGadgetState(#Button_1) = 1
Debug "Relay OFF______________________"
SwitchRelay(*BufOut, 2,2,0)
;SetGadgetState(#Button_1, 0)
Else
Debug "Relay ON_______________________"
SwitchRelay(*BufOut, 2,2,1)
;SetGadgetState(#Button_1, 1)
EndIf
EndSelect
EndIf
Until EventID = #PB_Event_CloseWindow
CloseSerialPort(#CanalCOM)
Else
MessageRequester(“Erreur”,“Le port série n’a pas été initialisé”,0)
EndIf
[/code]