Test and Measurement Forum

Getting a list of available devices

Post your .Net discussions here

Getting a list of available devices

Postby Simon » Tue Aug 04, 2015 2:23 pm

Hello

I'm trying to get a list of all available devices connected to my computer. Currently I am only intrested in PS5000 devices but this may change in the future to include other ranges.

Is there an easy way to do this? I have found the ps5000aEnumerateUnits command in the programmers manual, this command however does not seem to be included in the PS500AImports.cs file. I have tried adding the following to the file

Code: Select all
[DllImport(_DRIVER_FILENAME, EntryPoint = "ps5000aEnumerateUnits")]
public static extern DataAqu_PicoScope.PicoStatus EnumerateUnits(out short Count, out StringBuilder SerialCSV, out short SerialCSV_Length);


and then calling it with the following

Code: Select all
short deviceCount;
StringBuilder serialCSV = new StringBuilder();
short stringLength;
MyImports.EnumerateUnits(out deviceCount, out  serialCSV, out  stringLength);


I'm getting an access violation exception when calling EnumerateUnits

Thanks for any help

Simon
Last edited by Simon on Wed Aug 05, 2015 1:58 pm, edited 1 time in total.
Simon
Newbie
 
Posts: 0
Joined: Fri Jun 01, 2012 10:26 am

Re: Getting a list of available devices

Postby Hitesh » Wed Aug 05, 2015 1:35 pm

Hi Simon,

Try allocating some memory for your string e.g. create a string of 40 characters and set the length parameter accordingly.

The crash is probably occurring as the driver is not able to write the information anywhere.

Hope this helps.
Hitesh

Technical Specialist
Pico Technology
Hitesh
Site Admin
Site Admin
 
Posts: 2058
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire

Re: Getting a list of available devices

Postby Simon » Wed Aug 05, 2015 1:56 pm

ok so I have now made a few changes to my code, see below.

Code: Select all
[DllImport(_DRIVER_FILENAME, EntryPoint = "ps5000aEnumerateUnits")]
public static extern DataAqu_PicoScope.PicoStatus EnumerateUnits(out short Count, out StringBuilder SerialCSV, ref short SerialCSV_Length);


Code: Select all
//Test Code
            short deviceCount;
            StringBuilder serialCSV = new StringBuilder(40);
            short stringLength = 40;
            MyImports.EnumerateUnits(out deviceCount,out serialCSV, ref stringLength);
//Test Code End


This still brings up the same errors.

Simon
Simon
Newbie
 
Posts: 0
Joined: Fri Jun 01, 2012 10:26 am

Re: Getting a list of available devices

Postby Hitesh » Thu Aug 06, 2015 1:13 pm

Hi Simon,

Just ran some tests here - the StringBuilder object needs to be passed as is once you have allocated a size i.e. without an 'out' or a 'ref' keyword prefix.

Here's the declaration to use:

Code: Select all
[DllImport(_DRIVER_FILENAME, EntryPoint = "ps5000aEnumerateUnits")]
        public static extern uint EnumerateUnits(
                                                    out short count,
                                                    StringBuilder serials,
                                                    ref short serialLength);


Below is some of the code used to test it with:

Code: Select all
short count = 0;
short serialsLength = 40;
StringBuilder serials = new StringBuilder(serialsLength);

uint status = Imports.EnumerateUnits(out count, serials, ref serialsLength);

if (status != Imports.PICO_OK)
{
  Console.WriteLine("No devices found.\n");
  Console.WriteLine("Error code : {0}", status);
  Console.WriteLine("Press any key to exit.\n");
  WaitForKey();
  Environment.Exit(0);
}
else
{
   if(count == 1)
   {
     Console.WriteLine("Found {0} device:", count);
   }
   else
   {
     Console.WriteLine("Found {0} devices", count);
   }

    Console.WriteLine("Serial(s) {0}", serials);
}


This function definition will be in the imports file in a future SDK release.

Regards,
Hitesh

Technical Specialist
Pico Technology
Hitesh
Site Admin
Site Admin
 
Posts: 2058
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire

Re: Getting a list of available devices

Postby Simon » Fri Aug 07, 2015 10:17 am

Thanks for that, I now have it running perfectly in my app

Simon
Simon
Newbie
 
Posts: 0
Joined: Fri Jun 01, 2012 10:26 am


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

Who is online

Users browsing this forum: No registered users and 0 guests