Note
For initializing a Channel for CAN communication only (no flexible data rate), use the method Initialize instead.
public static PcanStatus Initialize(
PcanChannel channel,
BitrateFD bitrate
)
Public Shared Function Initialize (
channel As PcanChannel,
bitrate As BitrateFD
) As PcanStatus
public:
static PcanStatus Initialize(
PcanChannel channel,
BitrateFD^ bitrate
)
OK is returned on success. The typical errors in case of failure are:
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 None, that has not been initialized yet. Each initialized channel should be released when it is not needed anymore.
Initializing a PCAN Channel means:
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, BusWarning, and 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 CAN controller on the PCAN-Gateway device that is represented by that Channel. To initialize the Channel without having to specify the correct bit rate, the parameter BitrateAdapting can be used.
The following example shows the initialization and uninitialization processes using the USB interface (first PCAN-USB 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.
PcanChannel channel = PcanChannel.Usb01;
// The hardware represented by the given handle is initialized with 500 kBit/s nominal and 4 MBit/s data bit rate (SAE J2284-4)
//
PcanStatus result = Api.Initialize(channel, (BitrateFD)BitrateFD.BitrateSaeJ2284_4);
if (result != PcanStatus.OK)
{
// An error occurred
//
Api.GetErrorText(result, out var errorText);
Console.WriteLine(errorText);
}
else
{
// A success message is shown
//
Console.WriteLine($"The hardware represented by the handle {channel} was successfully initialized.");
// The connection to the hardware is finalized when it is no longer needed
//
result = Api.Uninitialize(channel);
if (result != PcanStatus.OK)
{
// An error occurred
//
Api.GetErrorText(result, out var errorText);
Console.WriteLine(errorText);
}
else
Console.WriteLine($"The hardware represented by the handle {channel} was successfully finalized.");
}
Dim channel As PcanChannel = PcanChannel.Usb01
' The hardware represented by the given handle is initialized with 500 kBit/s nominal and 4 MBit/s data bit rate (SAE J2284-4)
'
Dim result As PcanStatus = Api.Initialize(channel, CType(BitrateFD.BitrateSaeJ2284_4, BitrateFD))
If (result <> PcanStatus.OK) Then
' An error occurred
'
Dim errorText As String = String.Empty
Api.GetErrorText(result, errorText)
Console.WriteLine(errorText)
Else
' A success message is shown
'
Console.WriteLine($"The hardware represented by the handle {channel} was successfully initialized.")
' The connection to the hardware Is finalized when it is no longer needed
'
result = Api.Uninitialize(channel)
If (result <> PcanStatus.OK) Then
' An error occurred
'
Dim errorText As String = String.Empty
Api.GetErrorText(result, errorText)
Console.WriteLine(errorText)
Else
Console.WriteLine($"The hardware represented by the handle {channel} was successfully finalized.")
End If
End If
String^ errorText;
PcanChannel channel = PcanChannel::Usb01;
// The hardware represented by the given handle is initialized with 500 kBit/s nominal and 4 MBit/s data bit rate (SAE J2284-4)
//
PcanStatus result = Api::Initialize(channel, (BitrateFD^)BitrateFD::BitrateSaeJ2284_4);
if (result != PcanStatus::OK)
{
// An error occurred
//
Api::GetErrorText(result, errorText);
Console::WriteLine(errorText);
}
else
{
// A success message is shown
//
Console::WriteLine("The hardware represented by the handle {0} was successfully initialized.", channel);
// The connection to the hardware is finalized when it is no longer needed
//
result = Api::Uninitialize(channel);
if (result != PcanStatus::OK)
{
// An error occurred
//
Api::GetErrorText(result, errorText);
Console::WriteLine(errorText);
}
else
Console::WriteLine("The hardware represented by the handle {0} was successfully finalized.", channel);
}
DllNotFoundException | The underlying PCANBasic.dll library could not be found. |
PcanBasicException | The execution of a PCAN-Basic related check operation ended with an unexpected result. Typically, this exception is triggered when a device driver is not installed or is not up to date. |