SetTimeouts() public method

Sets the read and write timeout values.
public SetTimeouts ( UInt32 ReadTimeout, UInt32 WriteTimeout ) : FT_STATUS
ReadTimeout System.UInt32 Read timeout value in ms. A value of 0 indicates an infinite timeout.
WriteTimeout System.UInt32 Write timeout value in ms. A value of 0 indicates an infinite timeout.
return FT_STATUS
Example #1
0
        private void button5_Click( object sender, EventArgs e )
        {
            UInt32 ftdiDeviceCount = 0;
            

            // Create new instance of the FTDI device class
            myFtdiDevice = new FTDI();
            myFtdiDevice.GetNumberOfDevices( ref ftdiDeviceCount );
            FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[ftdiDeviceCount];
            myFtdiDevice.GetDeviceList( ftdiDeviceList );
            myFtdiDevice.OpenBySerialNumber( ftdiDeviceList[0].SerialNumber );
            myFtdiDevice.SetBaudRate( 9600 );
            myFtdiDevice.SetDataCharacteristics( FTDI.FT_DATA_BITS.FT_BITS_8, FTDI.FT_STOP_BITS.FT_STOP_BITS_1, FTDI.FT_PARITY.FT_PARITY_NONE );
            myFtdiDevice.SetFlowControl( FTDI.FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, 0x11, 0x13 );
            myFtdiDevice.SetTimeouts( 5000, 0 );
            richTextBox1.AppendText("polaczono");
        }
Example #2
0
        public CentralBoard()
        {
            centralBoardCom = new FTD2XX_NET.FTDI();
            propertyManager = PropertyManager.i;
            propertyManager.Load("Hardware");

            string SerialNumber = propertyManager.GetStringValue("Hardware", "CentralBoardSerialNumber");

            centralBoardCom.OpenBySerialNumber(SerialNumber);

            if (!centralBoardCom.IsOpen)
            {
                throw new Exception("Could Not Connect to CentralBoard");
            }

            centralBoardCom.SetBaudRate(38400);
            centralBoardCom.SetTimeouts(100, 1000);
        }
        //==============================
        //serial port stuff
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dev"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public FTDI.FT_STATUS setSerialParams(FTDI dev, uint index)
        {
            FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK;
            // Set up device data parameters
            // must be opened first

            // Set Baud rate to 9600
            ftStatus = dev.SetBaudRate(9600);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
                return ftStatus;

            // Set data characteristics - Data bits, Stop bits, Parity
            ftStatus = dev.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)
                return ftStatus;

            // Set flow control - set RTS/CTS flow control
            //ftStatus = dev.SetFlowControl(
            //    FTDI.FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, 0x11, 0x13);
            //ftStatus = dev.SetRTS(true);
            //Thread.Sleep(5);
            //ftStatus=dev.SetRTS(false);

            ftStatus = dev.SetFlowControl(
                FTDI.FT_FLOW_CONTROL.FT_FLOW_NONE, 0x11, 0x13);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
                return ftStatus;

            // Set read timeout to 5 seconds, write timeout to infinite
            ftStatus = dev.SetTimeouts(5000, 0);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
                return ftStatus;
            return ftStatus;
        }
