Page 1 of 1
UDS sample apps - converted to C#
Posted: Tue 8. Oct 2019, 13:28
by evilpaul
Hi everyone,
I have converted the C++ sample applications from the PCAN-UDS package into C#:
PCUServer :
https://github.com/Evilpaul/PCUServer
PCUClient :
https://github.com/Evilpaul/PCUClient
I have done a quick comparison using combinations of the C++ and C# applications and the behaviour appears to be the same.
The one difference is that I have not been able to get the testUsingEvent function to work due to getting an AccessViolationException when passing a handle to UDSApi.SetValue, so this is disabled for the moment.
Hope someone finds these useful.
regards,
Paul
Re: UDS sample apps - converted to C#
Posted: Tue 8. Oct 2019, 15:54
by K.Wagner
Hello,
thank you for your interest and your effort in porting the sample projects to C# and for sharing them to the community.
The problem seems to be in the overloading of UDS_SetValue for an IntPtr buffer.
It doesn't use the "ref" keyword. Try declaring it in this way:
Code: Select all
[DllImport("PCAN-UDS.dll", EntryPoint = "UDS_SetValue")]
public static extern TPUDSStatus SetValue(
[MarshalAs(UnmanagedType.U2)]
TPUDSCANHandle CanChannel,
[MarshalAs(UnmanagedType.U1)]
TPUDSParameter Parameter,
ref IntPtr Buffer,
UInt32 BufferLength);
We will need to adjust the header files. Thanks for bringing this to our attention.
EDIT: The events should be passed as an integer value, as done in PCAN-Basic (note this api use PCAN-Basic internally).
Re: UDS sample apps - converted to C#
Posted: Wed 9. Oct 2019, 14:27
by K.Wagner
Hello again,
after checking this deeper, it is now clear that the overload:
Code: Select all
SetValue(TPUDSCANHandle,TPUDSParameter, IntPtr, UInt32)
is not intended to be used with AutoreceiveEvent or ManualresetEvent. It is intended to be used with the parameter PUDS_PARAM_MAPPING_ADD.
The Events are to be set using the overload version for numeric values of SetValue,
Code: Select all
SetValue(TPUDSCANHandle, TPUDSParameter, ref UInt32, UInt32)
The way how you do this is as follow:
Code: Select all
TPUDSStatus status;
ManualResetEvent hEvent;
uint myEventAsInt;
hEvent = new ManualResetEvent(false);
myEventAsInt = (uint)hEvent.SafeWaitHandle.DangerousGetHandle().ToInt32();
status = SetValue(CanChannel, Parameter, ref myEventAsInt, sizeof(uint));
We will add a note in the documentation.
Re: UDS sample apps - converted to C#
Posted: Wed 9. Oct 2019, 15:06
by evilpaul
Hi Keneth,
Thanks for the information, I have committed the change to the PCUClient repo.
regards,
Paul