FTDI Community

General Category => Discussion - Software => Topic started by: sachin0005 on January 04, 2018, 04:52:56 AM

Title: FTDI 232R Communication with windows C# application
Post by: sachin0005 on January 04, 2018, 04:52:56 AM
Hi there,

I am trying to develop a windows application with C# language for communication with a device containing FTDI chip 232R UART.

I am using FTD2XX_NET class.

This class is showing that device is connected properly but i am unable to write my command and read response from it.

I tried to use C# Loop back sample code existing in FTDI website but the same code is not working for me.

I tried many times and one time i got the result but after that i am still trying and did not get any response from the device.


Please help and give sample code how to write command and read response with a FTDI device using C#.

Thanks in advance

Below is my code


public class FTDI
{
public FTDI myFtdiDevice;

public string command="?I_SCAN";

private void writecommand()
{

      ftStatus = myFtdiDevice.Write(command,command.length, ref numBytesWritten)
                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    MessageBox.Show("Failed to write to device (error " + ftStatus.ToString() + ")", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }

                numBytesAvailable = 0;

                do
                {
                    ftStatus = myFtdiDevice.GetRxBytesAvailable(ref numBytesAvailable);
                    myFtdiDevice.GetLineStatus(ref data);
                    var st = Encoding.ASCII.GetString(new byte[] { data });
                    if (ftStatus != FTDI.FT_STATUS.FT_OK)
                    {
                        MessageBox.Show("Failed to get number of bytes available to read (error " + ftStatus.ToString() + ")", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return false;
                    }
                    Thread.Sleep(16);
                } while (numBytesAvailable < FTDICommands.GETNUMBEROFSCAN.Length);
}
Title: Re: FTDI 232R Communication with windows C# application
Post by: FTDI Community on January 05, 2018, 01:31:41 PM
Hello,

Thanks for your question.

Can you describe the device you are implementing this code for? is it a cable? what is it trying to communicate with?

Are you implementing the Flow Control lines, I believe the loopback code example requires these.


Best Regards,

FTDI Community
Title: Re: FTDI 232R Communication with windows C# application
Post by: tomg on January 31, 2019, 07:06:42 PM
sachin0005,

I use the FTD2XX_NET library with .NET 4.5.2 and use the Write() and GetRxBytesAvailable() functions without problem.

Apologies, but a basic question up front.  Have you opened the FTDI device?   Your example code does not indicate that you have.

            UInt32 numDevs = 0;
            FTDI.FT_STATUS ftStatus = ftdi.GetNumberOfDevices(ref numDevs);
            FTDI.FT_DEVICE_INFO_NODE[] deviceList = new FTDI.FT_DEVICE_INFO_NODE[numDevs];
            ftStatus = ftdi.GetDeviceList(deviceList);
            foreach (FTDI.FT_DEVICE_INFO_NODE node in deviceList)
            {
                    ftStatus = ftdi.OpenByDescription(node.Description);
                    string comPort;
                    ftStatus = ftdi.GetCOMPort(out comPort);

                    //.....
             }
???
tom