locked
code that works in VB but not in C# using ftd2xx.dll RRS feed

  • Question

  • ok  have this in VB:

     
     Public Function xcCommGetDevicesList() As List(Of String)
        '********************************************************'
        '** Load USB Devices to the Combo Box
        '** Gets a list of all available MXDPP's connected on the computer and 
        '** populate them in a list of strings which is returned
        '********************************************************'
    
        Dim vDeviceList As New List(Of String)  'Stores list of devices found
        Dim vDeviceCount As Integer             'Stores the number of devices attached
        Dim vDevice As String                   'Stores the name of the device  
        Dim vIndex As Integer                   'Index of device request
    
        Try
            'Request the number of FTDI devices attached to computer
            vFT_Status = FT_GetNumberOfDevices(vDeviceCount, vbNullChar, cFT_LIST_NUMBER_ONLY)
    
            'Check if the return value was OK
            If vFT_Status = cFT_OK Then
    
                'Loop through each device attached and add to cmbDevices if it is an MXDPP
                For vIndex = 0 To vDeviceCount - 1
    
                    'Allocate space in the string variable (It doesn't work if you don't do this)
                    vDevice = Space(64)
    
                    'Request the description of the device at the specified vIndex
                    vFT_Status = FT_GetDeviceString(vIndex, vDevice, cFT_LIST_BY_INDEX Or cFT_OPEN_BY_DESCRIPTION)
    
                    'Check if the return value was OK
                    If vFT_Status = cFT_OK Then
    
                        'Remove null character from description
                        vDevice = Microsoft.VisualBasic.Left(vDevice, InStr(1, vDevice, vbNullChar) - 1)
    
                        'Determine if device is and MXDPP-50 add it to cmbDevices if it is
                        If vDevice.Contains("MXDPP-50") Then
    
                            'Allocate space in the string variable (It doesn't work if you don't do this)
                            vDevice = Space(16)
    
                            'Request the name of the device at the specified vIndex
                            vFT_Status = FT_GetDeviceString(vIndex, vDevice, cFT_LIST_BY_INDEX Or cFT_OPEN_BY_SERIAL_NUMBER)
    
                            'Check if the return value was OK
                            If vFT_Status = cFT_OK Then
                                'Remove null character and save name to Device array 
                                vDeviceList.Add(Microsoft.VisualBasic.Left(vDevice, InStr(1, vDevice, vbNullChar) - 1))
                            End If
                        End If
                    End If
                Next
            End If
        Catch ex As Exception
    
        End Try
    
    
        'Return vDeviceList to calling function
        Return vDeviceList
    End Function

    and this in C#

       
     public static List<string> DPP_GetDeviceList()
        {
            //********************************************************'
            //** Get Device List
            //** Gets a list of all available MXDPP's connected on the computer and
            //** populate them in a list of strings which is returned
            //********************************************************'
    
            List<string> vDeviceList = new List<string>(); //Stores list of devices found
            int vDeviceCount = default(int); //Stores the number of devices attached
            string vDevice = default(string); //Stores the name of the device
            int vIndex = default(int); //Index of device request
    
            try
            {
                //Request the number of FTDI devices attached to computer
                vFT_Status = System.Convert.ToInt32(FT_GetNumberOfDevices(ref vDeviceCount, Constants.vbNullChar, cFT_LIST_NUMBER_ONLY));
    
                //Check if the return value was OK
                if (vFT_Status == cFT_OK)
                {
    
                    //Loop through each device attached and add to List if it is an MXDPP
                    for (vIndex = 0; vIndex <= vDeviceCount - 1; vIndex++)
                    {
    
                        //Allocate space in the string variable (It doesn't work if you don't do this)
                        vDevice = Strings.Space(64);
    
                        //Request the description of the device at the specified vIndex
                        //vFT_Status = System.Convert.ToInt32(FT_GetDeviceString(vIndex, vDevice, cFT_LIST_BY_INDEX | cFT_OPEN_BY_DESCRIPTION));
                        vFT_Status = System.Convert.ToInt32(FT_GetDeviceString(vIndex, vDevice, cFT_LIST_BY_INDEX | cFT_OPEN_BY_SERIAL_NUMBER));
    
                        //Check if the return value was OK
                        if (vFT_Status == cFT_OK)
                        {
    
                            //Remove null character from description
                            vDevice = Microsoft.VisualBasic.Strings.Left(vDevice, vDevice.IndexOf(Constants.vbNullChar) + 0);
    
                            //Determine if device is and MXDPP-50 add it to list if it is
                            if (vDevice.Contains("MXDPP-50"))
                            {
    
                                //Allocate space in the string variable (It doesn't work if you don't do this)
                                vDevice = Strings.Space(16);
    
                                //Request the name of the device at the specified vIndex
                                vFT_Status = System.Convert.ToInt32(FT_GetDeviceString(vIndex, vDevice, cFT_LIST_BY_INDEX | cFT_OPEN_BY_SERIAL_NUMBER));
    
                                //Check if the return value was OK
                                if (vFT_Status == cFT_OK)
                                {
                                    //Remove null character and save name to Device array
                                    vDeviceList.Add(Microsoft.VisualBasic.Strings.Left(vDevice, vDevice.IndexOf(Constants.vbNullChar) + 0));
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception Err)
            {
                //Handle Errors Here
            }
    
            //Return vDeviceList to calling function
            return vDeviceList;
        }

    now after this this line :

     vFT_Status = System.Convert.ToInt32(FT_GetDeviceString(vIndex, vDevice, cFT_LIST_BY_INDEX | cFT_OPEN_BY_SERIAL_NUMBER));

    in C# VDevice is blank

    but in VB

     vFT_Status = FT_GetDeviceString(vIndex, vDevice, cFT_LIST_BY_INDEX Or cFT_OPEN_BY_SERIAL_NUMBER)

    it have the right information in VDevice

    now both classes uses  ftd2xx.dll

    D2XX drivers allow direct access to the USB device through a DLL.  Application software can access the USB device through a series of DLL function calls.  The functions available are listed in the  D2XX Programmer's Guide document which is available from the Documents section of this site. 

    Programming examples using the D2XX drivers and DLL can be found in the Projects section of this site.

    C#

    [DllImport("FTD2XX.DLL",EntryPoint="FT_ListDevices", ExactSpelling=true, CharSet=CharSet.Ansi, SetLastError=true)]
        private static extern int FT_GetNumberOfDevices(ref int lngNumberOfDevices, string pvArg2, int lngFlags);
        [DllImport("FTD2XX.DLL",EntryPoint="FT_ListDevices", ExactSpelling=true, CharSet=CharSet.Ansi, SetLastError=true)]
        private static extern int FT_GetDeviceString(int lngDeviceIndex, string lpszDeviceString, int lngFlags);
        [DllImport("FTD2XX.DLL", ExactSpelling=true, CharSet=CharSet.Ansi, SetLastError=true)]

    VB

     
    Private Declare Function FT_GetNumberOfDevices Lib "FTD2XX.DLL" Alias "FT_ListDevices" (ByRef lngNumberOfDevices As Integer, ByVal pvArg2 As String, ByVal lngFlags As Integer) As Integer
    Private Declare Function FT_GetDeviceString Lib "FTD2XX.DLL" Alias "FT_ListDevices" (ByVal lngDeviceIndex As Integer, ByVal lpszDeviceString As String, ByVal lngFlags As Integer) As Integer
    Private Declare Function FT_GetDeviceInfo Lib "FTD2XX.DLL" (ByVal lngHandle As Integer, ByRef 
    


    • Edited by Btb4198 Thursday, March 1, 2018 8:50 PM
    • Moved by Fei Hu Friday, March 2, 2018 5:35 AM Moved from C#
    Thursday, March 1, 2018 8:47 PM

All replies

  • Looks good. You can do that, you don't have to ask permission.

    Let us know if you have a question, these forums are for answering questions.



    Sam Hobbs
    SimpleSamples.Info

    Friday, March 2, 2018 4:24 AM
  • Hi Btb4198,

    You could repost a new thread to their forum for professional support and better solution based on your question is more related to third party libraries.

    If you have some grammar or code errors in using C#, please feel free to contact us. We will try our best to give you a solution.

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 2, 2018 5:34 AM
  • it is a c# question ...can someone here help me?

    Friday, March 2, 2018 1:21 PM