Example #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="iSoundChipType"></param>
        /// <param name="clock"></param>
        /// <returns></returns>
        public static VsifClient TryToConnectVSIF(VsifSoundModuleType soundModule, PortId comPort, bool shareOnly)
        {
            lock (lockObject)
            {
                foreach (var c in vsifClients)
                {
                    if (c.SoundModuleType == soundModule)
                    {
                        if (c.DataWriter.PortName.Equals("COM" + (int)(comPort + 1)))
                        {
                            c.ReferencedCount++;
                            return(c);
                        }
                        if (c.DataWriter.PortName.Equals("FTDI_COM" + (int)comPort))
                        {
                            c.ReferencedCount++;
                            return(c);
                        }
                    }
                }
                if (shareOnly)
                {
                    return(null);
                }

                try
                {
                    switch (soundModule)
                    {
                    case VsifSoundModuleType.Generic_UART:
                    {
                        SerialPort sp = null;
                        sp = new SerialPort("COM" + ((int)comPort + 1));
                        sp.WriteTimeout = 100;
                        sp.ReadTimeout  = 100;
                        sp.BaudRate     = 115200;
                        sp.BaudRate     = 1200 * (int)Settings.Default.BitBangWaitAY8910;
                        sp.StopBits     = StopBits.One;
                        sp.DataBits     = 8;
                        sp.Handshake    = Handshake.None;
                        sp.Open();
                        var client = new VsifClient(soundModule, new PortWriterGenericUart(sp));
                        client.Disposed += Client_Disposed;
                        vsifClients.Add(client);
                        return(client);
                    }

                    case VsifSoundModuleType.SMS:
                    {
                        SerialPort sp = null;
                        sp = new SerialPort("COM" + ((int)comPort + 1));
                        sp.WriteTimeout = 100;
                        sp.ReadTimeout  = 100;
                        sp.BaudRate     = 115200;
                        sp.StopBits     = StopBits.Two;
                        //sp.BaudRate = 57600;
                        //sp.StopBits = StopBits.One;
                        sp.Parity    = Parity.None;
                        sp.DataBits  = 8;
                        sp.Handshake = Handshake.None;
                        sp.Open();
                        var client = new VsifClient(soundModule, new PortWriterGenesis(sp));
                        client.Disposed += Client_Disposed;
                        vsifClients.Add(client);
                        return(client);
                    }

                    case VsifSoundModuleType.Genesis:
                    {
                        SerialPort sp = null;
                        sp = new SerialPort("COM" + ((int)comPort + 1));
                        sp.WriteTimeout = 500;
                        sp.ReadTimeout  = 500;
                        //sp.BaudRate = 230400;
                        sp.BaudRate = 163840;
                        sp.StopBits = StopBits.One;
                        sp.Parity   = Parity.None;
                        sp.DataBits = 8;
                        sp.Open();
                        var client = new VsifClient(soundModule, new PortWriterGenesis(sp));
                        client.Disposed += Client_Disposed;
                        vsifClients.Add(client);
                        return(client);
                    }

                    case VsifSoundModuleType.Genesis_Low:
                    {
                        SerialPort sp = null;
                        sp = new SerialPort("COM" + ((int)comPort + 1));
                        sp.WriteTimeout = 500;
                        sp.ReadTimeout  = 500;
                        sp.BaudRate     = 115200;
                        sp.StopBits     = StopBits.One;
                        sp.Parity       = Parity.None;
                        sp.DataBits     = 8;
                        sp.Open();
                        var client = new VsifClient(soundModule, new PortWriterGenesis(sp));
                        client.Disposed += Client_Disposed;
                        vsifClients.Add(client);
                        return(client);
                    }

                    case VsifSoundModuleType.Genesis_FTDI:
                    {
                        var ftdi = new FTD2XX_NET.FTDI();
                        var stat = ftdi.OpenByIndex((uint)comPort);
                        if (stat == FTDI.FT_STATUS.FT_OK)
                        {
                            ftdi.SetBitMode(0x00, FTDI.FT_BIT_MODES.FT_BIT_MODE_RESET);
                            ftdi.SetBitMode(0xff, FTDI.FT_BIT_MODES.FT_BIT_MODE_ASYNC_BITBANG);
                            ftdi.SetBaudRate(FTDI_BAUDRATE_GEN * FTDI_BAUDRATE_GEN_MUL);
                            ftdi.SetTimeouts(500, 500);
                            ftdi.SetLatency(0);
                            byte ps = 0;
                            ftdi.GetPinStates(ref ps);
                            if ((ps & 0x40) == 0)
                            {
                                uint dummy = 0;
                                ftdi.Write(new byte[] { 0x40 }, 1, ref dummy);
                            }

                            var client = new VsifClient(soundModule, new PortWriterGenesis(ftdi, comPort));

                            //ftdi.Write(new byte[] { (byte)(((0x07 << 1) & 0xe) | 0) }, 1, ref dummy);
                            //ftdi.Write(new byte[] { (byte)(((0x38 >> 2) & 0xe) | 1) }, 1, ref dummy);
                            //ftdi.Write(new byte[] { (byte)(((0xC0 >> 5) & 0xe) | 0) }, 1, ref dummy);
                            //ftdi.Write(new byte[] { 1 }, 1, ref dummy);

                            client.Disposed += Client_Disposed;
                            vsifClients.Add(client);
                            return(client);
                        }
                    }
                    break;

                    case VsifSoundModuleType.MSX_FTDI:
                    {
                        var ftdi = new FTD2XX_NET.FTDI();
                        var stat = ftdi.OpenByIndex((uint)comPort);
                        if (stat == FTDI.FT_STATUS.FT_OK)
                        {
                            ftdi.SetBitMode(0x00, FTDI.FT_BIT_MODES.FT_BIT_MODE_RESET);
                            ftdi.SetBitMode(0xff, FTDI.FT_BIT_MODES.FT_BIT_MODE_ASYNC_BITBANG);
                            ftdi.SetBaudRate(FTDI_BAUDRATE_MSX * FTDI_BAUDRATE_MSX_MUL);
                            ftdi.SetTimeouts(500, 500);
                            ftdi.SetLatency(0);
                            //{
                            //    uint dummy = 0x00;
                            //    ftdi.Write(new byte[] { (byte)dummy }, 1, ref dummy);
                            //}

                            var client = new VsifClient(soundModule, new PortWriterMsx(ftdi, comPort));

                            client.Disposed += Client_Disposed;
                            vsifClients.Add(client);
                            return(client);
                        }
                    }
                    break;

                    case VsifSoundModuleType.NES_FTDI_INDIRECT:
                    case VsifSoundModuleType.NES_FTDI_DIRECT:
                    {
                        var ftdi = new FTD2XX_NET.FTDI();
                        var stat = ftdi.OpenByIndex((uint)comPort);
                        if (stat == FTDI.FT_STATUS.FT_OK)
                        {
                            ftdi.SetBitMode(0x00, FTDI.FT_BIT_MODES.FT_BIT_MODE_RESET);
                            ftdi.SetBitMode(0xff, FTDI.FT_BIT_MODES.FT_BIT_MODE_ASYNC_BITBANG);
                            ftdi.SetBaudRate(FTDI_BAUDRATE_NES * FTDI_BAUDRATE_NES_MUL);
                            ftdi.SetTimeouts(500, 500);
                            ftdi.SetLatency(0);
                            byte ps = 0;
                            ftdi.GetPinStates(ref ps);
                            if ((ps & 0x10) == 0x10)
                            {
                                uint dummy = 0;
                                ftdi.Write(new byte[] { 0x00 }, 1, ref dummy);
                            }

                            VsifClient client = null;
                            switch (soundModule)
                            {
                            case VsifSoundModuleType.NES_FTDI_DIRECT:
                                client = new VsifClient(soundModule, new PortWriterNesDirect(ftdi, comPort));
                                break;

                            case VsifSoundModuleType.NES_FTDI_INDIRECT:
                                client = new VsifClient(soundModule, new PortWriterNesIndirect(ftdi, comPort));
                                break;
                            }

                            client.Disposed += Client_Disposed;
                            vsifClients.Add(client);
                            return(client);
                        }
                    }
                    break;
                    }

                    //sp.Write(new byte[] { (byte)'M', (byte)'a', (byte)'M', (byte)'i' }, 0, 4);
                    //sp.BaseStream.WriteByte((byte)soundModule);
                    //Thread.Sleep(100);
                    //var ret = sp.BaseStream.ReadByte();
                    //if (ret == 0x0F)   //OK
                }
                catch (Exception ex)
                {
                    if (ex.GetType() == typeof(Exception))
                    {
                        throw;
                    }
                    else if (ex.GetType() == typeof(SystemException))
                    {
                        throw;
                    }
                }
                return(null);
            }
        }
