PCAN Write cannot convert TPCANMsg

The free CAN Software API (Application Programming Interface) for Windows®
Post Reply
SimonH
Posts: 1
Joined: Thu 27. Jul 2023, 14:10

PCAN Write cannot convert TPCANMsg

Post by SimonH » Thu 27. Jul 2023, 14:32

peak driver 8.16.0
PCAN-Basic_Linux 4.7.0.3
Hardware: PCAN USB IPEH-002022

Hey,
im currently learning to work with CAN Bus and wanted to try the CAN Write function provided in the PCAN Basic doc. While compiling I got the error message:
can_send_test.cpp:30:38: error: cannot convert ‘TPCANMsg’ {aka ‘tagTPCANMsg’} to ‘TPCANMsg*’ {aka ‘tagTPCANMsg*’}
30 | result = CAN_Write(PCAN_USBBUS1, msg);
I added my c++ code below, any help would be appreciated :)

Code: Select all

#include<iostream>
#include<PCANBasic.h>

#include <asm/types.h>
#define DWORD  __u32
#define WORD   unsigned short
#define BYTE   unsigned char
#define LPSTR  char*


int main() {

    TPCANMsg msg;
    TPCANStatus result;
    char strMsg[256];

    // Konfigurieren einer CAN Nachricht
    //
    msg.ID = 895;
    msg.MSGTYPE = PCAN_MESSAGE_STANDARD;
    msg.LEN = 4;
    msg.DATA[0] = 0xFF;
    msg.DATA[1] = 0x00;
    msg.DATA[2] = 0x00;
    msg.DATA[3] = 0x00;

    // Die Nachricht wird über den PCAN-USB Kanal-1 gesendet.
    //
    result = CAN_Write(PCAN_USBBUS1, msg);
    
  return 0;
}
Last edited by K.Wagner on Thu 27. Jul 2023, 14:37, edited 1 time in total.
Reason: Code formatting for better reading

K.Wagner
Software Development
Software Development
Posts: 1080
Joined: Wed 22. Sep 2010, 13:36

Re: PCAN Write cannot convert TPCANMsg

Post by K.Wagner » Thu 27. Jul 2023, 14:43

Hello,

You have the answer already in the compiler error. Please note that the parameter "msg" has to be passed as a reference. Here the declaration of CAN_Write from PCANBasic.h:

Code: Select all

/// <summary>
/// Transmits a CAN message 
/// </summary>
/// <param name="Channel">"The handle of a PCAN Channel"</param>
/// <param name="MessageBuffer">"A TPCANMsg buffer with the message to be sent"</param>
/// <returns>"A TPCANStatus error code"</returns>
TPCANStatus __stdcall CAN_Write(
        TPCANHandle Channel, 
        TPCANMsg* MessageBuffer);
So, the compiler message "error: cannot convert ‘TPCANMsg’ {aka ‘tagTPCANMsg’} to ‘TPCANMsg*’ {aka ‘tagTPCANMsg*’}" is quite clear.
Best regards,
Keneth

K.Wagner
Software Development
Software Development
Posts: 1080
Joined: Wed 22. Sep 2010, 13:36

Re: PCAN Write cannot convert TPCANMsg

Post by K.Wagner » Thu 27. Jul 2023, 14:49

An additional note:

if the code you posted is the whole code being used, then it will not work since you need to initialize the channel first (see CAN_Initialize). Please check the help files and try to understand the sample projects first, before startig an application from scratch.
Best regards,
Keneth

Post Reply