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 )
}
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!