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

The free CAN Software API (Application Programming Interface) for Windows®
User avatar
PEAK-Support
Sales & Support
Sales & Support
Posts: 1646
Joined: Fri 10. Sep 2010, 19:34

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

Post by PEAK-Support » Wed 20. Jun 2012, 15:27

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.
--------------------------------
PEAK-System Technik
Technical Support Team
support[at]peak-system.com
-------------------------------

Mr.Ostrich
Posts: 3
Joined: Mon 17. Feb 2014, 23:47

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

Post by Mr.Ostrich » Tue 18. Feb 2014, 22:52

Hello
can you explan that a littlebit detailed?
What did you mean with
Step 4.

User avatar
PEAK-Support
Sales & Support
Sales & Support
Posts: 1646
Joined: Fri 10. Sep 2010, 19:34

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

Post by PEAK-Support » Wed 19. Feb 2014, 08:11

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...
Attachments
Excel VBA
Excel VBA
VBA-Module.JPG (92.54 KiB) Viewed 20206 times
--------------------------------
PEAK-System Technik
Technical Support Team
support[at]peak-system.com
-------------------------------

Mr.Ostrich
Posts: 3
Joined: Mon 17. Feb 2014, 23:47

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

Post by Mr.Ostrich » Wed 19. Feb 2014, 21:23

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

User avatar
PEAK-Support
Sales & Support
Sales & Support
Posts: 1646
Joined: Fri 10. Sep 2010, 19:34

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

Post by PEAK-Support » Wed 19. Feb 2014, 21:42

fine..so now you could start to work with the API in VBA
--------------------------------
PEAK-System Technik
Technical Support Team
support[at]peak-system.com
-------------------------------

harryx
Posts: 5
Joined: Tue 4. Nov 2014, 21:25

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

Post by harryx » Tue 4. Nov 2014, 21:29

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?

M.Maidhof
Support
Support
Posts: 1751
Joined: Wed 22. Sep 2010, 14:00

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

Post by M.Maidhof » Thu 6. Nov 2014, 13:35

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

User avatar
PEAK-Support
Sales & Support
Sales & Support
Posts: 1646
Joined: Fri 10. Sep 2010, 19:34

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

Post by PEAK-Support » Thu 6. Nov 2014, 19:22

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).
--------------------------------
PEAK-System Technik
Technical Support Team
support[at]peak-system.com
-------------------------------

harryx
Posts: 5
Joined: Tue 4. Nov 2014, 21:25

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

Post by harryx » Sun 9. Nov 2014, 20:38

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

harryx
Posts: 5
Joined: Tue 4. Nov 2014, 21:25

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

Post by harryx » Sun 9. Nov 2014, 21:00

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

Post Reply