ApiUninitialize Method

Uninitializes one or all PCAN Channels initialized by the methods Initialize and Initialize.

Definition

Namespace: Peak.Can.Basic
Assembly: PCANBasic.NET (in PCANBasic.NET.dll) Version: 4.9.0
C#
public static PcanStatus Uninitialize(
	PcanChannel channel
)

Parameters

channel  PcanChannel
The handle of a PCAN Channel.

Return Value

PcanStatus

OK is returned on success. The typical errors in case of failure are:

  • Initialize: Indicates that the channel cannot be terminated because it was not found in the list of reserved Channels of the calling application.
  • IllegalHandle: Indicates that the channel contains an invalid value.

Remarks

This method can be used to achieve two different goals:

  • To release a single Channel: Passing the handle of a PCAN Channel initialized before as parameter (see Initialize / Initialize) terminates the connection with that Chanel. If the given channel cannot be found, an error is returned.
  • To release all connected Channels: Passing the Channel None as parameter, terminates all connections, that have been issued and maintained by the calling application. This operation does not generate errors if no Channels were found and terminated.

  Important

When a connection is terminated, the underlying hardware's transmit queue will not be immediately discarded. The API will wait some time before terminating, so that the hardware has time to send (or try to send) the unsent messages. When this time runs out (about 500 milliseconds), the rest of the messages in the queue (if any) are discarded.

Example

  1. The following example shows the single release process 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.

    C#
    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.");
    }
  2. The following example shows the multiple release process using the USB interface (first and second 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.

    C#
    PcanChannel[] channels = new PcanChannel[] { PcanChannel.Usb01, PcanChannel.Usb02 };
    
    // The hardware represented by the given handles are initialized with 500 kBit/s bit rate (BTR0/BTR1 0x001C)
    //
    for (int i = 0; i < channels.Length; i++)
    {
        PcanStatus result = Api.Initialize(channels[i], Bitrate.Pcan500);
        if (result != PcanStatus.OK)
        {
            // An error occurred
            //
            Api.GetErrorText(result, out var errorText);
            Console.WriteLine($"Error initializing the Channel {channels[i]}: {errorText}");
            break;
        }
        else
            // A success message is shown
            //
            Console.WriteLine($"The hardware represented by the handle {channels[i]} was successfully initialized.");
    }
    
    // Any active hardware connection is finalized when no more needed
    //
    Api.Uninitialize(PcanChannel.None);
    Console.WriteLine("All active connections were terminated.");

Exceptions

DllNotFoundExceptionThe underlying PCANBasic.dll library could not be found.
PcanBasicExceptionThe 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.

See Also