Example #5
0
        static void open(FTDI myFtdiDevice)
        {
            UInt32 ftdiDeviceCount = 0;
            FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK;

            // Determine the number of FTDI devices connected to the machine
            ftStatus = myFtdiDevice.GetNumberOfDevices(ref ftdiDeviceCount);
            // Check status
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                return;
            }


            // If no devices available, return
            if (ftdiDeviceCount == 0) return;

            Console.WriteLine("Number of FTDI devices: " + ftdiDeviceCount.ToString());


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

            int oIdx=-1;
            if (ftStatus == FTDI.FT_STATUS.FT_OK)
            {
                for (Int32 i = 0; i < ftdiDeviceCount; i++)
                {
                    if (ftdiDeviceList[i].Type == FTDI.FT_DEVICE.FT_DEVICE_232R)
                    {
                        oIdx = i;
                    }
                }

                if (oIdx < 0)
                {
                    Console.WriteLine("no matching device");
                    return;
                }
            }

            Console.WriteLine("Device Index: " + oIdx.ToString());
            Console.WriteLine("Flags: " + String.Format("{0:x}", ftdiDeviceList[oIdx].Flags));
            Console.WriteLine("Type: " + ftdiDeviceList[oIdx].Type.ToString());
            Console.WriteLine("ID: " + String.Format("{0:x}", ftdiDeviceList[oIdx].ID));
            Console.WriteLine("Location ID: " + String.Format("{0:x}", ftdiDeviceList[oIdx].LocId));
            Console.WriteLine("Serial Number: " + ftdiDeviceList[oIdx].SerialNumber.ToString());
            Console.WriteLine("Description: " + ftdiDeviceList[oIdx].Description.ToString());

            myFtdiDevice.SetDTR(false);
            myFtdiDevice.SetRTS(false);

            // Open first device in our list by serial number
            ftStatus = myFtdiDevice.OpenBySerialNumber(ftdiDeviceList[oIdx].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;
            }

            myFtdiDevice.SetDTR(false);
            myFtdiDevice.SetRTS(false);

            // Set up device data parameters
            uint baud = 230400;
            if (ConfigurationManager.AppSettings["BaudRate"] != null)
            {
                baud = UInt32.Parse(ConfigurationManager.AppSettings["BaudRate"]);
            }
            ftStatus = myFtdiDevice.SetBaudRate(baud);
            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_NONE, 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;
            }

            // times out at 100ms
            ftStatus = myFtdiDevice.SetTimeouts(100, 100);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                // Wait for a key press
                Console.WriteLine("Failed to set timeouts (error " + ftStatus.ToString() + ")");
                //Console.ReadKey();
                return;
            }

            attention();
            Console.WriteLine("CONNECTED. PRESS ANY KEY TO START.");
            Console.WriteLine("CTRL-R: RESET");
            Console.WriteLine("CTRL-P: PROGRAM");
            Console.WriteLine("CTRL-T: SET TIME");
            prompt();

            myFtdiDevice.SetDTR(true);
            Thread.Sleep(100);
            myFtdiDevice.SetDTR(false);

            while (true)
            {
                UInt32 numBytes = 0;
                byte[] buf = new byte[1024];

                while (Console.KeyAvailable)
                {
                    ConsoleKeyInfo cki = Console.ReadKey(true);
                    //Console.WriteLine("{0} (character '{1}')", cki.Key, cki.KeyChar);
                    if ((cki.Modifiers & ConsoleModifiers.Control) != 0)
                    {
                        if (cki.Key.ToString().ToUpper().EndsWith("R"))
                        {
                            attention();
                            Console.WriteLine("\nRESET!");
                            prompt();

                            myFtdiDevice.SetRTS(false);
                            myFtdiDevice.SetDTR(true);
                            Thread.Sleep(100);
                            myFtdiDevice.SetDTR(false);
                            continue;
                        }
                        if (cki.Key.ToString().ToUpper().EndsWith("P"))
                        {
                            attention();

                            Console.WriteLine("\nPROGRAM MODE:");
                            myFtdiDevice.Close();
                            program();

                            string firmwareName = ConfigurationManager.AppSettings["FirmwareName"];
                            LpcProgrammer p = new LpcProgrammer(new StringOutDelegate(myStrDelegate));
                            //p.Prepare();
                            int r = p.Program(firmwareName);

                            attention();
                            Console.WriteLine("\nRETURN VALUE: {0}", r);
                            subdue();

                            throw new Exception("Leaving program mode");
                        }
                        if (cki.Key.ToString().ToUpper().EndsWith("T"))
                        {
                            //  Time: 10:11:0
                            //  Date: 2827.5.9
                            string datePatt = @"yyyy.M.d";
                            string timePatt = @"HH:mm:ss";
                            DateTime dispDt = DateTime.Now;
                            string dtString = "set_date " + dispDt.ToString(datePatt) + "\r";
                            string tmString = "set_time " + dispDt.ToString(timePatt) + "\r";
                            //Console.WriteLine(dtString);
                            //Console.WriteLine(tmString);
                            System.Text.ASCIIEncoding ASCII  = new System.Text.ASCIIEncoding();
                            Byte[] cmdBytes = ASCII.GetBytes(dtString);
                            myFtdiDevice.Write(cmdBytes, cmdBytes.Length, ref numBytes);
                            Thread.Sleep(200);
                            cmdBytes = ASCII.GetBytes(tmString);
                            myFtdiDevice.Write(cmdBytes, cmdBytes.Length, ref numBytes);
                            continue;
                        }
                    }
                    buf[0] = (byte)(cki.KeyChar & 0xff);
                  
                    numBytes = 0;
                    ftStatus = myFtdiDevice.Write(buf, 1, ref numBytes);
                    if (ftStatus != FTDI.FT_STATUS.FT_OK)
                    {
                        throw new Exception("Failed to write to device (error " + ftStatus.ToString() + ")");
                        //return;
                    }
                }

                numBytes = 0;
                ftStatus = myFtdiDevice.GetRxBytesAvailable(ref numBytes);
                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    throw new Exception("Failed to get number of bytes available to read (error " + ftStatus.ToString() + ")");
                    //return;
                }
                if (numBytes > 0)
                {
                    UInt32 numBytesRead = 0;
                    byte[] buffer = new byte[1024];
                    // Note that the Read method is overloaded, so can read string or byte array data
                    //ftStatus = myFtdiDevice.Read(out readData, 1024, ref numBytesRead);
                    ftStatus = myFtdiDevice.Read(buffer, 1024, ref numBytesRead);
                    if (ftStatus != FTDI.FT_STATUS.FT_OK)
                    {
                        // Wait for a key press
                        throw new Exception("Failed to read data (error " + ftStatus.ToString() + ")");
                        //Console.ReadKey();
                        //return;
                    }
                    string readData = System.Text.Encoding.ASCII.GetString(buffer, 0, (int)numBytesRead);
                    Console.Write(readData);
                }
                else
                {
                    Thread.Sleep(10);
                }

            }
        }
