UsbAdc11OpenUnit(); wrong value was give

Post your .Net discussions here
Post Reply
Seyi
Newbie
Posts: 1
Joined: Wed Sep 23, 2009 2:17 pm
Location: nigeria
Contact:

UsbAdc11OpenUnit(); wrong value was give

Post by Seyi »

using System.Runtime.InteropServices;
.
.
.
[DllImport("usbadc11.dll")]
public static extern Boolean UsbAdc11OpenUnit();
.
.
.

why should this code always return true even if the Usb adc11 is not connected.
private void button2_Click(object sender, EventArgs e)


{
try
{
MessageBox.Show(UsbAdc11OpenUnit().ToString());
}
catch (Exception )
{
MessageBox.Show("Device not connected");
}
}

when UsbAdc11 is connected it gives a red indicator

......................................................................................
also,

[DllImport("ADC1132.dll")]
public static extern short adc11_get_driver_version ();

private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(adc11_get_driver_version().ToString());

}

returns 1026 instead of 1023 is stated in the SDK, any explanation for this?
....................................................................................
public static extern uint UsbAdc11MaxValue(short handle, ref uint maxium );

[DllImport("usbadc11.dll")]
private void button4_Click(object sender, EventArgs e)
{
try
{
//int max;
// uint u=0;
// max = UsbAdc11MaxValue(101,ref u );

//StringBuilder shortpath = new StringBuilder (5000) ;

//int max = UsbAdc11MaxValue(101, shortpath,5000,u );
//string s = shortpath.ToString();
uint m = 0;
m = UsbAdc11MaxValue(101, ref m);

MessageBox.Show(m.ToString());

}
catch (Exception err)
{
MessageBox.Show(err.ToString());
}
}

give outrageous value 1179648 instead of 1023 or 4095
how can i also collect data without using the trigger fuction as stated by the routines.


How can is use this in my C# programming while usiong the
InteropServices service for each.

typedef enum enAdc11Inputs
{
ADC11_CHANNEL_1 = 1,
ADC11_CHANNEL_2,
ADC11_CHANNEL_3,
ADC11_CHANNEL_4,
ADC11_CHANNEL_5,
ADC11_CHANNEL_6,
ADC11_CHANNEL_7,
ADC11_CHANNEL_8,
ADC11_CHANNEL_9,
ADC11_CHANNEL_10,
ADC11_CHANNEL_11,
ADC11_MAX_CHANNELS = ADC11_CHANNEL_11
} ADC11_INPUTS;

typedef enum enAdc11Info
{
USBADC11_DRIVER_VERSION,
USBADC11_USB_VERSION,
USBADC11_HARDWARE_VERSION,
USBADC11_VARIANT_INFO,
USBADC11_BATCH_AND_SERIAL,
USBADC11_CAL_DATE,
USBADC11_KERNEL_DRIVER_VERSION,
USBADC11_ERROR,
USBADC11_SETTINGS,
} USBADC11_INFO;

typedef enum enAdc11ErrorCode
{
ADC11_OK,
ADC11_KERNEL_DRIVER,
ADC11_NOT_FOUND,
ADC11_CONFIG_FAIL,
ADC11_FW_FAIL,
ADC11_ERROR_OS_NOT_SUPPORTED,
ADC11_MAX_DEVICES
} ADC11_ERROR_CODES;


typedef enum enAdc11SettingsError
{
SE_CONVERSION_TIME_OUT_OF_RANGE,
SE_SAMPLEINTERVAL_OUT_OF_RANGE,
SE_CONVERTION_TIME_TO_SLOW,
SE_CHANNEL_NOT_AVAILABLE,
SE_INVALID_CHANNEL,
SE_INVALID_VOLTAGE_RANGE,
SE_INVALID_PARAMETER,
SE_CONVERSION_IN_PROGRESS,
SE_OK,
SE_MAX = SE_OK
} ADC11_SETTINGS_ERROR;

typedef enum enAdc11OpenProgress
{
USBADC11_OPEN_PROGRESS_FAIL = -1,
USBADC11_OPEN_PROGRESS_PENDING = 0,
USBADC11_OPEN_PROGRESS_COMPLETE = 1
} USBADC11_OPEN_PROGRESS;


