Important
A Channel that is initialized using Initialize must use
Read and Write for communication.
Calling ReadFD
and/or WriteFD will result in a PCAN_ERROR_ILLOPERATION error.
Initializes a PCAN Channel.
public static TPCANStatus Initialize(
ushort Channel,
TPCANBaudrate Btr0Btr1,
TPCANType HwType,
uint IOPort,
ushort Interrupt
)
Public Shared Function Initialize (
Channel As UShort,
Btr0Btr1 As TPCANBaudrate,
HwType As TPCANType,
IOPort As UInteger,
Interrupt As UShort
) As TPCANStatus
public:
static TPCANStatus Initialize(
unsigned short Channel,
TPCANBaudrate Btr0Btr1,
TPCANType HwType,
unsigned int IOPort,
unsigned short Interrupt
)
The return value is a TPCANStatus value. PCAN_ERROR_OK is returned on success. The typical errors in case of failure are:
The parameters HwType, Interrupt, and IOPort are only required for the initialization of non plug-and-play channels.
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:
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_BUSHEAVY, and PCAN_ERROR_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 PCAN_BITRATE_ADAPTING should be used.
The following example shows the initialization and uninitialization processes 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.
TPCANStatus result;
System.Text.StringBuilder strMsg;
// The Not Plug & Play Channel (PCAN-DNG) is initialized
//
result = PCANBasic.Initialize(PCANBasic.PCAN_DNGBUS1, TPCANBaudrate.PCAN_BAUD_500K, TPCANType.PCAN_TYPE_DNG_SJA, 0x378, 7);
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-DNG (Ch-1) was initialized");
// All initialized channels are released
//
PCANBasic.Uninitialize(PCANBasic.PCAN_NONEBUS);
Dim result As TPCANStatus
Dim strMsg As System.Text.StringBuilder
' The Not Plug & Play Channel (PCAN-DNG) is initialized
'
result = PCANBasic.Initialize(PCANBasic.PCAN_DNGBUS1, TPCANBaudrate.PCAN_BAUD_500K, TPCANType.PCAN_TYPE_DNG_SJA, &H378, 7)
If result <> TPCANStatus.PCAN_ERROR_OK Then
' 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-DNG (Ch-1) was initialized")
End If
' All initialized channels are released
'
PCANBasic.Uninitialize(PCANBasic.PCAN_NONEBUS)
TPCANStatus result;
System::Text::StringBuilder^ strMsg;
// The Not Plug & Play Channel (PCAN-DNG) is initialized
//
result = PCANBasic::Initialize(PCANBasic::PCAN_DNGBUS1, TPCANBaudrate::PCAN_BAUD_500K, TPCANType::PCAN_TYPE_DNG_SJA, 0x378, 7);
if (result != TPCANStatus::PCAN_ERROR_OK)
{
// An error occurred, get a text describing the error and show it
//
strMsg = gcnew System::Text::StringBuilder(256);
PCANBasic::GetErrorText(result, 0, strMsg);
Console::WriteLine(strMsg->ToString());
}
else
Console::WriteLine("PCAN-DNG (Ch-1) was initialized");
// All initialized channels are released
//
PCANBasic::Uninitialize(PCANBasic::PCAN_NONEBUS);