Hello Experts,
I have difficulties to establish communication with a microcontroller using CAN bus. My project has came into limit of a device hence I have to use multiple devices and controlling all of them via a CAN bus. Searching everywhere but no luck on helping me with CAN Layer2 API that I am using. I'm really new with CAN programming.
DLL used:
canL2.dll
Can_def.h
CANL2.H
Below example is C codes from Softing but I'm not sure how to use them with VC++.
Thanks in advance.
mraaf
What I have tried:
#include "can_def.h"
#include "canL2.h"
main()
{
int ret;
CANL2_CH_STRUCT ch1, ch2;
CAN_HANDLE can1, can2;
L2CONFIG L2Config[2];
char custom, *pName[2] = {NULL,NULL};
PCHDSNAPSHOT pBuffer = NULL;
unsigned long u32NeededBufferSize, u32NumOfChannels, u32ProvidedBufferSize, channelIndex;
int sw_version, fw_version, hw_version, license, chip_type, i;
printf ("Analyzing your system ...\n");
u32ProvidedBufferSize = 0;
ret = CANL2_get_all_CAN_channels(0, &u32NeededBufferSize, &u32NumOfChannels, NULL);
if(!u32NumOfChannels)
{
printf("you have no Softing CAN interface card plugged in your Computer!\n");
printf("plug a interface card first and start this program again after this.\n");
custom=getch();
exit(0);
}
if(ret)
{
printf("The driver reported a problem: Error Code %x\n", ret);
custom=getch();
exit(0);
}
pBuffer = malloc(u32NeededBufferSize);
if(!pBuffer)
{
printf("Out of memory\n");
custom=getch();
exit(0);
}
u32ProvidedBufferSize = u32NeededBufferSize;
ret = CANL2_get_all_CAN_channels(u32ProvidedBufferSize, &u32NeededBufferSize, &u32NumOfChannels, pBuffer);
if(ret)
{
printf("The driver reported a problem: Error Code %x\n", ret);
custom=getch();
free(pBuffer);
exit(0);
}
printf("You have %u Softing CAN channels in your system\n\n", u32NumOfChannels);
printf("\tname\t\t serialnumber\t type\t\t chan.\t open\n");
printf("------------------------------------------------------------------------\n");
printf("\n");
for(channelIndex=0; channelIndex<u32NumOfChannels; channelIndex++)
{
PCHDSNAPSHOT pCh = &pBuffer[channelIndex];
printf("% 17s\t %09u % 18s\t %2u\t % 5s\n",
pCh->ChannelName,
pCh->u32Serial,
getDeviceType(pCh->u32DeviceType),
pCh->u32PhysCh,
(pCh->bIsOpen) ? "yes" : "no");
if(!pCh->bIsOpen)
{
if(ch<2)
{
pName[ch] = pCh->ChannelName;
ch++;
}
}
}
if(ch < 2)
{
if(ch == 0)
{
printf("\nAll CAN channels are used by other applications!\n");
printf("Type <return> to exit program!\n");
custom=getch();
free(pBuffer);
exit(0);
}
printf("\nYou have only 1 channel plugged, the commands for the second channel will not \n");
printf("be available!\n");
printf("Type <return> to proceed!\n");
custom=getch();
}
L2Config[0].bEnableAck = GET_FROM_SCIM;
L2Config[0].bEnableErrorframe = GET_FROM_SCIM;
L2Config[0].s32AccCodeStd = GET_FROM_SCIM;
L2Config[0].s32AccCodeXtd = GET_FROM_SCIM;
L2Config[0].s32AccMaskStd = GET_FROM_SCIM;
L2Config[0].s32AccMaskXtd = GET_FROM_SCIM;
L2Config[0].s32OutputCtrl = GET_FROM_SCIM;
L2Config[0].s32Prescaler = GET_FROM_SCIM;
L2Config[0].s32Sam = GET_FROM_SCIM;
L2Config[0].s32Sjw = GET_FROM_SCIM;
L2Config[0].s32Tseg1 = GET_FROM_SCIM;
L2Config[0].s32Tseg2 = GET_FROM_SCIM;
memcpy(&L2Config[1], &L2Config[0], sizeof(L2CONFIG));
strcpy(ch1.sChannelName,pName[0]);
if(ch > 1)
{
strcpy(ch2.sChannelName,pName[1]);
}
if(ch > 1)
{
printf("\ninitializing %s and %s\n", ch1.sChannelName, ch2.sChannelName);
}
else
{
printf("\ninitializing %s\n", ch1.sChannelName);
}
printf("Initialization started...\n");
free(pBuffer);
ret = INIL2_initialize_channel(&ch1.ulChannelHandle, ch1.sChannelName);
if(ret)
{
printf("Error %u in INIL2_initialize_channel()\n",ret);
custom=getch();
return ret;
}
can1 = ch1.ulChannelHandle;
if(ch > 1)
{
ret = INIL2_initialize_channel(&ch2.ulChannelHandle, ch2.sChannelName);
if(ret)
{
printf("Error %u in INIL2_initialize_channel()\n",ret);
custom=getch();
return ret;
}
can2 = ch2.ulChannelHandle;
}
else
{
can2 = (CAN_HANDLE)INVALID_HANDLE_VALUE;
}
ret = PrepareForIntEvents(&L2Config[0], &can1, &L2Config[1], &can2);
if(ret)
{
printf("Error %u in PrepareForIntEvents()\n",ret);
custom=getch();
return ret;
}
ret = CANL2_initialize_fifo_mode(can1, &L2Config[0]);
if(ret)
{
printf("Error %u in CANL2_initialize_fifo_mode()\n",ret);
printf("please configure the channel by the SCIM first\n");
custom=getch();
return ret;
}
if(ch > 1)
{
ret = CANL2_initialize_fifo_mode(can2, &L2Config[1]);
if(ret)
{
printf("Error %u in CANL2_initialize_fifo_mode()\n",ret);
printf("please configure the channel by the SCIM first\n");
custom=getch();
return ret;
}
}
ret = CANL2_get_version(can1, &sw_version, &fw_version, &hw_version, &license, &chip_type);
if(ret)
{
printf("Error %u in CANL2_get_version(CAN1)\n",ret);
}
printf("\n VERSION INFO CAN1: \n\n");
printf(" - Software version: %u.%02u\n", sw_version/100, sw_version%100);
printf(" - Firmware version: %u.%02u\n", fw_version/100, fw_version%100);
printf(" - Hardware version: %x.%02x\n", hw_version/0x100, hw_version%0x100);
printf(" - CAN chip : %s\n", (chip_type==1000)? "SJA1000": (chip_type==161) ? "Infineon XC161" : "Infineon XE164");
if(ch > 1)
{
ret = CANL2_get_version(can2, &sw_version, &fw_version, &hw_version, &license, &chip_type);
if(ret)
{
printf("Error %u in CANL2_get_version(CAN1)\n",ret);
}
printf("\n VERSION INFO CAN2: \n\n");
printf(" - Software version: %u.%02u\n", sw_version/100, sw_version%100);
printf(" - Firmware version: %u.%02u\n", fw_version/100, fw_version%100);
printf(" - Hardware version: %x.%02x\n", hw_version/0x100, hw_version%0x100);
printf(" - CAN chip : %s\n", (chip_type==1000)? "SJA1000": (chip_type==161) ? "Infineon XC161" : "Infineon XE164");
printf("The 2 channels are online now!\n\n");
}
else
{
printf("\n\nThe CAN channel is online now, a second channel is not available!\n\n");
}
custom = 'h';
do
{
Sleep(0);
ret=UserRequestFIFO(custom, can1, can2);
while (!kbhit())
{
}
custom=getch();
if (ret < 0)
{
printf("-->User request failed \n");
INIL2_close_channel(can1);
if(ch > 1)
{
INIL2_close_channel(can2);
}
return(-1);
}
}
while (custom != 'q');
SetEvent(hThreadEvents[1]);
WaitForSingleObject(hIntThread[0], 2000);
if(ch > 1)
{
SetEvent(hThreadEvents[3]);
WaitForSingleObject(hIntThread[1], 2000);
}
for(i=0; i<((ch>1)? 2 : 1); i++)
{
CloseHandle(hIntThread[i]);
}
for(i=0; i<((ch>1)? 4 : 2); i++)
{
CloseHandle(hThreadEvents[i]);
}
INIL2_close_channel(can1);
if(ch > 1)
{
INIL2_close_channel(can2);
}
return ret;
}