User manual

63 © 2007-2010 Analytica GmbH
Chapter 7. Programming examples
7.1. Programming language C/C++
The AnaGate programming API can be used on Windows systems as well as on linux systems (X86). All
available API functions are coded operating system independant, so that source code once created can be
used on both operating systems. Only the way the libraries are linked on the different operating systems
or different compilers have to be customized.
Windows operating systems
There are basically two ways to access the API functions for the C/C++ programmer:
When directly accessing the library all functions has to made known by preceding calls to the Win32
methods LoadLibrary and GetProcAddress.
The functions can be alternatively accessed easily via an import library, which automatically loads all
DLL functions and make them available implicitely.
In the following examples it is assumed that the second option is used and the corresponding import library
is linked.
7.1.1. CAN Console application C/C++ (MSVC)
This programming example for C++ demonstrates how to connect to an AnaGate CAN and how to process
received CAN data via a callback function.
Note
The source code of the example can be found in directory Samples/CAN-VC6 resp.
Samples/CAN-VC7 on the CD.
#include "AnaGateDLLCAN.h"
WINAPI void MyCallback(int nIdentifier, const char * pcBuffer, int nBufLen, int nFlags, int nHandle)
{
std::cout << "CAN-ID=" << nIdentifier << ", Data=";
for ( int i = 0; i < nBufLen; i++ )
{
std::cout " 0x" << std::hex << pcBuffer[i];
}
std::cout << std::endl;
}
int main( )
{
int hHandle = NULL;
// opens AnaGate CAN duo device on port A, timeout after 1000 millseconds
int nRC = CANOpenDevice(&hHandle,FALSE,TRUE,0,"192.168.2.1",1000);
if ( nRC == 0 )
{
nRC = CANSetCallback(hHandle,MyCallback);
getch(); // wait for keyborad input
}
if ( nRC == 0 )
{
nRC = CANCloseDevice(hHandle);// close device
}
}