OpenByIndex() public méthode

Opens the FTDI device with the specified index.
Initialises the device to 8 data bits, 1 stop bit, no parity, no flow control and 9600 Baud.
public OpenByIndex ( UInt32 index ) : FT_STATUS
index System.UInt32 Index of the device to open. /// Note that this cannot be guaranteed to open a specific device.
Résultat FT_STATUS
Exemple #1
0
        private void Open(FTDI port, string description)
        {
            uint deviceCount = 0;

            if (port == null) throw new Exception(description + "is not initialized");
            if (port.IsOpen) {
                Global.ShowError(description + " is already open");
                return;
            }

            // Determine the number of FTDI devices connected to the machine
            if ((ftStatus = port.GetNumberOfDevices(ref deviceCount)) != FTDI.FT_STATUS.FT_OK) {
                throw new FTDI.FT_EXCEPTION("Failed to get number of devices. err: " + ftStatus.ToString());
            }

            // If no devices available, return
            if (deviceCount == 0) throw new Exception("No devices on " + description);

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

            // Populate our device list
            if ((ftStatus = port.GetDeviceList(ftdiDeviceList)) != FTDI.FT_STATUS.FT_OK) {
                throw new FTDI.FT_EXCEPTION("Failed to get device list. err " + ftStatus.ToString());
            }

            int foundIndex = -1;
            for (int i = 0; i < deviceCount; i++) {
                if (ftdiDeviceList[i].Description.ToString().Contains(description)) {
                    foundIndex = 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("");
                */
            }
            if (foundIndex < 0) {
                throw new Exception("Failed to find device with description " + description);
            }

            if ((ftStatus = port.OpenByIndex((uint) foundIndex)) != FTDI.FT_STATUS.FT_OK) {
                throw new FTDI.FT_EXCEPTION("Failed to open device. err: " + ftStatus.ToString());
            }
        }
Exemple #2
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);
            }
        }
        /// <summary>
        /// Called when the window is loaded
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void WindowLoaded(object sender, RoutedEventArgs e)
        {
            int i;
            
            // Configure datagrid
            dataGrid1.Items.Clear();
            dataGrid1.Columns.Clear();
            dataGrid1.Columns.Add(new DataGridTextColumn { Header = "Device Number", Binding = new Binding("Dn") });
            dataGrid1.Columns.Add(new DataGridTextColumn { Header = "Serial Number", Binding = new Binding("Sn") });
            dataGrid1.Columns.Add(new DataGridTextColumn { Header = "Device Description", Binding = new Binding("Dd") });

            // Get FTDI basic data
            FTDI f = new FTDI();
            uint devicecount = 0;
            f.GetNumberOfDevices(ref devicecount);

            // Fill datagrid with device data
            string dd, sn, dn;
            if (devicecount == 0)
            {
                FtErrorReport("GetFTDeviceCount", FTDI.FT_STATUS.FT_DEVICE_NOT_FOUND);
            }
            for (i = 0; i < devicecount; i++)
            {
                f.OpenByIndex((uint)i);
                dn = String.Format("Device {0}", i);
                f.GetSerialNumber(out sn);
                f.GetDescription(out dd);
                f.Close();
                dataGrid1.Items.Add( new SDevice{ Dd = dd, Dn = dn, Sn = sn } );
            }
        }