Test and Measurement Forum

get_unit_info2 c#

Post your .Net discussions here

get_unit_info2 c#

Postby JacobDixon » Thu May 31, 2012 8:55 am

I cannot seem to get usb_tc08_get_unit_info2 or any of the info functions to work in c#,

I have tried declaring a struct like so:

Code: Select all
public struct usbtc08Info
{
    public short size;
    public char[] DriverVersion;
    public short PicoppVersion;
    public short HardwareVersion;
    public short Variant;
    public char[] szSerial;
    public char[] szCalDate;
}


and passing that to the normal info one,

ive tried passing char arrays and string across to info2 by val and as ref and the closed ive got is getting the first letter of the serial no by passing a char array,

ive even tried the get formatted info but i keep getting too many bits to copy error and every time i put a value in above 255 the application dies.

a short snippit of the code that is important

Code: Select all
[DllImport("usbtc08.dll")]
        public static extern short usb_tc08_open_unit();
        [DllImport("usbtc08.dll")]
        public static extern int usb_tc08_close_unit(short handle);
        [DllImport("usbtc08.dll")]
        public static extern int usb_tc08_set_channel(short handle, int channel, byte tc_type);
        [DllImport("usbtc08.dll")]
        public static extern int usb_tc08_set_mains(short handle, int sixty_herts);
        [DllImport("usbtc08.dll")]
        public static extern long usb_tc08_get_single(short handle, ref Single temp, ref int overflow, int units);
        [DllImport("usbtc08.dll")]
        public static extern int usb_tc08_get_last_error(short handle);
        [DllImport("usbtc08.dll")]
        public static extern long usb_tc08_run(short handle, long interval_ms);
        [DllImport("usbtc08.dll")]
        public static extern long usb_tc08_get_temp(short handel, ref Single temp_buffer, ref long times_ms_buffer, long buffer_length, ref int overflow, int channel, int units, int fill_missing);
        [DllImport("usbtc08.dll")]
        public static extern long usb_tc08_get_minimum_interval_ms(short handle);
        [DllImport("usbtc08.dll")]
        public static extern short usb_tc08_get_unit_info2(short handle, ref char info, int string_length, int line_no);
        [DllImport("usbtc08.dll")]
        public static extern short usb_tc08_get_unit_info2(short handle, ref string info, int string_length, int line_no);
        [DllImport("usbtc08.dll")]
        public static extern short usb_tc08_get_unit_info(short handle, ref usbtc08Info info);
        [DllImport("usbtc08.dll")]
        public static extern short usb_tc08_get_formatted_info(short handle, ref char info, short string_length);

        short tc08Handle;
        char[] tc08SerialNo = new char[256];

        public Menu()
        {
            InitializeComponent();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            tslStatus.Text = "Starting";
            if (tc08Handle < 1)
            {
                tc08Handle = usb_tc08_open_unit();

                if (tc08Handle > 0)
                {
                    try
                    {
                        //usb_tc08_get_unit_info2(tc08Handle, ref tc08SerialNo[0], 11, 4);
                        //string driver = new string(tc08SerialNo);
                        //MessageBox.Show(driver);


                        //string test = new string(tc08SerialNo);
                        //usb_tc08_get_unit_info2(tc08Handle, ref test, 11, 4);
                        //MessageBox.Show(test);

                        //usbtc08Info info = new usbtc08Info();
                        //info.size = sizeof(char) * 45 + sizeof(short) * 4;
                        //usb_tc08_get_unit_info(tc08Handle, ref info);
                        //MessageBox.Show(info.szSerial.ToString());

                        short res = usb_tc08_get_formatted_info(tc08Handle, ref tc08SerialNo[0], (short)512);
                        //string info = new string(tc08SerialNo);
                        MessageBox.Show(res.ToString()); //+ " " + info);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }               


any one got any advice?
Thanks,
Jacob Dixon
JacobDixon
Newbie
 
Posts: 0
Joined: Wed May 30, 2012 8:38 am

Re: get_unit_info2 c#

Postby Martyn » Fri Jun 01, 2012 12:58 pm

Don't have any examples for TC-08 but this is how I handled this for the DrDaq

Code: Select all
      [DllImport(_DRIVER_FILENAME, EntryPoint = "UsbDrDaqGetUnitInfo")]
         public static extern short GetUnitInfo(   
            short   handle,      
            StringBuilder daqstring,
            short   stringLen,
            out short requiredSize,
            Info    daqinfo
      );


            System.Text.StringBuilder line = new System.Text.StringBuilder(80);
            Imports.GetUnitInfo(handleDAQ, line, 80, out requiredSize, Imports.Info.USBDrDAQ_BATCH_AND_SERIAL);

Martyn
Technical Specialist
Martyn
Site Admin
Site Admin
 
Posts: 2287
Joined: Fri Jun 10, 2011 8:15 am
Location: St. Neots

Re: get_unit_info2 c#

Postby JacobDixon » Mon Jun 04, 2012 9:18 am

Thanks that was VERY helpful and works perfectly!

revised code:
Code: Select all
[DllImport("usbtc08.dll")]
        public static extern short usb_tc08_get_unit_info2(short handle, StringBuilder info, int string_length, int line_no);


then implement it:
Code: Select all
StringBuilder serialNo = new StringBuilder(11);
                        usb_tc08_get_unit_info2(tc08Handle, serialNo, 11, 4);


hope this helps some one else too!
JacobDixon
Newbie
 
Posts: 0
Joined: Wed May 30, 2012 8:38 am


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

Who is online

Users browsing this forum: No registered users and 0 guests