代码在VB中工作,但在C#中使用ftd2xx.dll

时间:2018-03-01 18:17:44

标签: c# vb.net

确定在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

这在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;
    }

此后此行:

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

在C#VDevice中是空白的

但在VB中

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

它在VDevice中有正确的信息

现在这两个类都使用了 ftd2xx.dll

D2XX驱动程序允许通过DLL直接访问USB设备。应用软件可以通过一系列DLL函数调用来访问USB设备。可用的功能列于D2XX程序员指南文档中,该文档可从本网站的“文档”部分获得。

使用D2XX驱动程序和DLL的编程示例可以在本站点的“项目”部分找到。

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 lngFT_Type As Integer, ByRef lngID As Integer, ByVal pucSerialNumber As String, ByVal pucDescription As String, ByRef pvDummy As Byte) As Integer

更新:

    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
        StringBuilder vDevice = default(StringBuilder); //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 = StringBuilder

                    //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;
    }

此外,

//********************************************************'

    [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);
    private static extern int FT_GetDeviceString(int lngDeviceIndex, StringBuilder lpszDeviceString, int lngFlags);
    [DllImport("FTD2XX.DLL", ExactSpelling=true, CharSet=CharSet.Ansi, SetLastError=true)]

但是, 那没用...... Vdevice还是空白......

0 个答案:

没有答案