Test and Measurement Forum

c# and 2204 and ps2000wrap.dll

Post your .Net discussions here

c# and 2204 and ps2000wrap.dll

Postby mrlucretius » Fri Mar 23, 2012 7:24 pm

Hello,

Can anyone point me to an example of how to use ps2000wrap.dll with c# and the picoscope2204?

I can't seem to figure it out. So far, I can talk to the dll's, but I do not understand how to get the callback implemented right so that I may use ps2000_get_streaming_last_values...

Thanks,
Alex
mrlucretius
Newbie
 
Posts: 0
Joined: Fri Mar 02, 2012 5:54 pm

Re: c# and 2204 and ps2000wrap.dll

Postby Martyn » Mon Mar 26, 2012 10:58 am

We don't have any c# examples for the 2000 series scopes but if you would like to post your code here, or send it to us at support@picotech.com, we can advise you on this.
Martyn
Technical Specialist
Martyn
Site Admin
Site Admin
 
Posts: 2351
Joined: Fri Jun 10, 2011 8:15 am
Location: St. Neots

Re: c# and 2204 and ps2000wrap.dll

Postby mrlucretius » Mon Mar 26, 2012 4:30 pm

So I am trying to create a buffer in csharp, pin it in memory, and pass the reference to the buffer using the SetBuffer dll export. All of this compiles fine, but complains of am unbalanced stack when it is run.
VS2010 Error:
############
A call to PInvoke function 'PS2000CSConsole!PS2000CSConsole.Imports::SetBuffer' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
#############
Here is the ps2000wrap.dll method (picotech code ps2000wrap.c):
####################################
extern void __declspec(dllexport) __stdcall SetBuffer(short handle, short channel, short * buffer, unsigned long bufferSize)
{
short index = channel * 2;
if(index >=8) return;

_buffers[index] = _buffers[index + 1] = NULL;
_bufferSizes[index] = bufferSize;

if(buffer != NULL) _buffers[index] = buffer;
}
##################################
Here is the imports definition (very similar to the ps3000 csharp example):
##################################
[DllImport(_PS2000wrap_DRIVER_FILENAME, EntryPoint = "SetBuffer")]
unsafe public static extern void SetBuffer(
short handle,
short channel,
short* buffer,
ulong bufferSize);
##################################
And here I am trying to set the buffer (after starting the streaming):
##################################
ulong sampleCount = 1000;

short[] bufferArray = new short[sampleCount];
var bufferArrayHandle = GCHandle.Alloc(bufferArray, GCHandleType.Pinned);
IntPtr bufferArrayAddress = bufferArrayHandle.AddrOfPinnedObject();

unsafe
{
Imports.SetBuffer(handle, (short)Imports.Channel.ChannelA, (short*)bufferArrayAddress.ToPointer(), sampleCount);
}
##################################

I could be going about this in the wrong way. Though this currently makes sense (pin a buffer in memory, pass the reference to the dll).

Can you provide some guidance here?

Thanks,
Alex Martin
mrlucretius
Newbie
 
Posts: 0
Joined: Fri Mar 02, 2012 5:54 pm

Re: c# and 2204 and ps2000wrap.dll

Postby mrlucretius » Mon Mar 26, 2012 8:28 pm

I think I figured it out. It only took 3 days.

The problem is that in the dll ps2000wrap.dll the method is exported like so:

extern void __declspec(dllexport) __stdcall SetBuffer(short handle, short channel, short * buffer, unsigned long bufferSize)

But the type unsigned long is different than the csharp type... Csharp notice is a uint...

Here is the correct import of the dll function above:

[DllImport(_PS2000wrap_DRIVER_FILENAME, EntryPoint = "SetBuffer")]
public static extern void SetBuffer(
short handle,
short channel,
short[] buffer,
uint bufferSize);

And here is the implementation in csharp:

short[] bufferArray = new short[sampleCount];
Imports.SetBuffer(handle, (short)Imports.Channel.ChannelA, bufferArray, sampleCount);

That seems to be it. I hope I am not jumping the gun here but I think that was my issue... It appears that the bufferArray has actual sampled scope data in it now.

Thanks,
Alex
mrlucretius
Newbie
 
Posts: 0
Joined: Fri Mar 02, 2012 5:54 pm

Re: c# and 2204 and ps2000wrap.dll

Postby Martyn » Tue Mar 27, 2012 8:38 am

Thank you for the update.
Martyn
Technical Specialist
Martyn
Site Admin
Site Admin
 
Posts: 2351
Joined: Fri Jun 10, 2011 8:15 am
Location: St. Neots


Return to .Net Languages (C#, VB.Net, .)

Who is online

Users browsing this forum: No registered users and 0 guests