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 <stdio.h>
#include <conio.h>
#include <windows.h>
#include <stdbool.h>
#include "PCANBasic.h"
int LoadDLL();
int UnloadDLL();
bool GetFunctionAdress(HINSTANCE h_module);
typedef TPCANStatus (__stdcall *PCAN_Initialize)(TPCANHandle Channel, TPCANBaudrate Btr0Btr1, TPCANType HwType, DWORD IOPort, WORD Interrupt);
typedef TPCANStatus (__stdcall *PCAN_Uninitialize)(TPCANHandle Channel);
typedef TPCANStatus (__stdcall *PCAN_Read)(TPCANHandle Channel, TPCANMsg* MessageBuffer, TPCANTimestamp* TimestampBuffer);
typedef TPCANStatus (__stdcall *PCAN_Write)(TPCANHandle Channel, TPCANMsg* MessageBuffer);
PCAN_Initialize g_CAN_Initialize;
PCAN_Uninitialize g_CAN_Uninitialize;
PCAN_Read g_CAN_Read;
PCAN_Write g_CAN_Write;
HINSTANCE g_i_DLL;
TPCANHandle g_hChannel;
TPCANBaudrate g_Baudrate;
char g_LibFileName[] = "PCANBasic";
int LoadDLL()
{
if(g_i_DLL == NULL)
{
g_i_DLL = LoadLibrary("PCANBasic.dll");
if(g_i_DLL == NULL)
{
printf("ERROR: can not load pcanbasic.dll\n");
return -1;
}
else
{
if(GetFunctionAdress(g_i_DLL) == true)
{
printf("Loaded functions from PCANBasic.dll\n");
}
else
{
printf("ERROR: cannot load function addresses\n");
return -2;
}
}
}
return 0;
}
bool GetFunctionAdress(HINSTANCE h_module)
{
g_CAN_Initialize = (PCAN_Initialize)GetProcAddress(h_module, "CAN_Initialize");
g_CAN_Uninitialize = (PCAN_Uninitialize)GetProcAddress(h_module, "CAN_Uninitialize");
g_CAN_Read = (PCAN_Read)GetProcAddress(h_module, "CAN_Read");
g_CAN_Write = (PCAN_Write)GetProcAddress(h_module, "CAN_Write");
return g_CAN_Initialize && g_CAN_Uninitialize && g_CAN_Read && g_CAN_Write;
}
int UnloadDLL()
{
if(g_i_DLL)
{
FreeLibrary(g_i_DLL);
g_CAN_Initialize = NULL;
g_CAN_Uninitialize = NULL;
g_CAN_Read = NULL;
g_CAN_Write = NULL;
return 0;
}
return -1;
}
int main()
{
int ret;
TPCANStatus CANStatus;
TPCANMsg SendMessageBuffer;
TPCANTimestamp TimestampBuffer;
printf("PCAN-Basic Example\n");
printf("Using 500k baud rate\n");
printf("...........CAN Status - 0x%x \n",CANStatus);
printf("............CAN PCAN Eroor - %d \n",PCAN_ERROR_OK);
g_hChannel = PCAN_USBBUS1;
g_Baudrate = PCAN_BAUD_500K;
ret = LoadDLL();
if(ret != 0)
{
printf("Error loading DLL: %i\n", ret);
return -1;
}
printf("status = 0%x\n",CANStatus);
CANStatus = g_CAN_Initialize(g_hChannel, g_Baudrate, 0, 0, 0);
if(CANStatus != PCAN_ERROR_OK)
{
printf("Error with initializing CAN interface: 0x%x\n", CANStatus);
printf("%x\n",CANStatus);
UnloadDLL();
return -1;
}
printf("CAN interface initialized\n");
printf("Sending CAN frames...\n");
SendMessageBuffer.ID = 0x00100;
SendMessageBuffer.LEN = 8;
SendMessageBuffer.MSGTYPE = PCAN_MESSAGE_STANDARD;
SendMessageBuffer.DATA[0] = 0x01;
SendMessageBuffer.DATA[1] = 0x02;
SendMessageBuffer.DATA[2] = 0x03;
SendMessageBuffer.DATA[3] = 0x04;
SendMessageBuffer.DATA[4] = 0x05;
SendMessageBuffer.DATA[5] = 0x06;
SendMessageBuffer.DATA[6] = 0x07;
SendMessageBuffer.DATA[7] = 0x08;
CANStatus = g_CAN_Write(g_hChannel, &SendMessageBuffer);
if (CANStatus != PCAN_ERROR_OK)
{
printf("Error in sending CAN frame: 0x%x\n", CANStatus);
}
else
{
printf("CAN frame sent successfully\n");
}
printf("Reading CAN messages...\n");
CANStatus = g_CAN_Read(g_hChannel, &SendMessageBuffer, &TimestampBuffer);
if(CANStatus == PCAN_ERROR_OK)
{
printf("Received CAN frame with ID: 0x%x\n", SendMessageBuffer.ID);
}
else
{
printf("Error receiving CAN frame: 0x%x\n", CANStatus);
}
CANStatus = g_CAN_Uninitialize(g_hChannel);
if(CANStatus != PCAN_ERROR_OK)
{
printf("Error uninitializing CAN channel\n");
}
printf("Press any key to exit...\n");
_getch();
UnloadDLL();
return 0;