none
code works in one soluation but not in the other RRS feed

  • Question

  • ok I download some sample code and it works. now I am trying to get my code to do what the sample code it doing. I copy and past the code but it is not working for me. I get a error during runtime.

    here is the code I downloaded:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;
    
    using FTD2XX_NET;
    
    namespace LoopBack
    {
        class Program
        {
            static void Main(string[] args)
            {
                UInt32 ftdiDeviceCount = 0;
                FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK;
    
                // Create new instance of the FTDI device class
                FTDI myFtdiDevice = new FTDI();
                
                // Determine the number of FTDI devices connected to the machine
                ftStatus = myFtdiDevice.GetNumberOfDevices(ref ftdiDeviceCount);
                // Check status
                if (ftStatus == FTDI.FT_STATUS.FT_OK)
                {
                    Console.WriteLine("Number of FTDI devices: " + ftdiDeviceCount.ToString());
                    Console.WriteLine("");
                }
                else
                {
                    // Wait for a key press
                    Console.WriteLine("Failed to get number of devices (error " + ftStatus.ToString() + ")");
                    Console.ReadKey();
                    return;
                }
    
                // If no devices available, return
                if (ftdiDeviceCount == 0)
                {
                    // Wait for a key press
                    Console.WriteLine("Failed to get number of devices (error " + ftStatus.ToString() + ")");
                    Console.ReadKey();
                    return;
                }
    
                // Allocate storage for device info list
                FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[ftdiDeviceCount];
    
                // Populate our device list
                ftStatus = myFtdiDevice.GetDeviceList(ftdiDeviceList);
    
                if (ftStatus == FTDI.FT_STATUS.FT_OK)
                {
                    for (UInt32 i = 0; i < ftdiDeviceCount; i++)
                    {
                        Console.WriteLine("Device Index: " + i.ToString());
                        Console.WriteLine("Flags: " + String.Format("{0:x}", ftdiDeviceList[i].Flags));
                        Console.WriteLine("Type: " + ftdiDeviceList[i].Type.ToString());
                        Console.WriteLine("ID: " + String.Format("{0:x}", ftdiDeviceList[i].ID));
                        Console.WriteLine("Location ID: " + String.Format("{0:x}", ftdiDeviceList[i].LocId));
                        Console.WriteLine("Serial Number: " + ftdiDeviceList[i].SerialNumber.ToString());
                        Console.WriteLine("Description: " + ftdiDeviceList[i].Description.ToString());
                        Console.WriteLine("");
                    }
                }
    
    
                // Open first device in our list by serial number
                ftStatus = myFtdiDevice.OpenBySerialNumber(ftdiDeviceList[0].SerialNumber);
                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    // Wait for a key press
                    Console.WriteLine("Failed to open device (error " + ftStatus.ToString() + ")");
                    Console.ReadKey();
                    return;
                }
    
                // Set up device data parameters
                // Set Baud rate to 9600
                ftStatus = myFtdiDevice.SetBaudRate(9600);
                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    // Wait for a key press
                    Console.WriteLine("Failed to set Baud rate (error " + ftStatus.ToString() + ")");
                    Console.ReadKey();
                    return;
                }
    
                // Set data characteristics - Data bits, Stop bits, Parity
                ftStatus = myFtdiDevice.SetDataCharacteristics(FTDI.FT_DATA_BITS.FT_BITS_8, FTDI.FT_STOP_BITS.FT_STOP_BITS_1, FTDI.FT_PARITY.FT_PARITY_NONE);
                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    // Wait for a key press
                    Console.WriteLine("Failed to set data characteristics (error " + ftStatus.ToString() + ")");
                    Console.ReadKey();
                    return;
                }
    
                // Set flow control - set RTS/CTS flow control
                ftStatus = myFtdiDevice.SetFlowControl(FTDI.FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, 0x11, 0x13);
                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    // Wait for a key press
                    Console.WriteLine("Failed to set flow control (error " + ftStatus.ToString() + ")");
                    Console.ReadKey();
                    return;
                }
    
                // Set read timeout to 5 seconds, write timeout to infinite
                ftStatus = myFtdiDevice.SetTimeouts(5000, 0);
                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    // Wait for a key press
                    Console.WriteLine("Failed to set timeouts (error " + ftStatus.ToString() + ")");
                    Console.ReadKey();
                    return;
                }
    
                // Perform loop back - make sure loop back connector is fitted to the device
                // Write string data to the device
                string dataToWrite = "Hello world!";
                UInt32 numBytesWritten = 0;
                // Note that the Write method is overloaded, so can write string or byte array data
                ftStatus = myFtdiDevice.Write(dataToWrite, dataToWrite.Length, ref numBytesWritten);
                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    // Wait for a key press
                    Console.WriteLine("Failed to write to device (error " + ftStatus.ToString() + ")");
                    Console.ReadKey();
                    return;
                }
    
    
                // Check the amount of data available to read
                // In this case we know how much data we are expecting, 
                // so wait until we have all of the bytes we have sent.
                UInt32 numBytesAvailable = 0;
                do
                {
                    ftStatus = myFtdiDevice.GetRxBytesAvailable(ref numBytesAvailable);
                    if (ftStatus != FTDI.FT_STATUS.FT_OK)
                    {
                        // Wait for a key press
                        Console.WriteLine("Failed to get number of bytes available to read (error " + ftStatus.ToString() + ")");
                        Console.ReadKey();
                        return;
                    }
                    Thread.Sleep(10);
                } while (numBytesAvailable < dataToWrite.Length);
    
                // Now that we have the amount of data we want available, read it
                string readData;
                UInt32 numBytesRead = 0;
                // Note that the Read method is overloaded, so can read string or byte array data
                ftStatus = myFtdiDevice.Read(out readData, numBytesAvailable, ref numBytesRead);
                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    // Wait for a key press
                    Console.WriteLine("Failed to read data (error " + ftStatus.ToString() + ")");
                    Console.ReadKey();
                    return;
                }
                Console.WriteLine(readData);
    
                // Close our device
                ftStatus = myFtdiDevice.Close();
    
                // Wait for a key press
                Console.WriteLine("Press any key to continue.");
                Console.ReadKey();
                return;
            }
        }
    }
    

    not there program is a console and my program is a winform but that should not matter...

    I have this function  :

     private void LoadDevices()
            {
                //********************************************************'
                //** Loads all device found into device combo box
                //********************************************************'
    
                 UInt32 ftdiDeviceCount = 0;
            FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK;
    
            // Create new instance of the FTDI device class
            FTDI myFtdiDevice = new FTDI();
    
               // List<string> vDeviceList = default(List<string>); //Contains a list of devices found
                string vDevice = default(string); //Single Device name
                // Determine the number of FTDI devices connected to the machine
                ftStatus = myFtdiDevice.GetNumberOfDevices(ref ftdiDeviceCount);
                // Check status
                if (ftStatus == FTDI.FT_STATUS.FT_OK)
                {
                    AppendTextBox("Number of FTDI devices: " + ftdiDeviceCount.ToString() + Environment.NewLine);
                   
                }
                else
                {
                    // Wait for a key press
                    AppendTextBox("Failed to get number of devices (error " + ftStatus.ToString() + ")");
                    return;
                }
    
                // If no devices available, return
                if (ftdiDeviceCount == 0)
                {
                    // Wait for a key press
                    AppendTextBox("Failed to get number of devices (error " + ftStatus.ToString() + ")");
                    return;
                }
                // Allocate storage for device info list
                FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[ftdiDeviceCount];
    
                // Populate our device list
                ftStatus = myFtdiDevice.GetDeviceList(ftdiDeviceList);
                //Clear the combo box
                cmbDevices.Items.Clear();
                if (ftStatus == FTDI.FT_STATUS.FT_OK)
                {
                    //Add each device found to the combo box
                    for (UInt32 i = 0; i < ftdiDeviceCount; i++)
                    {
                        vDevice = ftdiDeviceList[i].Description.ToString();
                        cmbDevices.Items.Add(vDevice);
                    }
                }
            }
    


    not when I run mine code it say this :

    Failed to load function FT_CreateDeviceinforList...

    at this line:

     ftStatus = myFtdiDevice.GetNumberOfDevices(refftdiDeviceCount);

    why?

    it is the same code and same library..

    • Moved by Fei Hu Friday, March 2, 2018 5:26 AM Moved from C#
    Thursday, March 1, 2018 8:56 PM

All replies

  • Try their support page is your best bet to start with as this is a third party product and they would know best.

    http://www.ftdichip.com/FTSupport.htm


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, March 2, 2018 12:56 AM
  • Hi Btb4198,

    According to your question is more related to FTDI, you could repost a new thread to FTDI forum for professional support and better solution.

    The Visual C# discuss and ask the C# programming language, IDE, libraries, samples and tools. If you have some grammar or code errors, 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.

    • Edited by Fei Hu Friday, March 2, 2018 5:25 AM
    Friday, March 2, 2018 5:25 AM