CAN_Initialize

PCAN-Basic

PCAN-Basic Documentation
Home
PreviousUpNext
CAN_Initialize

Initializes a PCAN Channel.

Syntax
#ifdef __cplusplus
#define _DEF_ARG =0   // Using of default arguments
#else
#define _DEF_ARG
#endif

TPCANStatus __stdcall CAN_Initialize(
        TPCANHandle Channel,
        TPCANBaudrate Btr0Btr1,
        TPCANType HwType _DEF_ARG,
        DWORD IOPort _DEF_ARG,
        WORD Interrupt _DEF_ARG
);
Parameters 
Description 
Channel 
The handle of a PCAN Channel (see TPCANHandle). 
Btr0Btr1 
The speed for the communication (BTR0BTR1 code). 
HwType 
The type of the Non-Plug-and-Play hardware and its operation mode. 
IOPort 
The I/O address for the parallel port of the Non-Plug-and-Play hardware. 
Interrupt 
The Interrupt number of the parallel port of the Non-Plug-and-Play hardware. 

The return value is a TPCANStatus code. PCAN_ERROR_OK is returned on success. The typical errors in case of failure are:

PCAN_ERROR_CAUTION: 
Indicates that the channel has been initialized but at a different bit rate as the given one. 
PCAN_ERROR_ILLHANDLE: 
Indicates that the desired PCAN Channel is not valid. Check the list of valid Channels
PCAN_ERROR_ILLHW: 
indicates that the desired PCAN Channel is not available. 
PCAN_ERROR_ILLOPERATION: 
Indicates that an action cannot be executed due to the state of the hardware. Possible causes are:
  • The desired PCAN-Channel is a LAN Channel, which uses a different bit rate than the specified.

 

PCAN_ERROR_REGTEST: 
(Not-Plug-And-Play Only) Indicates a problem with hardware registration, normally due to wrong values in the parameters 'HwType', 'IOPort' and 'Interrupt'. 
PCAN_ERROR_INITIALIZE: 
Indicates that the desired PCAN channel cannot be connected because it is already in use (PCAN-Basic / PCAN-Light environment). 
PCAN_ERROR_NETINUSE: 
Indicates that the desired PCAN-Channel is being used with a different bit rate (PCAN-View). 
PCAN_ERROR_HWINUSE: 
Indicates that the desired PCAN-Channel is being used (CanApi2 connection). 
PCAN_ERROR_NODRIVER: 
The driver needed for connecting the desired PCAN Channel is not loaded. 

Note on correspondence of Functions

A Channel that is initialized using CAN_Initialize must use CAN_Read and CAN_Write for communication. Calling CAN_ReadFD and/or CAN_WriteFD will result in a PCAN_ERROR_ILLOPERATION error. 

 

As indicated by its name, the CAN_Initialize function initiates a PCAN Channel, preparing it for communicate within the CAN bus connected to it. Calls to the API functions will fail if they are used with a Channel handle, different than PCAN_NONEBUS, that has not been initialized jet. Each initialized channel should be released when it is not needed anymore. 

Initializing a PCAN Channel means:

  • to reserve the Channel for the calling application/process.
  • to allocate channel resources, like receive and transmit queues.
  • to register/connect the Hardware denoted by the channel handle.
  • to check and adapt the bus speed, if the Channel is already in use. (Only if the Channel was pre-configured as Bitrate Adapting; see: Bitrate-Adapting Parameter).
  • to set the channel in Listen-Only mode. (Only if the channel was pre-configured as Listen-Only; see: Listen-Only Parameter).
  • to open the messages filter for the application/process.
  • to set-up the default values of the different parameters (See CAN_GetValue).
  • to set the Receive Status of the channel. (Pre-configured value; see: Receive Status Parameter).

Different than the PCAN-Light API, the Initialization process will fail if an application try to initialize a PCAN-Channel that has been initialized already within the same process. 

The PCAN-Basic API use the same function for initializations of both, Plug-And-Play and Not-Plug-And-Play hardware. The CAN_Initialize function has three additional parameters that are only for the connection of Non-Plug-And-Play hardware. With Plug-And-Play hardware, however, only two parameters are to be supplied. The remaining three are not evaluated. 

Take in consideration that initializing a channel causes a reset of the CAN hardware , when the bus status is other than OK. In this way errors like BUSOFF, BUSHEAVY, and BUSLIGHT, are removed. 

PCAN-LAN Channels 

A PCAN-LAN channel doesn't allow changing the bit rate using PCAN-Basic. In order to connect a PCAN-LAN Channel it is necessary to know the bit rate of the PCAN-Gateway device that is represented by that channel. If the bit rate is not known, the parameter Bitrate-Adapting should be used.

The following example shows the initialize and uninitialize processes for a Plug-And-Play channel (channel 2 of a PCAN-PCI hardware) and for a Not-Plug-And-Play channel (channel 1 of the PCAN-DNG). In case of failure, the returned code will be translated to a text (according with the operating system language) in English, German, Italian, French or Spanish, and it will be shown to the user. 

C++:  

TPCANStatus result;
char strMsg[256];

// The Plug & Play Channel (PCAN-PCI) is initialized
//
result = CAN_Initialize(PCAN_PCIBUS2,PCAN_BAUD_500K);
if(result != PCAN_ERROR_OK)
{
    // An error occurred, get a text describing the error and show it
    //
    CAN_GetErrorText(result, 0, strMsg);
    MessageBox(strMsg);
}
else
    MessageBox("PCAN-PCI (Ch-2) was initialized");

// The Not Plug & Play Channel (PCAN-Dongle) is initialized
//
result = CAN_Initialize(PCAN_DNGBUS1, PCAN_BAUD_500K, PCAN_TYPE_DNG, 0x378, 7);
if(result != PCAN_ERROR_OK)
{
    // An error occurred, get a text describing the error and show it
    //
    CAN_GetErrorText(result, 0, strMsg);
    MessageBox(strMsg);
}
else
    MessageBox("PCAN-Dongle (Ch-1) was initialized");

....

// All initialized channels are released
//
CAN_Uninitialize(PCAN_NONEBUS);
Copyright © 2017. PEAK-System Technik GmbH. All rights reserved.
Send feedback to this documentation