PCANBasicInitializeFD Method

Initializes a FD capable PCAN Channel.

Definition

Namespace: Peak.Can.Basic.BackwardCompatibility
Assembly: PCANBasic.NET (in PCANBasic.NET.dll) Version: 4.9.0
C#
public static TPCANStatus InitializeFD(
	ushort Channel,
	string BitrateFD
)

Parameters

Channel  UInt16
The handle of a FD capable PCAN Channel (see PCAN Handle Definitions).
BitrateFD  String
The speed for the communication (see FD Bit rate Parameter Definitions).

Return Value

TPCANStatus

The return value is a TPCANStatus value. 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.
  • 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 not CAN-FD capable and cannot be initialized using this method.
    • The desired PCAN Channel is a LAN Channel, which uses a different bit rate than the specified.
  • PCAN_ERROR_ILLMODE: Indicates that the hardware is working in a different or incompatible mode than requested. Possible causes are:
    • The desired PCAN Channel is being operated in CAN 2.0 A/B mode.
  • 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 (PcanApi connection).
  • PCAN_ERROR_NODRIVER: The driver needed for connecting the desired PCAN Channel is not loaded.

Remarks

  Note

For initializing a channel for CAN communication (no CAN-FD), use the method Initialize instead.

As indicated by its name, the InitializeFD 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 configured to adapt an available bit rate; see: PCAN_BITRATE_ADAPTING parameter).
  • to set the channel in Listen-Only mode. (Only if the channel was configured to work in Listen-Only mode; see: PCAN_LISTEN_ONLY parameter).
  • to configure the filter to catch all messages being transmitted in the bus.
  • to set-up the default values of the different parameters (see method GetValue).
  • to set the Receive Status of the channel. (See: PCAN_RECEIVE_STATUS parameter).

Other than in the PCAN-Light API, the Initialization process will fail if an application tries 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 PCAN_ERROR_BUSOFF, PCAN_ERROR_BUSWARNING, and PCAN_ERROR_BUSPASSIVE, 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 PCAN_BITRATE_ADAPTING should be used.

Example

The following example shows the initialization and uninitialization processes for a CAN-FD capable channel (channel 1 of a PCAN-USB Pro FD 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#
string bitrate;
TPCANStatus result;
System.Text.StringBuilder strMsg;

// Defines a FD Bit rate string with nominal bit rate of 500 kBit/s and data bit rate of 2 MB (SAE J2284-4)
//
bitrate = "f_clock=80000000,nom_brp=2,nom_tseg1=63,nom_tseg2=16,nom_sjw=16,data_brp=2,data_tseg1=15,data_tseg2=4,data_sjw=4";

// The FD capable Channel (PCAN-USB Pro FD) is initialized
//
result = PCANBasic.InitializeFD(PCANBasic.PCAN_USBBUS1, bitrate);
if (result != TPCANStatus.PCAN_ERROR_OK)
{
    // An error occurred, get a text describing the error and show it
    //
    strMsg = new System.Text.StringBuilder(256);
    PCANBasic.GetErrorText(result, 0, strMsg);
    Console.WriteLine(strMsg.ToString());
}
else
    Console.WriteLine("PCAN-USB Pro FD (Ch-1) was initialized");

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

See Also