Page 2 of 2

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

Posted: Mon 10. Nov 2014, 11:03
by PEAK-Support
Hier ein kleiner VBA code, der nach dem starten solange ließt bis die ID 0x100 als 11Bit Frame gelesen wurde.
Dann schreibt er die Datenbytes in die Excel Zelle.

Code: Select all

#If VBA7 And Win64 Then
    ' 64 bit Excel
    Public Declare PtrSafe Sub Sleep Lib "kernel32" ( _
        ByVal dwMilliseconds As LongLong)
#Else
    ' 32 bit Excel
    Public Declare Sub Sleep Lib "kernel32" ( _
        ByVal dwMilliseconds As Long)
#End If

' the CAN Types
Dim myMsgRecv As TPCANMsg
Dim myMsgSend As TPCANMsg
Dim myTimeStamp As TPCANTimestamp
 
 Sub WaitForCANID()
 Sheets("Tabelle1").Activate ' select first Sheet
 Range("A1").Select ' set cursor to A1
 ActiveCell().Value = "CAN-Data"
 ' now Init CAN-USB Channel one with 500K
 ret = CAN_Initialize(PCAN_USBBUS1, PCAN_BAUD_500K)
 If ret = PCAN_ERROR_OK Then ' CAN Init OK?
  MsgBox "CAN Bus OK!", vbInformation ' show MsgBox if OK
  ret = CAN_Read(PCAN_USBBUS1, myMsgRecv, myTimeStamp) ' read from driver
  While (ret = PCAN_ERROR_OK) Or (ret = PCAN_ERROR_QRCVEMPTY) ' read as long a Msg available or queu empty
    ' if a CAN Message was reveived - we send out the CAN-ID+1 DLC1 with 0xFF as dataByte 1 for Test
    If ret = PCAN_ERROR_OK Then ' Msg received
        myMsgSend.ID = myMsgRecv.ID + 1
        myMsgSend.LEN = 1
        myMsgSend.DATA(0) = &HFF
        ret = CAN_Write(PCAN_USBBUS1, myMsgSend)
        If myMsgRecv.MsgType = PCAN_MESSAGE_STANDARD Then ' Msg is 11Bit type
            If myMsgRecv.ID = &H100 Then ' ID of the Message must be 0x100 (HEX)
                ' we put the values in the first sheet in Cell A2 to A11
                ActiveCell(2, 1).Value = myMsgRecv.ID
                ActiveCell(3, 1).Value = myMsgRecv.LEN
                For a = 0 To myMsgRecv.LEN - 1
                    ActiveCell(4 + a).Value = myMsgRecv.DATA(a)
                Next
                CAN_Uninitialize (PCAN_USBBUS1) ' deinit the CAN
                Exit Sub ' end leave the sub
            End If
        End If
    End If
    Sleep (50) ' 50ms sleep and read again:
    ret = CAN_Read(PCAN_USBBUS1, myMsgRecv, myTimeStamp)
  Wend
  
 Else
    MsgBox "Error while init CAN Bus", vbCritical
 End If
    CAN_Uninitialize (PCAN_USBBUS1)
 End Sub
ABER! VBA ist nicht wirklich dafür geeignet CAN Daten zyklisch zu lesen und auszugeben - es fehlen Timer Callbacks bzw. Event orientierte Funktionen. Das bedeutet solange das Macro läuft und nicht die ID 0x100 als 11Bit ID kommt, steht Excel ! Keine Bedienung und keine Bildschirmausgabe - das will man nicht wirklich. Verwenden Sie statdesen lieber eine echte Programmiersprache mit unserer freien PCANBasic API (dotNet Compiler gibt´s bei Microsoft kostenlos und damit kann man Excel super anprogrammieren, und die ganzen CAN Empfangsroutinen in VB.Net oder C# schreiben.

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

Posted: Wed 19. Nov 2014, 08:14
by harryx
Hallo,

sorry für die verspätete Antwort. Funktioniert perfekt, danke.

Gruss

Harryx

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

Posted: Tue 4. Feb 2020, 02:22
by Trummybum
Hello,

There seems to be an issue at the DWORD line when I run it. I receive "Expected: end of statement" error message.

Any help would be appreciated.

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

Posted: Tue 4. Feb 2020, 09:17
by M.Heidemann
Hello,
There seems to be an issue at the DWORD line when I run it. I receive "Expected: end of statement" error message.

If this occuring in the header-file (PCANBasic.bas) for VisualBasic please change to following line in the struct "TPCANChannelInformation":

Code: Select all

DWORD device_features;                          ' Device capabilities flag (see FEATURE_*)
to

Code: Select all

device_features As Long                      ' Device capabilities flag (see FEATURE_*)
Note from moderator: Already corrected code; used to contain a semicolon after "Long".


Best Regards

Marvin

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

Posted: Tue 4. Feb 2020, 14:55
by Trummybum
Hi, sir,

Unfortunately that returned the same error.

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

Posted: Tue 4. Feb 2020, 15:16
by M.Heidemann
Hello,

Yes, you`re right. Pardon me.

Check the code again, i corrected it.

Code: Select all

device_features As Long                         ' Device capabilities flag (see FEATURE_*)
The semicolon was the culprit, i am sorry.

Please try again.


Best Regards

Marvin

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

Posted: Tue 4. Feb 2020, 19:55
by Trummybum
Moderation remark (by M.Gerber): Please don't use full quotes of preceding posts, as this information is already there (right above). This leads to better reading experience when browsing through the thread.

That worked great! Thank you.
Do you offer a sample code to read CAN messages and insert them into an Excel spreadsheet?

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

Posted: Wed 5. Feb 2020, 09:31
by M.Heidemann
Please see the code in the post from U.Wihlem dated 10th of November 2014.


You can use this as a starting point.

You can rid of the conditions if you just want to log the data as is.

I`ll translate the comment made by U.Wilhelm regarding it:

This code will read until ID 0X100 was received as an 11-Bit frame, afterwards it will parse the data into the Excel-sheet.

*The Code*

BUT! VBA is not really the best solution to cyclically read and write CAN-Data - Timer Callbacks or rather event orientated functions are not present. This means that as long as this macro is running and the 11 bit ID of 0x100 is not coming in Excel will be essentially frozen. No Response, No Output to Screen - Not desirable. Preferably use a real programming langauge with our free PCANBasic API ( There are .Net compilers available - free of charge, that are highly compatible with Excel. You could use C# or VB.Net to implement something like this)



Best Regards


Marvin