Example #6
0
        // public methods
        /// <summary>
        /// Attempt to open RHA2000-EVAL board connected to USB port.
        /// </summary>
        /// <param name="firmwareID1">Board ID number (1 of 3)</param>
        /// <param name="firmwareID2">Board ID number (2 of 3)</param>
        /// <param name="firmwareID3">Board ID number (3 of 3)</param>
        public void Open(ref int firmwareID1, ref int firmwareID2, ref int firmwareID3)
        {
            // Open FTDI USB device.
            UInt32 ftdiDeviceCount = 0;
            FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK;

            // Create new instance of the FTDI device class.
            myFtdiDeviceA = new FTDI();

            // Determine the number of FTDI devices connected to the machine.
            ftStatus = myFtdiDeviceA.GetNumberOfDevices(ref ftdiDeviceCount);

            // Check status.
            if (!(ftStatus == FTDI.FT_STATUS.FT_OK))
            {
                UsbException e = new UsbException("USB Setup Error: Failed to get number devices");
                throw e;
            }

            // If no devices available, return.
            if (ftdiDeviceCount == 0)
            {
                UsbException e = new UsbException("No valid USB devices detected");
                throw e;
            }

            // Allocate storage for device info list.
            FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[ftdiDeviceCount];

            // Populate our device list.
            ftStatus = myFtdiDeviceA.GetDeviceList(ftdiDeviceList);
            // There could be a status error here, but we're not checking for it...

            // The Intan Technologies RHA2000-EVAL board uses an FTDI FT2232H chip to provide a USB
            // interface to a PC.  Detailed information on this chip may be found at:
            //
            //   http://www.ftdichip.com/Products/ICs/FT2232H.htm
            //
            // The FT2232H supports two independent FIFO channels.  The channel used by the RHA2000-EVAL
            // board is factory-configured with the name "Intan I/O board 1.0 A".  (FTDI provides software
            // routines to open device by its name.)

            ftStatus = myFtdiDeviceA.OpenByDescription("Intan I/O Board 1.0 A");
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                UsbException e = new UsbException("Intan USB device A not found");
                throw e;
            }

            // Set read timeout to 5 seconds, write timeout to infinite
            ftStatus = myFtdiDeviceA.SetTimeouts(5000, 0);
            // There could be status error here, but we're not checking for it...

            this.Stop();

            // Purge receive buffer

            myFtdiDeviceA.Purge(FTDI.FT_PURGE.FT_PURGE_RX);

            // Check board ID and version number
            //
            // The RHA2000-EVAL board is controlled by sending one-byte ASCII command characters over
            // the USB interface.  The 'I' character commands the board to return a 3-byte ID/version
            // number.

            UInt32 numBytesWritten = 0;
            Byte[] myChars = { 73 };   // 'I' = request board ID and version number

            ftStatus = myFtdiDeviceA.Write(myChars, 1, ref numBytesWritten);

            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                UsbException e = new UsbException("Could not write to Intan USB device");
                throw e;
            }

            const UInt32 numBytesToRead = 3;

            // Wait until the desired number of bytes have been received.
            UInt32 numBytesAvailable = 0;

            while (numBytesAvailable < numBytesToRead)
            {
                ftStatus = myFtdiDeviceA.GetRxBytesAvailable(ref numBytesAvailable);

                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    UsbException e = new UsbException("Failed to get number of USB bytes available to read");
                    throw e;
                }
            }

            // Now that we have the amount of data we want available, read it.

            UInt32 numBytesRead = 0;

            ftStatus = myFtdiDeviceA.Read(readDataBufferA, numBytesToRead, ref numBytesRead);

            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                UsbException e = new UsbException("USB read error");
                throw e;
            }

            firmwareID1 = Convert.ToInt32(readDataBufferA[0]);
            firmwareID2 = Convert.ToInt32(readDataBufferA[1]);
            firmwareID3 = Convert.ToInt32(readDataBufferA[2]);

            Debug.WriteLine("Board ID: " + readDataBufferA[0] + " " + (Convert.ToInt32(readDataBufferA[1])).ToString() + " " + (Convert.ToInt32(readDataBufferA[2])).ToString());

            this.ZCheckOff();
            this.SettleOff();

            // Purge receive buffer.
            myFtdiDeviceA.Purge(FTDI.FT_PURGE.FT_PURGE_RX);

            dataJustStarted = true;
        }
Example #7
0
        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[1].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_NONE,0,0);
            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);

            myFtdiDevice.Purge(FTDI.FT_PURGE.FT_PURGE_TX);
            if (numBytesWritten != dataToWrite.Length)
            {
                Console.WriteLine("Error writting");
                Console.Read();
                return;
            }

            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);
                //Console.WriteLine(ftStatus + " " + 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;
        }