Occasional PCAN_ERROR_INITIALIZE error when writing a message

The free CAN Software API (Application Programming Interface) for Windows®
Locked
KostasN
Posts: 2
Joined: Thu 18. Jul 2024, 12:06

Occasional PCAN_ERROR_INITIALIZE error when writing a message

Post by KostasN » Thu 18. Jul 2024, 12:48

Hello,

We have implemented a cross-platform testing framework in C# ( .NET 6 ) which internally uses PCANBasic.

The source code originally utilized a single instance of a comms object ( in this case a PCAN communication wrapper ) throughout the lifetime of the application to write and read messages to/from a device under test and was considered mature, as it has been in production for more than 1 year without any issues.

For reasons we could not control ( i.e. external requirements ), we recently had to change the architecture a little bit: Instead of having a single object throughout the application's lifetime, the user wishes to test like follows ( pseudo-code ):

Code: Select all

while ( true )
{
    this.pcanDevice = new PCAN();
    this.pcanDevice.Connect( deviceId ); // uses CAN_Initialize internally
    this.pcanDevice.RegisterToRxEvents(); // uses Get_Status or Set_Status depending on OS
    executeTestSequence();
    printTestResult();
    this.pcanDevice.UnegisterFromRxEvents(); // writes 0 to PCAN_RECEIVE_EVENT but is not strictly needed, as CAN_Uninitialize should have the same effect?
    this.pcanDevice.Disconnect(); // uses CAN_Uninitialize internally
    this.pcanDevice.Dispose(); // cleanup of wrapper resources ( loggers, AutoResetEvents etc )
}
Please mind that the only difference between this approach and the old one is the while loop. The code within the while loop itself is identical to the code we were using in the past without issues.

The results we get with the new approach is the following:
- First X tests run OK
- Test X + 1 fails with PCAN_ERROR_INITIALIZE when trying to write a CAN message, after having successfully written multiple CAN messages previously within the same test ( e.g. 20 writes OK, 21st write returns this error )
- Next Y tests run OK
- Test Y + 1 fails with the same issue.
- and so on

Now this is very weird behavior to me, as according to the documentation the error "Indicates that the desired PCAN Channel cannot be connected because it is already in use", where clearly the same application is using the same handle to do all reads and writes. Of course, since the PCANBasic dll has proven very stable I am probably something stupid, but it seems to me that no-one is messing with the channel handle, as it is a private variable.

Is there a way to avoid getting this error? If nothing comes to mind, is there a way to just handle the error and continue writing/reading, without having to terminate my test sequence?

Thank you in advance for your help!

M.Heidemann
Sales & Support
Sales & Support
Posts: 977
Joined: Fri 20. Sep 2019, 13:31

Re: Occasional PCAN_ERROR_INITIALIZE error when writing a message

Post by M.Heidemann » Thu 18. Jul 2024, 15:58

Hello,

PCANBasic can only initialize the same channel once,
which according to your pseudo-code would also be true in your while-loop.

However:

If for some reason you'll exit the loop before the channel is uninitialized, you would get this exact error.

So my first point of investigation is if either "executeTestSequence();" or "printTestResult();" have the ability to break the loop prematurely, the only way they could do that if they raise an exception. If your pseudocode matches your actual production code that's the only possibility that i can come up with on the spot.

If you do break the loop based on the return of those functions that would be an easy explanation.

Also, do you see anything out of the odinary if step through this section of code?

Please let us know.

Best Regards

Marvin
---
Marvin Heidemann
PEAK-Support Team

KostasN
Posts: 2
Joined: Thu 18. Jul 2024, 12:06

Re: Occasional PCAN_ERROR_INITIALIZE error when writing a message

Post by KostasN » Fri 19. Jul 2024, 14:29

Hello again Marvin,

As predicted it was our stupidity.

We had a finalizer in our class which dates back from the days we thought C# finalizers are like C++ destructors.

Removed the finalizer and now works like a charm.

Thank you very much for your support!

M.Heidemann
Sales & Support
Sales & Support
Posts: 977
Joined: Fri 20. Sep 2019, 13:31

Re: Occasional PCAN_ERROR_INITIALIZE error when writing a message

Post by M.Heidemann » Fri 19. Jul 2024, 15:09

Hello Kostas!

Thank you for letting us know!

Best Regards

Marvin

- Topic closed -
---
Marvin Heidemann
PEAK-Support Team

Locked