Initializes a PCAN Channel which represents a Not Plug & Play PCAN-Device.
class function Initialize( Channel: TPCANHandle; Btr0Btr1: TPCANBaudrate; HwType: TPCANType; IOPort: LongWord; Interrupt: Word ): TPCANStatus; overload;
[DllImport("PCANBasic.dll", EntryPoint = "CAN_Initialize")] public static extern TPCANStatus Initialize( [MarshalAs(UnmanagedType.U1)] TPCANHandle Channel, [MarshalAs(UnmanagedType.U2)] TPCANBaudrate Btr0Btr1, [MarshalAs(UnmanagedType.U1)] TPCANType HwType, UInt32 IOPort, UInt16 Interrupt);
[DllImport("PCANBasic.dll", EntryPoint = "CAN_Initialize")] static TPCANStatus Initialize( [MarshalAs(UnmanagedType::U1)] TPCANHandle Channel, [MarshalAs(UnmanagedType::U2)] TPCANBaudrate Btr0Btr1, [MarshalAs(UnmanagedType::U1)] TPCANType HwType, UInt32 IOPort, UInt16 Interrupt);
<DllImport("PCANBasic.dll", EntryPoint:="CAN_Initialize")> _ Public Shared Function Initialize( _ <MarshalAs(UnmanagedType.U1)> _ ByVal Channel As TPCANHandle, _ <MarshalAs(UnmanagedType.U2)> _ ByVal Btr0Btr1 As TPCANBaudrate, _ <MarshalAs(UnmanagedType.U1)> _ ByVal HwType As TPCANType, _ ByVal IOPort As UInt32, _ ByVal Interrupt As UInt16) As TPCANStatus End Function
def Initialize( self, Channel, Btr0Btr1, HwType = TPCANType(0), IOPort = c_uint(0), Interrupt = c_ushort(0))
Parameters |
Description |
Channel |
The handle of a PCAN Channel (see TPCANHandle). |
Btr0Btr1 |
The speed for the communication (BTR0BTR1 code). |
HwType |
The type of hardware and operation mode (see TPCANMode). |
IOPort |
The I/O address for the parallel port. |
Interrupt |
Interrupt number of the parallel port. |
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:
|
PCAN_ERROR_REGTEST: |
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. |
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 configure the filter to catch all messages being transmitted in the bus.
- 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, 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 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: Unlike 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 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; 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 StringBuilder(256); PCANBasic.GetErrorText(result, 0, strMsg); MessageBox.Show(strMsg.ToString()); } else MessageBox.Show("PCAN-DNG (Ch-1) was initialized"); // All initialized channels are released // PCANBasic.Uninitialize(PCANBasic.PCAN_NONEBUS);
C++/CLR:
TPCANStatus result; 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 StringBuilder(256); PCANBasic::GetErrorText(result, 0, strMsg); MessageBox::Show(strMsg->ToString()); } else MessageBox::Show("PCAN-DNG (Ch-1) was initialized"); // All initialized channels are released // PCANBasic::Uninitialize(PCANBasic::PCAN_NONEBUS);
Visual Basic:
Dim result As TPCANStatus Dim strMsg As 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 StringBuilder(256) PCANBasic.GetErrorText(result, 0, strMsg) MessageBox.Show(strMsg.ToString) Else MessageBox.Show("PCAN-DNG (Ch-1) 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 Not Plug & Play Channel (PCAN-DNG) is initialized // result := TPCANBasic.Initialize(TPCANBasic.PCAN_DNGBUS1, PCAN_BAUD_500K, PCAN_TYPE_DNG_SJA, $378, 7); 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-DNG (Ch-1) was initialized','Success', MB_OK); // All initialized channels are released // TPCANBasic.Uninitialize(TPCANBasic.PCAN_NONEBUS); end;
Python:
# The Not Plug & Play Channel (PCAN-DNG) is initialized # objPCAN = PCANBasic() result = objPCAN.Initialize(PCAN_DNGBUS1, PCAN_BAUD_500K, PCAN_TYPE_DNG_SJA, 0x378, 7) 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-DNG (Ch-1) 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
|