Initialize(TPCANHandle, TPCANBaudrate)

PCAN-Basic

PCAN-Basic Documentation
Home
PreviousUpNext
Initialize(TPCANHandle, TPCANBaudrate)

Initializes a PCAN Channel which represents a Plug & Play PCAN-Device.

Syntax
class function Initialize(
    Channel: TPCANHandle;
    Btr0Btr1: TPCANBaudrate
    ): TPCANStatus; overload;
public static extern TPCANStatus Initialize(
    TPCANHandle Channel,
    TPCANBaudrate Btr0Btr1);
static TPCANStatus Initialize(
    TPCANHandle Channel,
    TPCANBaudrate Btr0Btr1);
Public Shared Function Initialize( _
    ByVal Channel As TPCANHandle, _
    ByVal Btr0Btr1 As TPCANBaudrate) As TPCANStatus
End Function
def Initialize(
    self,
    Channel,
    Btr0Btr1)
Parameters 
Description 
Channel 
The handle of a PCAN Channel (see TPCANHandle). 
Btr0Btr1 
The speed for the communication (BTR0BTR1 code). 

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_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. 

As indicated by its name, the Initialize method initiates a PCAN Channel, preparing it for communicate within the CAN bus connected to it. Calls to the other methods will fail if they are used with a Channel handle, different than PCAN_NONEBUS, that has not been initialized yet. 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 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. 

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. 

Python Notes 

  • Class-Method: Different than the .NET Framework, under Python a variable has to be instantiated with an object of type PCANBasic in order to use the API functionality.
  • Python's first argument convention: Under Python, 'self' is a parameter that is automatically included within the call of this method, within a PCANBasic object and hasn't to be indicated in a method call. This parameter represents the calling object itself.
  • Plug-&-Play and No-Plug-&-Play hardware: In order to initialize a channel which represents a Plug-&-Play PCAN device, only the Channel-handle and bit rate parameters are needed. The other parameters will be assigned their default values. For No-Plug-&-Play devices, all parameters have to be entered.

The following example shows the initialize and uninitialize processes for a Plug-And-Play channel (channel 2 of a PCAN-PCI hardware). 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;
StringBuilder strMsg;

// The Plug & Play Channel (PCAN-PCI) is initialized
//
result = PCANBasic.Initialize(PCANBasic.PCAN_PCIBUS2, TPCANBaudrate.PCAN_BAUD_500K);
if (result != TPCANStatus.PCAN_ERROR_OK)
{
    // An error occurred, get a text describing the error and show it
    //
    strMsg = new StringBuilder(256);
    PCANBasic.GetErrorText(result, 0, strMsg);
    MessageBox.Show(strMsg.ToString());
}
else
    MessageBox.Show("PCAN-PCI (Ch-2) was initialized");

// All initialized channels are released
//
PCANBasic.Uninitialize(PCANBasic.PCAN_NONEBUS);

C++/CLR:

TPCANStatus result;
StringBuilder^ strMsg;

// The Plug & Play Channel (PCAN-PCI) is initialized
//
result = PCANBasic::Initialize(PCANBasic::PCAN_PCIBUS2, TPCANBaudrate::PCAN_BAUD_500K);
if (result != TPCANStatus::PCAN_ERROR_OK)
{
    // An error occurred, get a text describing the error and show it
    //
    strMsg = gcnew StringBuilder(256);
    PCANBasic::GetErrorText(result, 0, strMsg);
    MessageBox::Show(strMsg->ToString());
}
else
    MessageBox::Show("PCAN-PCI (Ch-2) was initialized");

// All initialized channels are released
//
PCANBasic::Uninitialize(PCANBasic::PCAN_NONEBUS);

Visual Basic:

Dim result As TPCANStatus
Dim strMsg As StringBuilder

' The Plug & Play Channel (PCAN-PCI) is initialized
'
result = PCANBasic.Initialize(PCANBasic.PCAN_PCIBUS2, TPCANBaudrate.PCAN_BAUD_500K)
If result <> TPCANStatus.PCAN_ERROR_OK Then
    ' An error occurred, get a text describing the error and show it
    '
    strMsg = New StringBuilder(256)
    PCANBasic.GetErrorText(result, 0, strMsg)
    MessageBox.Show(strMsg.ToString)
Else
    MessageBox.Show("PCAN-PCI (Ch-2) was initialized")
End If

' All initialized channels are released
'
PCANBasic.Uninitialize(PCANBasic.PCAN_NONEBUS)

Pascal OO:

var
 result : TPCANStatus;
 strMsg: array [0..256] of Char;
begin
    // The Plug & Play Channel (PCAN-PCI) is initialized
    //
    result := TPCANBasic.Initialize(TPCANBasic.PCAN_PCIBUS2, PCAN_BAUD_500K);
    If (result <> PCAN_ERROR_OK) Then
    begin
        // An error occurred, get a text describing the error and show it
        //
        TPCANBasic.GetErrorText(result, 0, strMsg);
        MessageBox(0, strMsg, 'Error',MB_OK);
    end
    else
        MessageBox(0,'PCAN-PCI (Ch-2) was initialized','Success', MB_OK);

    // All initialized channels are released
    //
    TPCANBasic.Uninitialize(TPCANBasic.PCAN_NONEBUS);
end;

Python:

# The Plug & Play Channel (PCAN-PCI) is initialized
#
objPCAN = PCANBasic()
result = objPCAN.Initialize(PCAN_PCIBUS2, PCAN_BAUD_500K)
if result != PCAN_ERROR_OK:
    # An error occurred, get a text describing the error and show it
    #
    result = objPCAN.GetErrorText(result)
    print result[1]
else:
    print "PCAN-PCI (Ch-2) was initialized"

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