Velbus in PureBasic

I would to write a parser or a library for Purebasic. I can use these functions :
CallCFunction
CallCFunctionFast
CallFunction
CallFunctionFast
CloseLibrary
CountLibraryFunctions
ExamineLibraryFunctions
GetFunction
GetFunctionEntry
IsLibrary
LibraryFunctionAddress
LibraryFunctionName
LibraryID
NextLibraryFunction
OpenLibrary

So an example is given by Purebasic team :

[code]CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
If OpenLibrary(0, “USER32.DLL”)
*MessageBox = GetFunction(0, “MessageBoxA”)
If *MessageBox
CallFunctionFast(*MessageBox, 0, “Body”, “Title”, 0)
EndIf
CloseLibrary(0)
EndIf

CompilerCase #PB_OS_Linux
If OpenLibrary(0, “libc.so”)
*MAlloc = GetFunction(0, “malloc”)
If *MAlloc
*Buffer = CallCFunctionFast(*MAlloc, 128)
If *Buffer
Debug “Buffer allocated”
CallCFunction(0, “free”, *Buffer)
EndIf
EndIf
CloseLibrary(0)
EndIf

CompilerEndSelect
[/code]

Do you think I could easely use Velleman.Velbus.dll with these functions ?

If someone could help me to develop this module, I will give it as an open-source or something for all Purebasic users (free of course).

Edit : I’ve try this (part) of code

[code]Enumeration
#Bibliotheque
EndEnumeration
NomFichier$ = “D:\telechargement\Domotique\Velbus\VelbusLib\Velbus\bin\Release\Velleman.Velbus.dll”

IncludeFile “Common.pb”
Resultat = OpenLibrary(#Bibliotheque, NomFichier$)
Debug Resultat
Resultat = OpenLibrary(#Bibliotheque, NomFichier$)
Debug Resultat --> OK
Debug IsLibrary(#Bibliotheque) --> OK
Debug CountLibraryFunctions(#Bibliotheque) --> 0 (!)
Debug ExamineLibraryFunctions(#Bibliotheque) --> -1 (!)
Debug GetFunction(#Bibliotheque, “Velleman.Velbus.bus.SendBlocking”) --> 0 (!)
Debug GetFunction(#Bibliotheque, “bus.SendBlocking”) --> 0 (!)
[/code]

and I have 0 or -1 (non existant or null) for last call functions :cry:

Velleman.Velbus.dll is a .NET assembly file, not a Dynamic Link Library (like user32.dll, …). This mistake is made very often.

The easiest thing to do would maybe be to write you own library in Purebasic that communicates via the serial port?

[quote=“VEL448”]

The easiest thing to do would maybe be to write you own library in Purebasic that communicates via the serial port?[/quote]

Thanks for explanations… but what to do if I’ve USB module (like on demobox) ?

The last possibility is the server access via VelbusLink (localhost, port 3788) but by default (after a windows start/restart) Velbuslink isn’t connected and not in server mode… :confused:

I really want to program my installation (complex informations like holidays, night hours, seasons, etc should be treated by a PC) but .NET is a big limitation for me (maybe I’ve to go back to school for programmation lesson ? :unamused: )

Well I have good news, it’s exactly the same for the VMB1USB. The driver supplied with the module creates a virtual serial port, which is identical to a normal serial port in terms of I/O. So you can just go ahead and open that port in any programming language.

In device manager you can see which extra port has been created when you plug in the VMB1USB (‘COM5’ for example).

[quote=“VEL448”]Well I have good news, it’s exactly the same for the VMB1USB. The driver supplied with the module creates a virtual serial port, which is identical to a normal serial port in terms of I/O. So you can just go ahead and open that port in any programming language.

In device manager you can see which extra port has been created when you plug in the VMB1USB (‘COM5’ for example).[/quote]

You’re right !!! :smiley:

And after some (a lot of) tries :

0F F8 01 04 00 02 00 00 F2 04 0F FB 01 02 F6 40 BD 04 0F FB 01 02 F6 02 FB 04 0F FB 02 08 FB 02 00 02 80 00 00 00 6D 04 00 0F F8 02 04 00 02 00 00 F1 04 0F 0F F8 01 04 00 00 02 00 F2 04 0F 0F F8 01 04 00 01 00 00 F3 04 0F FB 01 02 F5 01 FD 04 0F FB 01 02 F5 01 FD 04 0F FB 02 08 FB 01 00 00 00 00 00 00 F0 04 0F FB 01 02 F6 40 BD 04 0F F8 02 04 00 00 01 00 F2 04 00 0F F8 01 04 00 00 00 01 F3 04 0F 0F F8 01 04 00 00 01 00 F3 04 0F
My current code is very bad (cause debug and lot of check) but I think it become possible to create a Velbus parser 8)

THANKS !!!

That’s looking very good

The trick is to find a start of packet (0F), and then wait until you have 4 bytes, since byte nr 4 indicates the number of data bytes included in the packet.

0F F8 01 04 00 00 01 00 F3 04

Then you can determine that the packet will be (in this example)

  4 (header)
+ 4 (databytes)
+ 2 (checksum + end of packet)
= 10 bytes

Once you have 10 bytes, you perform some sanity checks to see if the packet is actually a packet.