UDS sample apps - converted to C#

A free API for the communication with control devices according to UDS (ISO 14229-1)
Post Reply
evilpaul
Posts: 3
Joined: Sat 29. Jun 2019, 16:07

UDS sample apps - converted to C#

Post by evilpaul » Tue 8. Oct 2019, 13:28

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

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

Re: UDS sample apps - converted to C#

Post by K.Wagner » Tue 8. Oct 2019, 15:54

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).
Best regards,
Keneth

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

Re: UDS sample apps - converted to C#

Post by K.Wagner » Wed 9. Oct 2019, 14:27

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.
Best regards,
Keneth

evilpaul
Posts: 3
Joined: Sat 29. Jun 2019, 16:07

Re: UDS sample apps - converted to C#

Post by evilpaul » Wed 9. Oct 2019, 15:06

Hi Keneth,

Thanks for the information, I have committed the change to the PCUClient repo.

regards,
Paul

Post Reply