ApiGetStatus Method

Retrieves the current status of the CAN controller of the hardware represented by a PCAN Channel.

Definition

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

Parameters

channel  PcanChannel
The handle of a PCAN Channel.

Return Value

PcanStatus

OK is returned if the CAN controller of the hardware represented by the given PCAN Channel is in a normal state. 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.
  • IllegalHardwareHandle: Indicates that the hardware represented by channel was removed or deactivated.
  • IllegalHandle: Indicates that the channel contains an invalid value.
  • BusHeavyIndicates a bus error within the CAN controller of the hardware represented by the given PCAN Channel. An error counter reached the 'heavy' limit.
  • BusLightIndicates a bus error within the CAN controller of the hardware represented by the given PCAN Channel. An error counter reached the 'light' limit.
  • BusPassiveIndicates a bus error within the CAN controller of the hardware represented by the given PCAN Channel. The CAN controller is in error passive state.
  • BusWarningIndicates a bus error within the CAN controller of the hardware represented by the given PCAN Channel. An error counter reached the 'warning' limit.
  • BusOffIndicates a bus error within the CAN controller of the hardware represented by the given PCAN Channel. The CAN controller is in bus-off state.

Remarks

When the hardware status is bus-off, an application cannot communicate anymore. Consider using the BusOffAutoReset parameter, which instructs the API to automatically reset the CAN controller when a bus-off state is detected.

Another way to reset bus errors like BusOff, BusPassive, and others:

  • Performing an uninitialize / initialize cycle: This causes a hardware reset, but only when no more clients are connected to that channel.
  • Using the parameter HardResetStatus: It instructs this method to explicitly perform a hardware reset regardless of whether other clients are connected to that channel.

Example

The following example shows the use of the GetStatus method using the USB interface (first PCAN-USB hardware). A Channel is initialized and used to send 255 messages in about 4 seconds. After each sending the status of the CAN controller is checked.

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.

  1. C#
    private static void GetStatusExample()
    {            
        PcanChannel channel = PcanChannel.Usb01;
    
        // The hardware represented by the given handle is initialized with 500 kBit/s bit rate (BTR0/BTR1 0x001C)
        //
        PcanStatus result = Api.Initialize(channel, Bitrate.Pcan500);
        if (result != PcanStatus.OK)
        {
            // An error occurred
            //
            Api.GetErrorText(result, out var errorText);
            Console.WriteLine(errorText);
        }
        else
        {
            // A success message on connection is shown.
            //
            Console.WriteLine($"The hardware represented by the handle {channel} was successfully initialized.");
    
            // Start a thread for a writing messages and wait until it is finished
            //
            var worker = new System.Threading.Thread(DoWriteProcess);
            worker.Start(channel);
            worker.Join();
    
            // 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.");
        }
    
    }
    
    private static void DoWriteProcess(object channelToUse)
    {
        // Channel to use for message reading
        //
        PcanChannel channel = (PcanChannel)channelToUse;
        PcanStatus result;
    
        // Message to be send
        PcanMessage toSend = new PcanMessage(0x100, MessageType.Standard, 1, new byte[1]);            
    
        // 255 messages are send, using the first data byte as a message counter
        //
        for (byte i=0; i < byte.MaxValue; i++)
        {
            // Send the message
            //
            toSend.Data[0] = (byte)(i + 1);
            result = Api.Write(channel, toSend);
    
            // Check for any unexpected error
            //
            if ((result == PcanStatus.Initialize) || (result == PcanStatus.IllegalHandle) 
                || (result == PcanStatus.InvalidOperation) || (result == PcanStatus.InvalidValue))
            {
                // An unexpected error occurred
                //
                Api.GetErrorText(result, out var errorText);
                Console.WriteLine(errorText);
                break;
            }
    
            // The status of the Channel is checked and outputted 
            // 
            result = Api.GetStatus(channel);
            switch (result)
            {
                case PcanStatus.BusLight:
                    Console.WriteLine($"An error counter of the Channel {channel} has reached the bus-light limit by sending the message number {i + 1}.");
                    break;
                case PcanStatus.BusHeavy:
                    Console.WriteLine($"An error counter of the Channel {channel} has reached the bus-heavy limit by sending the message number {i + 1}.");
                    break;
                case PcanStatus.BusOff:
                    Console.WriteLine($"An error counter of the Channel {channel} has reached the bus-off state by sending the message number {i + 1}.");
                    break;
                case PcanStatus.OK:
                    Console.WriteLine($"Status of Channel {channel} after sending message {i + 1} is 'OK'.");
                    break;
                default:
                    // An unexpected error occurred
                    //
                    Api.GetErrorText(result, out var errorText);
                    Console.WriteLine(errorText);
                    break;
            }
    
            // Wait some milliseconds to slow down things 
            //
            System.Threading.Thread.Sleep(15);
        }
    }

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