when used like this it state no entry point

enum enAdc11Info
{
USBADC11_DRIVER_VERSION,
USBADC11_USB_VERSION,
USBADC11_HARDWARE_VERSION,
USBADC11_VARIANT_INFO,
USBADC11_BATCH_AND_SERIAL,
USBADC11_CAL_DATE,
USBADC11_KERNEL_DRIVER_VERSION,
USBADC11_ERROR,
USBADC11_SETTINGS,


}

enum ADC11_INPUTS
{
ADC11_CHANNEL_1 = 1,
ADC11_CHANNEL_2,
ADC11_CHANNEL_3,
ADC11_CHANNEL_4,
ADC11_CHANNEL_5,
ADC11_CHANNEL_6,
ADC11_CHANNEL_7,
ADC11_CHANNEL_8,
ADC11_CHANNEL_9,
ADC11_CHANNEL_10,
ADC11_CHANNEL_11,
ADC11_MAX_CHANNELS = ADC11_CHANNEL_11
}
programming

markB
Site Admin
Site Admin
Posts: 83
Joined: Tue Mar 27, 2007 9:43 am
Location: Cambridgeshire,UK

Re: UsbAdc11OpenUnit(); wrong value was give

Post by markB »

[DllImport("usbadc11.dll")]
public static extern Boolean UsbAdc11OpenUnit();

why should this code always return true even if the Usb adc11 is not connected.

Firstly, the UsbAdc11OpenUnit() function returns a handle not a boolean. The handle will be zero if nothing is connected.

Secondly, there is some wierdness when marshalling bools because there is no standard native boolean. Try using something like the following when attempting to marshal a boolean:

Code: Select all

  public struct BOOL
  {
    private short _boolean;

    public static implicit operator bool(BOOL from)
    {
      return Convert.ToBoolean(from._boolean);
    }
  }
[DllImport("ADC1132.dll")]
public static extern short adc11_get_driver_version ();
...
returns 1026 instead of 1023 is stated in the SDK, any explanation for this?
You have a later version of the dll!
public static extern uint UsbAdc11MaxValue(short handle, ref uint maxium );

[DllImport("usbadc11.dll")]

give outrageous value 1179648 instead of 1023 or 4095
how can i also collect data without using the trigger fuction as stated by the routines.
You have an incorrect declaration, try:

Code: Select all

    [DllImport("usbadc11.dll")]
    public static extern short UsbAdc11OpenUnit();

    [DllImport("usbadc11.dll")]
    public static extern BOOL UsbAdc11MaxValue(short handle, out ushort max);     

... 
      short handle = USBADC11.UsbAdc11OpenUnit();

      if(handle > 0)
      {
        // Q3
        ushort maxValue;
        BOOL success = USBADC11.UsbAdc11MaxValue(handle, out maxValue);
        
        Console.Out.WriteLine(success ? string.Format("maxValue = {0}", maxValue) : "error");
      }
How can is use this in my C# programming while usiong the
InteropServices service for each.

Code: Select all

        

    public enum enAdc11Info
    {
      USBADC11_DRIVER_VERSION,
      USBADC11_USB_VERSION,
      USBADC11_HARDWARE_VERSION,
      USBADC11_VARIANT_INFO,
      USBADC11_BATCH_AND_SERIAL,
      USBADC11_CAL_DATE,
      USBADC11_KERNEL_DRIVER_VERSION,
      USBADC11_ERROR,
      USBADC11_SETTINGS,
    }

    [DllImport("usbadc11.dll")]
    public static extern BOOL UsbAdc11GetUnitInfo(short handle, StringBuilder str, short stringLength, enAdc11Info info);


...

StringBuilder sb = new StringBuilder(100);

        success = USBADC11.UsbAdc11GetUnitInfo(handle, sb, 100, USBADC11.enAdc11Info.USBADC11_BATCH_AND_SERIAL);

        Console.Out.WriteLine(success ? string.Format("info = {0}", sb) : "error");

        USBADC11.UsbAdc11CloseUnit(handle);
Regards

Mark

Post Reply