Page 1 of 1

Not able to initialize can using CPP code

Posted: Mon 3. Feb 2025, 11:40
by Tejaswi
Hello,
I am currently working with the PCAN-USB FD device and trying to initialize CAN-FD communication using the PCAN Basic API. However, I am encountering the error "Error with initializing CAN interface: 0x1400".
I have also reviewed the configuration and ensured that both the nominal and data bitrates are set correctly in PCAN-View.

I have attached the code and screenshot,

Code: Select all

#include <iostream>
#include <windows.h>
#include "PCANBasic.h"  // Make sure this header is available
using namespace std;
// Function declarations
int LoadDLL();
int UnloadDLL();
bool GetFunctionAddress(HINSTANCE h_module);

// Define CAN FD bitrate manually
#define CAN_FD_BITRATE "f_clock=80000000, nom_brp=5, nom_tseg1=15, nom_tseg2=4, nom_sjw=4, data_brp=2, data_tseg1=7, data_tseg2=2, data_sjw=2"

// Typedefs for functions from the DLL
typedef TPCANStatus (__stdcall *PCAN_InitializeFD)(TPCANHandle Channel, const char* BitrateFD);
typedef TPCANStatus (__stdcall *PCAN_Uninitialize)(TPCANHandle Channel);
typedef TPCANStatus (__stdcall *PCAN_ReadFD)(TPCANHandle Channel, TPCANMsgFD* MessageBuffer, TPCANTimestampFD* TimestampBuffer);
typedef TPCANStatus (__stdcall *PCAN_WriteFD)(TPCANHandle Channel, TPCANMsgFD* MessageBuffer);

// Function pointers for DLL functions
PCAN_InitializeFD g_CAN_InitializeFD = nullptr;
PCAN_Uninitialize   g_CAN_Uninitialize   = nullptr;
PCAN_ReadFD       g_CAN_ReadFD       = nullptr;
PCAN_WriteFD      g_CAN_WriteFD      = nullptr;

// DLL handle and other global variables
HINSTANCE g_i_DLL = nullptr;
TPCANHandle g_hChannel;

// Function to load the DLL
int LoadDLL()
{
    if (g_i_DLL == nullptr)
    {
        g_i_DLL = LoadLibrary("PCANBasic.dll");
        if (g_i_DLL == nullptr)
        {
            std::cerr << "ERROR: Cannot load PCANBasic.dll" << std::endl;
            return -1;
        }
        else
        {
            if (GetFunctionAddress(g_i_DLL))
            {
                std::cout << "Loaded functions from PCANBasic.dll" << std::endl;
            }
            else
            {
                std::cerr << "ERROR: Cannot load function addresses" << std::endl;
                return -2;
            }
        }
    }
    return 0;
}

// Function to load the function addresses from the DLL
bool GetFunctionAddress(HINSTANCE h_module)
{
    g_CAN_InitializeFD = reinterpret_cast<PCAN_InitializeFD>(GetProcAddress(h_module, "CAN_InitializeFD"));
    g_CAN_Uninitialize   = reinterpret_cast<PCAN_Uninitialize>(GetProcAddress(h_module, "CAN_Uninitialize"));
    g_CAN_ReadFD       = reinterpret_cast<PCAN_ReadFD>(GetProcAddress(h_module, "CAN_ReadFD"));
    g_CAN_WriteFD      = reinterpret_cast<PCAN_WriteFD>(GetProcAddress(h_module, "CAN_WriteFD"));

    return g_CAN_InitializeFD && g_CAN_Uninitialize && g_CAN_ReadFD && g_CAN_WriteFD;
}

// Function to unload the DLL
int UnloadDLL()
{
    if (g_i_DLL)
    {
        FreeLibrary(g_i_DLL);
        g_CAN_InitializeFD = nullptr;
        g_CAN_Uninitialize   = nullptr;
        g_CAN_ReadFD       = nullptr;
        g_CAN_WriteFD      = nullptr;
        return 0;
    }
    return -1;
}

int main()
{
    int ret;
    TPCANStatus CANStatus;
    TPCANMsgFD SendMessageBuffer;
    TPCANTimestampFD TimestampBuffer;

    std::cout << "PCAN-Basic CAN FD" << std::endl;
    std::cout << "Using 500k nominal + 2M data baud rate" << std::endl;

    // Initialize channel (for example, USB Channel 1)
    g_hChannel = PCAN_USBBUS1;

    // Load the DLL and get the function pointers
    ret = LoadDLL();
    if (ret != 0)
    {
        std::cerr << "Error loading DLL: " << ret << std::endl;
        return -1;
    }


    // Initialize the CAN FD interface with a manually defined bitrate
    CANStatus = g_CAN_InitializeFD(g_hChannel, CAN_FD_BITRATE);
    if (CANStatus != PCAN_ERROR_OK)
    {
        std::cerr << "Error initializing CAN FD interface: 0x" 
                  << std::hex << CANStatus << std::dec << std::endl;
        UnloadDLL();
        return -1;
    }
    std::cout << "CAN FD interface initialized" << std::endl;

    // Send CAN FD frame
    std::cout << "Sending CAN FD frame..." << std::endl;
    SendMessageBuffer.ID = 0x1234;      // Sample CAN FD ID
    SendMessageBuffer.DLC = 8;          // Message length (8 bytes)
    SendMessageBuffer.MSGTYPE = PCAN_MESSAGE_FD; // Specify FD frame

    // Fill the data field (8 bytes for example)
    SendMessageBuffer.DATA[0] = 0x11;
    SendMessageBuffer.DATA[1] = 0x22;
    SendMessageBuffer.DATA[2] = 0x33;
    SendMessageBuffer.DATA[3] = 0x44;
    SendMessageBuffer.DATA[4] = 0x55;
    SendMessageBuffer.DATA[5] = 0x66;
    SendMessageBuffer.DATA[6] = 0x77;
    SendMessageBuffer.DATA[7] = 0x88;

    CANStatus = g_CAN_WriteFD(g_hChannel, &SendMessageBuffer);
    if (CANStatus != PCAN_ERROR_OK)
    {
        std::cerr << "Error sending CAN FD frame: 0x" 
                  << std::hex << CANStatus << std::dec << std::endl;
    }
    else
    {
        std::cout << "CAN FD frame sent successfully" << std::endl;
    }

    // Read a CAN FD frame from the channel
    std::cout << "Reading CAN FD message..." << std::endl;
    CANStatus = g_CAN_ReadFD(g_hChannel, &SendMessageBuffer, &TimestampBuffer);
    if (CANStatus == PCAN_ERROR_OK)
    {
        std::cout << "Received CAN FD frame with ID: 0x" 
                  << std::hex << SendMessageBuffer.ID << std::dec << std::endl;
    }
    else
    {
        std::cerr << "Error receiving CAN FD frame: 0x" 
                  << std::hex << CANStatus << std::dec << std::endl;
    }

    // Uninitialize the CAN channel and unload the DLL
    CANStatus = g_CAN_Uninitialize(g_hChannel);
    if (CANStatus != PCAN_ERROR_OK)
    {
        std::cerr << "Error uninitializing CAN FD channel" << std::endl;
    }

    std::cout << "Press Enter to exit..." << std::endl;
    std::cin.get();

    UnloadDLL();
    return 0;
}

Re: Not able to initialize can using CPP code

Posted: Mon 3. Feb 2025, 12:01
by M.Heidemann
Hello,

When referencing the PCANBasic documentation, you'll see that this error indicates that the Hardware is currently in use, is any other software already using this channel upon initialization?


BR

marvin