Finding Correct PCAN-Basic PCAN handle value for Linux

USB to CAN Interface
Post Reply
eoztosun
Posts: 5
Joined: Tue 11. Mar 2025, 06:09

Finding Correct PCAN-Basic PCAN handle value for Linux

Post by eoztosun » Thu 20. Mar 2025, 09:17

Hi,

I have the following piece of code that works well on Windows 10 and Windows 11 intel x86 machines. It basically checks whether the PCAN-USB device is connected.

On Debian/Linux intel x86, I'm having the following output most of the time when I plug out and then replug the PCAN-USB.
The value of a PCAN-Hardware handle is invalid
I see that there are macros, from 1 to 16 for specifying the handle (i.e PCAN_USBBUS1, PCAN_USBBUS2...PCAN_USBBUS16). Am I suppose to try all these values to see which one is correct?

Code: Select all

{
    std::lock_guard<std::mutex> lock(mainMutex);
    status = CAN_Initialize(PcanHandle, Bitrate);
}
CANstsProcessedFlag = false;
caller->CANStatusWorkerNotify(); 
while (true) {
    std::this_thread::sleep_for(std::chrono::milliseconds(CAN_CONN_RECHECK_TIMEOUT_MS));
    if (CANstsProcessedFlag) {
        {
            std::lock_guard<std::mutex> lock(mainMutex);
            prevStatus = status;
            status = CAN_GetStatus(PcanHandle);
            if (status != PCAN_ERROR_OK) {
                status = CAN_Initialize(PcanHandle, Bitrate);
            }
        }
        CANstsProcessedFlag = false;
        caller->CANStatusWorkerNotify();
    }
}

F.Vergnaud
Software Development
Software Development
Posts: 305
Joined: Mon 9. Sep 2013, 12:21

Re: Finding Correct PCAN-Basic PCAN handle value for Linux

Post by F.Vergnaud » Mon 24. Mar 2025, 16:04

Hello,

If you're requesting the status of a channel that is not available (anymore) then this error message is consistent.

To check which channels are available you should use parameter PCAN_ATTACHED_CHANNELS with CAN_GetValue function: it will get information about all existing PCAN channels on a system in a single call.
You'll find description and a sample in documentation "PCAN-Parameter_Documentation.pdf", page 20-22.
If you prefer to check a single channel, you should use parameter PCAN_CHANNEL_CONDITION.
Best regards,
Fabrice

eoztosun
Posts: 5
Joined: Tue 11. Mar 2025, 06:09

Re: Finding Correct PCAN-Basic PCAN handle value for Linux

Post by eoztosun » Tue 25. Mar 2025, 10:51

Thank you for your response, it was very useful.

I solved the issue by adding

Code: Select all

CAN_Uninitialize(PcanHandle);
before

Code: Select all

status = CAN_Initialize(PcanHandle, Bitrate);
within the loop.

But now after a couple plug-ins and plug-outs, I'm having viewtopic.php?f=7&t=7924 issue.

F.Vergnaud
Software Development
Software Development
Posts: 305
Joined: Mon 9. Sep 2013, 12:21

Re: Finding Correct PCAN-Basic PCAN handle value for Linux

Post by F.Vergnaud » Tue 25. Mar 2025, 11:59

Thank you for your feedback, the initial request is closed.

Regarding your second issue, please update the corresponding thread and send a photo of the adapter showing the serial number to our support email address, to see which hardware you are using.
Best regards,
Fabrice

Post Reply