Peak PCANBasic.NET Exception

The free CAN Software API (Application Programming Interface) for Windows®
Locked
LEFEVRJK
Posts: 4
Joined: Tue 23. Apr 2024, 02:51

Peak PCANBasic.NET Exception

Post by LEFEVRJK » Tue 7. May 2024, 00:30

I am attempting to initialize one of two Peak CAN USB FD devices. I am using PCANBasic.NET version 4.9.0.942 in a .NET Framework 4.8 WPF application.
I used PCAN View to change the device ID for one device to 0 and the other to 1. So in this case I'm trying to initialize the USB CAN FD with device ID 0.

Here is the problem code:

Code: Select all

public void InitializeCANFDByDeviceID(uint deviceID)
{
            PcanChannel channel = PcanChannel.None;
            PcanDevice deviceType = PcanDevice.PcanUsb;
            string parameters = ParameterValue.LookUp.GetCriteriaString(deviceType, deviceID, null, null);
            PcanStatus result = Api.LookUpChannel(parameters, out channel);
            if (result != PcanStatus.OK)
            {
                Api.GetErrorText(result, out var errorText);
                throw new Exception($"Peak PCAN API Error: {errorText}");
            }
            if (channel == PcanChannel.None)
            {
                throw new Exception($"Peak PCAN API Error: no channel returned for ID {deviceID}");
            }
            FDChannel = (ushort)channel;
            result = Api.Initialize((PcanChannel)FDChannel, (BitrateFD)BitrateFD.BitrateSaeJ2284_4);
            if (result != PcanStatus.OK)
            {
                Api.GetErrorText(result, out var errorText);
                throw new Exception($"Peak PCAN API Initialization Failure: {errorText}{Environment.NewLine}Check that the PCAN is connected.");
            }
            IsInitialized = result == PcanStatus.OK;
            return;
}
The call to GetCriteriaString results in: "devicetype=PcanUsb,deviceid=0"

On the line which calls Api.LookUpChannel, I am getting an exception:
"The type initializer for 'Peak.Can.Basic.Api' threw an exception."

The Peak driver version is 4.4.0.16619, firmware version 3.4.3 (both devices)
Windows 10 Enterprise version 22H2, build 19045.4291

Please let me know what I can look at to troubleshoot the exception. Thank you!

K.Wagner
Software Development
Software Development
Posts: 1080
Joined: Wed 22. Sep 2010, 13:36

Re: Peak PCANBasic.NET Exception

Post by K.Wagner » Tue 7. May 2024, 09:16

Hello,

running your code on our side did not show any problem. The only exception we see is your own, if there is no USB with device ID 0.
LEFEVRJK wrote:
Tue 7. May 2024, 00:30
"The type initializer for 'Peak.Can.Basic.Api' threw an exception."
"The type initializer ... threw an exception" just mean an error occurred inside of the assmebly. For instance, if you wouldn't have PCANBasic.dll installed, you would see the same kind of exception.

Please show the whole exception text i.e. the inner exception, so we can check what is happening on your side.
Best regards,
Keneth

LEFEVRJK
Posts: 4
Joined: Tue 23. Apr 2024, 02:51

Re: Peak PCANBasic.NET Exception

Post by LEFEVRJK » Tue 7. May 2024, 21:46

Thank you for your response! I did not think to check the inner exception as the exception message displays in a pop-up message. Once I stepped through, the inner exception message told me exactly what the issue was.
I updated the NuGet package because the one associated to my project was depricated.

However, that did not update the PCANBasic.dll. In order to update the PCANBasic.dll, I needed to update the driver. I updated the driver, which eliminated the issue.

Thank you for pointing me in the correct direction!

K.Wagner
Software Development
Software Development
Posts: 1080
Joined: Wed 22. Sep 2010, 13:36

Re: Peak PCANBasic.NET Exception

Post by K.Wagner » Wed 8. May 2024, 08:46

Hello,

glad to see that the problem is solved.
LEFEVRJK wrote:
Tue 7. May 2024, 21:46
However, that did not update the PCANBasic.dll. In order to update the PCANBasic.dll, I needed to update the driver. I updated the driver, which eliminated the issue.
The NuGet package doesn't install the native library. We communicate this also in the documentation:
Note.png
Note.png (33.63 KiB) Viewed 2238 times
Closed.
Best regards,
Keneth

Locked