Page 1 of 2

Using the PCAN-Basic API with VBA (Excel for example)

Posted: Wed 20. Jun 2012, 15:27
by PEAK-Support
It is very easy to use the PCAN-Basic API from VBA (Visual Basic for Applications). For example from Excel.
Step 1.
copy the pcan_basic.bas file to your folder where you like to store the Excel Sheet
Step 2.
Open Excel and create a new Macro
Step 3.
Add the pcan_basic.bas to your module folder
Step 4.
create a own module and copy this simple code into the file:

Code: Select all

Dim myMsg As TPCANMsg

Sub SendCANID()
 myMsg.ID = 255
 myMsg.LEN = 4
 myMsg.MsgType = PCAN_MESSAGE_STANDARD
 myMsg.DATA(0) = 1
 myMsg.DATA(1) = 2
 myMsg.DATA(2) = 3
 myMsg.DATA(3) = 4
 
 ret = CAN_Initialize(PCAN_USBBUS1, PCAN_BAUD_500K)
 If ret = PCAN_ERROR_OK Then
    For a = 1 To 100
        ret = CAN_Write(PCAN_USBBUS1, myMsg)
    Next a
 End If
 CAN_Uninitialize (PCAN_USBBUS1)
 End Sub
Step 5.
Press F5 to run the macro
If you have a PCAN-USB Adapter installed, the Interface will send out 100 CAN Frames.
Very easy - very nice :D
To use a differnet CAN Channel, a other Hardware Type or a different Baud Rate, simple exchange the PCAN_USBBUS1 and/or the PCAN_BAUD_500K for you need in the CAN_Initialize(...) function.

Re: Using the PCAN-Basic API with VBA (Excel for example)

Posted: Tue 18. Feb 2014, 22:52
by Mr.Ostrich
Hello
can you explan that a littlebit detailed?
What did you mean with
Step 4.

Re: Using the PCAN-Basic API with VBA (Excel for example)

Posted: Wed 19. Feb 2014, 08:11
by PEAK-Support
When ever you want to use our CAN API, you need to have basic understanding of software development. If you using VBA you need to create a Module where you have to place your code...very simple - Google is your friend...

Re: Using the PCAN-Basic API with VBA (Excel for example)

Posted: Wed 19. Feb 2014, 21:23
by Mr.Ostrich
Hello

Sorry but i know about dev in VBA. But it was not clear what you mean with "Add the pcan_basic.bas to your module folder"
But thank to your screenshot i saw it.

thank you

Re: Using the PCAN-Basic API with VBA (Excel for example)

Posted: Wed 19. Feb 2014, 21:42
by PEAK-Support
fine..so now you could start to work with the API in VBA

Re: Using the PCAN-Basic API with VBA (Excel for example)

Posted: Tue 4. Nov 2014, 21:29
by harryx
Hello,

when i copy this vba code in a new makro, i get allways the erorr Message: "Fehler beim Kompilieren: Außerhalb einer Prozedur ungültig" can you help me, what is the problem?

Re: Using the PCAN-Basic API with VBA (Excel for example)

Posted: Thu 6. Nov 2014, 13:35
by M.Maidhof
Hi,

did you copy the PCANBasic dlls (32 and 64bit) in the Windows directories, as written in the readme.txt of the PCANBasic package?

regards

Michael

Re: Using the PCAN-Basic API with VBA (Excel for example)

Posted: Thu 6. Nov 2014, 19:22
by PEAK-Support
Grüß Gott,
ich denke wir können auch Deutsch schreiben , da der Text "Fehler beim Kompilieren: Außerhalb einer Prozedur ungültig" auf eine Deutsche Office Version hindeutet.
Schauen Sie mal ob außerhalb des Sub und End Sub noch Text steht. Das Modul konnte nicht durch den Parser laufen (was Microsoft dann "compilieren" nennt).
Irgendetwas steht vor oder nach dem Funktionsanfang/Ende was da nicht hingehört...
und, wie von meinem Kollegen bereits mitgeteilt, sicherstellen das die DLL in einem Verzeichnis liegt der im Pfad eingetragen ist (System32/SysWoW64).

Re: Using the PCAN-Basic API with VBA (Excel for example)

Posted: Sun 9. Nov 2014, 20:38
by harryx
Hallo,

danke für die Hilfe, es lag an beiden dll Dateien, es läuft wirklich super, Top. :D
Ist es denn auch möglich das man eine gesendete Botschaft direkt in excel einlesen bzw. darstellen kann?
Ich habe den Code umgeschriebne und bekommen nun die Fehlermeldung "Fehler beim Kompilieren: Argument ist nicht optional."
Habe statt can_write folgendes eingesetzt.-> ret = CAN_Read(PCAN_USBBUS1, myMsg)
Und can_read ist gelb hinterlegt. muss hier evtl. eine andere API verwendet werden oder gibt es hier auch andere dlls?
Vielleicht habt ihr hier auch Erfahrung und könnt mir nochmal helfen?

Danke & Gruss

Harry

Re: Using the PCAN-Basic API with VBA (Excel for example)

Posted: Sun 9. Nov 2014, 21:00
by harryx
Hallo,

habe noch was vergessen, ich möcchte natürlich nur auf eine bestimmte Botschaft warten, bei der die ID, Länge usw. genau definiert werden kann. Villeicht könnte ihr mir dabei nochmal helfen.

Dnake & Gruss

Harry