public bool OpenDeviceByLocation(uint LocationID)
        {
            if (USB_Interface.IsOpen)
            {
                USB_Interface.Close();
            }

            ExtLog.AddLine("Opening device");
            var ftStatus = USB_Interface.OpenByLocation(LocationID);

            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                ExtLog.AddLine("Failed to open device (error " + ftStatus + ")");
                return(false);
            }

            ExtLog.AddLine("Setting default bauld rate (" + GlobalProperties.baudRate + ")");
            ftStatus = USB_Interface.SetBaudRate(GlobalProperties.baudRate);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                ExtLog.AddLine("Failed to set Baud rate (error " + ftStatus + ")");
                return(false);
            }

            ExtLog.AddLine("Setting BitMode");
            ftStatus = USB_Interface.SetBitMode(GlobalProperties.portDirectionMask, FTDI.FT_BIT_MODES.FT_BIT_MODE_SYNC_BITBANG);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                ExtLog.AddLine("Failed to set BitMode (error " + ftStatus + ")");
                return(false);
            }

            UpdateProgress(100, true);
            return(true);
        }
示例#2
0
 private void OpenIdFTDI()
 {
     if (ftdiDeviceList != null)
     {
         // Open first device in our list by serial number
         //ftStatus = myFtdiDevice.OpenBySerialNumber(ftdiDeviceList[0].SerialNumber);
         //ftStatus = myFtdiDevice.OpenBySerialNumber(comboBox1.Text);
         ftStatus = myFtdiDevice.OpenByLocation(Convert.ToUInt32(comboBox1.Text));
         Log("Opening device " + comboBox1.Text);
         if (ftStatus != FTDI.FT_STATUS.FT_OK)
         {
             // Wait for a key press
             Log("Failed to open device (error " + ftStatus.ToString() + ")");
             Text = "BitBang - Failed to open";
             return;
         }
         else
         {
             Log("Device opened succesfully");
         }
     }
     else
     {
         Log("NULL");
     }
     Log("_____________________________________");
 }
示例#3
0
        public static List <FDTIPort> FindFdtiUsbDevices()
        {
            ///////////////////////
            // Requires
            // FTD2XX_NET.dll
            ///////////////////////

            List <FDTIPort> ports = new List <FDTIPort>();

            FTDI _ftdi = new FTDI();


            UInt32 count = 0;

            FTDI.FT_STATUS status = _ftdi.GetNumberOfDevices(ref count);
            if (status != FTDI.FT_STATUS.FT_OK)
            {
                Console.WriteLine("log.Warn: Unable to access FTDI");
                return(ports);
            }

            FTDI.FT_DEVICE_INFO_NODE[] list = new FTDI.FT_DEVICE_INFO_NODE[count];
            status = _ftdi.GetDeviceList(list);
            if (status != FTDI.FT_STATUS.FT_OK)
            {
                Console.WriteLine("log.Warn: Unable to access FTDI");
                return(ports);
            }


            foreach (FTDI.FT_DEVICE_INFO_NODE node in list)
            {
                if ((status = _ftdi.OpenByLocation(node.LocId)) == FTDI.FT_STATUS.FT_OK)
                {
                    try
                    {
                        string comport;
                        _ftdi.GetCOMPort(out comport);

                        if (comport != null && comport.Length > 0)
                        {
                            //Console.WriteLine(node.Type);
                            ports.Add(new FDTIPort(comport, node.Description.ToString(), node.SerialNumber.ToString()));
                        }
                    }
                    finally
                    {
                        _ftdi.Close();
                    }
                }
            }

            //_ftdi.Dispose();
            return(ports);
        }
示例#4
0
        public override IObservable <Mat> Generate()
        {
            return(Observable.Create <Mat>((observer, cancellationToken) =>
            {
                return Task.Factory.StartNew(() =>
                {
                    var source = new FTDI();
                    var status = source.OpenByLocation((uint)LocationId);
                    if (status != FTDI.FT_STATUS.FT_OK)
                    {
                        throw new InvalidOperationException("Unable to open the FTDI device at the specified serial port.");
                    }

                    status = source.SetTimeouts(100, 100);
                    if (status != FTDI.FT_STATUS.FT_OK)
                    {
                        throw new InvalidOperationException("Unable to set timeouts on the FTDI device.");
                    }

                    Write(source, StartCommand);
                    var dataFrame = new short[ChannelCount, 1];
                    var packet = new byte[FrameSize];
                    while (!cancellationToken.IsCancellationRequested)
                    {
                        Read(source, packet);
                        for (int i = 0; i < packet.Length; i += 20)
                        {
                            var channelOffset = (i / 20) * 2;

                            // Check if device data is valid
                            if (packet[i + 18] != 0x3F)
                            {
                                dataFrame[channelOffset + 0, 0] = (Int16)(((packet[i + 1] << 8) | packet[i + 2]) >> 4);
                                dataFrame[channelOffset + 1, 0] = (Int16)(((packet[i + 3] << 8) | packet[i + 4]) >> 4);
                            }
                            else
                            {
                                dataFrame[channelOffset + 0, 0] = -1;
                                dataFrame[channelOffset + 1, 0] = -1;
                            }
                        }

                        var dataOutput = Mat.FromArray(dataFrame);
                        observer.OnNext(dataOutput);
                    }

                    Write(source, StopCommand);
                    source.Close();
                },
                                             cancellationToken,
                                             TaskCreationOptions.LongRunning,
                                             TaskScheduler.Default);
            }));
        }
示例#5
0
        public virtual bool OpenDevice()
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                return(ftdi.OpenByIndex(0) == FTDI.FT_STATUS.FT_OK);
            }

            var deviceList      = GetDevicesList();
            var dmxUsbProDevice = deviceList.FirstOrDefault(d => d.Description != null && d.Description.Equals("DMX USB PRO"));

            return(ftdi.OpenByLocation(dmxUsbProDevice.LocId) == FTDI.FT_STATUS.FT_OK);
        }
示例#6
0
        private void open(uint locId)
        {
            lock (_lock) {
                FTDI.FT_STATUS ftStatus = ftdi.OpenByLocation(locId);
                if (ftStatus == FTDI.FT_STATUS.FT_OK)
                {
                    return;
                }

                String errMsg = "Failed to open device using index " + locId + "(error " + ftStatus.ToString() + ")";
                throw new FtdiException(errMsg);
            }
        }
示例#7
0
 public void Open(uint locationId)
 {
     // Open first device in our list by serial number
     if (!mDevice.LoadLibrarySuccess)
     {
         return; // fail to load library
     }
     if (mDevice.IsOpen)
     {
         mDevice.Close();
     }
     mDevice.OpenByLocation(locationId);
 }
示例#8
0
        public FTDIInfo FindFTDI()
        {
            var ft = new FTDI();

            uint deviceCount = 0;
            uint deviceID    = 0;

            var stat = ft.GetNumberOfDevices(ref deviceCount);

            FTDI.FT_DEVICE_INFO_NODE[] devices = new FTDI.FT_DEVICE_INFO_NODE[deviceCount];
            stat = ft.GetDeviceList(devices);

            foreach (var dev in devices)
            {
                try
                {
                    stat = ft.OpenByLocation(dev.LocId);
                    if (stat == FTDI.FT_STATUS.FT_OK)
                    {
                        ft.GetDeviceID(ref deviceID);
                        if (deviceID != 67330049)
                        {
                            ft.GetCOMPort(out var portName);
                            ft.Close();
                            return(new FTDIInfo(portName, deviceID));
                        }
                    }
                }
                catch
                {
                    try
                    {
                        if (ft.IsOpen)
                        {
                            ft.Close();
                        }
                    }
                    finally
                    {
                        if (ft.IsOpen)
                        {
                            ft.Close();
                        }
                    }
                }
            }
            return(null);
        }
示例#9
0
    private string getComPort(FTDI.FT_DEVICE_INFO_NODE node)
    {
        string comport = "";

        //http://stackoverflow.com/questions/2279646/finding-usb-serial-ports-from-a-net-application-under-windows-7
        if (ftdiDeviceWin.OpenByLocation(node.LocId) == FTDI.FT_STATUS.FT_OK)
        {
            try {
                ftdiDeviceWin.GetCOMPort(out comport);
            }
            finally {
                ftdiDeviceWin.Close();
            }
        }

        return(comport);
    }
示例#10
0
        public void Open(string portName, int baudRate)
        {
            if (IsOpen)
            {
                Close();
            }

            var status = getDevice(out _openedDevice);

            status = status == FT_STATUS.FT_OK ? _ftdi.OpenByLocation(_openedDevice.LocId) : status;
            status = status == FT_STATUS.FT_OK ? _ftdi.SetBaudRate(baudRate.ToUInt32()) : status;
            status = status == FT_STATUS.FT_OK ? _ftdi.SetDataCharacteristics(8, 0, 0) : status;
            status = status == FT_STATUS.FT_OK ? _ftdi.SetFlowControl(0x0000, 0x00, 0x00) : status;
            status = status == FT_STATUS.FT_OK ? _ftdi.SetLatency(1) : status;
            status = status == FT_STATUS.FT_OK ? _ftdi.SetUSBParameters(32768, 0) : status;
            status = status == FT_STATUS.FT_OK ? _ftdi.SetTimeouts(1, 1) : status;

            if (status == FT_STATUS.FT_OK)
            {
                PortName = portName;

                ConnectionEstablished?.Invoke(this);
            }
            else
            {
                Logger.LogError($"Не удалось открыть порт {portName}", $"-MSG, статус: {status}");

                _openedDevice = null;
                Close();
            }

            FT_STATUS getDevice(out FT_DEVICE_INFO_NODE device)
            {
                var index = portName
                            .SkipWhile(char.IsLetter)
                            .Aggregate()
                            .ParseToUInt32Invariant();

                device = getAllDevices().ElementAtOrDefault((int)index);

                return(device == null ? FT_STATUS.FT_DEVICE_NOT_FOUND : FT_STATUS.FT_OK);
            }
        }
示例#11
0
 public bool Open()
 {
     FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK;
     if (theDevice != null)
     {
         ftStatus = theDevice.Close();
     }
     if (LocId != 0 && ftStatus == FTDI.FT_STATUS.FT_OK)
     {
         theDevice = new FTDI();
         ftStatus  = theDevice.OpenByLocation(LocId);
         //set MCU Host Bus Emulation mode
         if (ftStatus == FTDI.FT_STATUS.FT_OK)
         {
             ftStatus = theDevice.SetBitMode(0x00, 0x08);
         }
     }
     else
     {
         ftStatus = FTDI.FT_STATUS.FT_OTHER_ERROR;
     }
     return(ftStatus == FTDI.FT_STATUS.FT_OK);
 }
示例#12
0
        public void executaComando(uint location, int tp, byte idRele)
        {
            FTDI acesso = new FTDI();

            FTD2XX_NET.FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK;
            ftStatus = acesso.OpenByLocation(location);


            if (tp == Tipos.Inicia || tp == Tipos.Finaliza)
            {
                saida = idRele;
            }
            else if (tp == Tipos.Liga)
            {
                saida = (byte)(saida | idRele);
            }
            else
            {
                saida = (byte)(saida & idRele);
            }

            ftStatus = acesso.SetBitMode(saida, FTDI.FT_BIT_MODES.FT_BIT_MODE_CBUS_BITBANG);
            ftStatus = acesso.Close();
        }
示例#13
0
        /// <summary>
        /// Opens a connection to the ledstrip controller with the specified controller number.
        /// </summary>
        /// <param name="ControllerNumber">The controller number.</param>
        public void Open(int ControllerNumber)
        {
            lock (FT245RLocker)
            {
                Close();

                bool OK = false;

                string Desc = "";
                this.ControllerNumber = ControllerNumber;

                FT245R = new FTDI();
                FTDI.FT_STATUS FTStatus;



                //Get number of devices
                uint DeviceCnt = 0;
                FTStatus = FT245R.GetNumberOfDevices(ref DeviceCnt);

                if (FTStatus == FTDI.FT_STATUS.FT_OK && DeviceCnt > 0)
                {
                    FTDI.FT_DEVICE_INFO_NODE[] Devices = new FTDI.FT_DEVICE_INFO_NODE[DeviceCnt];

                    //for (uint i = 0; i < DeviceCnt; i++)
                    //{
                    //    FTStatus = FT245R.OpenByIndex(i);
                    // //   Log.Write("Open {0}: Result: {1}".Build(i, FTStatus.ToString()));
                    //    if (FT245R.IsOpen)
                    //    {
                    //        string D = "";
                    //        FT245R.GetDescription(out D);
                    //        Log.Write("Desc: {0}".Build(D));
                    //        try
                    //        {
                    //            FTStatus = FT245R.Close();
                    //            Log.Write("Close {i}: Result: {1}".Build(i, FTStatus.ToString()));
                    //        }
                    //        catch { }
                    //    }

                    //}
                    //Log.Write("All listed");

                    FTStatus = FT245R.GetDeviceList(Devices);
                    if (FTStatus == FTDI.FT_STATUS.FT_OK)
                    {
                        foreach (FTDI.FT_DEVICE_INFO_NODE DI in Devices)
                        {
                            if (DI != null && DI.Type == FTDI.FT_DEVICE.FT_DEVICE_232R)
                            {
                                if (ControllerNameBase.Any(N => DI.Description == N.Trim() + " " + ControllerNumber))
                                {
                                    Desc = DI.Description;
                                    FT245R.CharReceived += new EventHandler <EventArgs>(FT245R_CharReceived);

                                    FTStatus = FT245R.OpenByLocation(DI.LocId);
                                    if (FTStatus == FTDI.FT_STATUS.FT_OK)
                                    {
                                        FTStatus = FT245R.Purge(FTDI.FT_PURGE.FT_PURGE_RX + FTDI.FT_PURGE.FT_PURGE_TX);
                                        if (FTStatus == FTDI.FT_STATUS.FT_OK)
                                        {
                                            OK = true;
                                            break;
                                        }
                                        else
                                        {
                                            Log.Exception("Purge failed for WS2811StripController {0}. Error: {1}".Build(ControllerNumber, FTStatus.ToString()));
                                        }
                                    }
                                    else
                                    {
                                        Log.Exception("Open failed for WS2811StripController {0}. Error: {1}".Build(ControllerNumber, FTStatus.ToString()));
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        Log.Exception("Could not fetch devicelist for WS2811StripControllers. Error: {0}".Build(FTStatus.ToString()));
                    }
                }


                if (!OK)
                {
                    if (!Desc.IsNullOrWhiteSpace())
                    {
                        Log.Warning("{0} detected, but could not open connection.".Build(Desc));
                    }
                    else
                    {
                        Log.Warning("Direct Strip Controller with number {0} not found.".Build(ControllerNumber));
                    }
                    Close();
                }
                else
                {
                    Log.Write("{0} detected and connection opend.".Build(Desc));
                }
            }
        }
示例#14
0
        internal void encode(string SerialNumber, string ChipID, string New)
        {
            content.Clear();


            FTDI.FT_DEVICE_INFO_NODE keyPro;
            content.Clear();
            if (!keyProMap.TryGetValue(ChipID, out keyPro))
            {
                content.Append("Device not found! SerialNumber:" + SerialNumber);
                return;
            }
            FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK;
            // Create new instance of the FTDI device class
            FTDI myFtdiDevice = new FTDI();

            // Open device in our list by serial number
            ftStatus = myFtdiDevice.OpenBySerialNumber(keyPro.SerialNumber);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                content.Append("Failed to open device (error " + ftStatus.ToString() + ")" + "\n");
                return;
            }
            // get encode
            string encode = getMD5("SANWA" + ChipID + New);

            encode = encode.Substring(0, 8) + encode.Substring(12, 8);

            // Create our device EEPROM structure based on the type of device we have open
            if (keyPro.Type == FTDI.FT_DEVICE.FT_DEVICE_232R)
            {
                // We have an FT232R or FT245R so use FT232R EEPROM structure
                FTDI.FT232R_EEPROM_STRUCTURE myEEData = new FTDI.FT232R_EEPROM_STRUCTURE();
                // Read the device EEPROM
                // This can throw an exception if trying to read a device type that does not
                // match the EEPROM structure being used, so should always use a
                // try - catch block when calling
                try
                {
                    ftStatus = myFtdiDevice.ReadFT232REEPROM(myEEData);
                }
                catch (FTDI.FT_EXCEPTION e)
                {
                    content.Append("Exception thrown when calling ReadFT232REEPROM" + "\n");
                }

                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    // Wait for a key press
                    content.Append("Failed to read device EEPROM (error " + ftStatus.ToString() + ")" + "\n");
                    // Close the device
                    myFtdiDevice.Close();
                    return;
                }

                // Change our serial number to write back to device
                // By setting to an empty string, we allow the FTD2XX DLL
                // to generate a serial number
                myEEData.SerialNumber = New;
                myEEData.Description  = encode;

                // Write our modified data structure back to the device EEPROM
                // This can throw an exception if trying to write a device type that does not
                // match the EEPROM structure being used, so should always use a
                // try - catch block when calling
                try
                {
                    ftStatus = myFtdiDevice.WriteFT232REEPROM(myEEData);
                }
                catch (FTDI.FT_EXCEPTION)
                {
                    content.Append("Exception thrown when calling WriteFT232REEPROM" + "\n");
                }

                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    // Wait for a key press
                    content.Append("Failed to write device EEPROM (error " + ftStatus.ToString() + ")" + "\n");
                    // Close the device
                    myFtdiDevice.Close();
                    return;
                }

                // Write common EEPROM elements to our console
                content.Append("EEPROM Contents for device at index 0:" + "\n");
                content.Append("Vendor ID: " + String.Format("{0:x}", myEEData.VendorID) + "\n");
                content.Append("Product ID: " + String.Format("{0:x}", myEEData.ProductID) + "\n");
                content.Append("Manufacturer: " + myEEData.Manufacturer.ToString() + "\n");
                content.Append("Manufacturer ID: " + myEEData.ManufacturerID.ToString() + "\n");
                //content.Append("Description: " + myEEData.Description.ToString() + "\n");
                content.Append("Description: " + encode + "\n");
                content.Append("Serial Number: " + myEEData.SerialNumber.ToString() + "\n");
                content.Append("Max Power: " + myEEData.MaxPower.ToString() + "mA" + "\n");
                content.Append("Self Powered: " + myEEData.SelfPowered.ToString() + "\n");
                content.Append("Remote Wakeup Enabled: " + myEEData.RemoteWakeup.ToString() + "\n");
            }
            else if (keyPro.Type == FTDI.FT_DEVICE.FT_DEVICE_2232)
            {
                // We have an FT2232 so use FT2232 EEPROM structure
                FTDI.FT2232_EEPROM_STRUCTURE myEEData = new FTDI.FT2232_EEPROM_STRUCTURE();
                // Read the device EEPROM
                ftStatus = myFtdiDevice.ReadFT2232EEPROM(myEEData);
                // This can throw an exception if trying to read a device type that does not
                // match the EEPROM structure being used, so should always use a
                // try - catch block when calling
                try
                {
                    ftStatus = myFtdiDevice.ReadFT2232EEPROM(myEEData);
                }
                catch (FTDI.FT_EXCEPTION)
                {
                    content.Append("Exception thrown when calling ReadFT2232EEPROM" + "\n");
                }

                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    // Wait for a key press
                    content.Append("Failed to read device EEPROM (error " + ftStatus.ToString() + ")" + "\n");
                    // Close the device
                    myFtdiDevice.Close();
                    return;
                }

                // Write common EEPROM elements to our console
                content.Append("EEPROM Contents for device at index 0:" + "\n");
                content.Append("Vendor ID: " + String.Format("{0:x}", myEEData.VendorID) + "\n");
                content.Append("Product ID: " + String.Format("{0:x}", myEEData.ProductID) + "\n");
                content.Append("Manufacturer: " + myEEData.Manufacturer.ToString() + "\n");
                content.Append("Manufacturer ID: " + myEEData.ManufacturerID.ToString() + "\n");
                //content.Append("Description: " + myEEData.Description.ToString() + "\n");
                content.Append("Description: " + encode + "\n");
                content.Append("Serial Number: " + myEEData.SerialNumber.ToString() + "\n");
                content.Append("Max Power: " + myEEData.MaxPower.ToString() + "mA" + "\n");
                content.Append("Self Powered: " + myEEData.SelfPowered.ToString() + "\n");
                content.Append("Remote Wakeup Enabled: " + myEEData.RemoteWakeup.ToString() + "\n");

                // Change our serial number to write back to device
                // By setting to an empty string, we allow the FTD2XX DLL
                // to generate a serial number
                //myEEData.SerialNumber = String.Empty;
                myEEData.Description = encode;

                // Write our modified data structure back to the device EEPROM
                // This can throw an exception if trying to write a device type that does not
                // match the EEPROM structure being used, so should always use a
                // try - catch block when calling
                try
                {
                    ftStatus = myFtdiDevice.WriteFT2232EEPROM(myEEData);
                }
                catch (FTDI.FT_EXCEPTION)
                {
                    content.Append("Exception thrown when calling WriteFT2232EEPROM" + "\n");
                }

                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    content.Append("Failed to write device EEPROM (error " + ftStatus.ToString() + ")" + "\n");
                    // Close the device
                    myFtdiDevice.Close();
                    return;
                }
            }
            else if (keyPro.Type == FTDI.FT_DEVICE.FT_DEVICE_BM)
            {
                // We have an FT232B or FT245B so use FT232B EEPROM structure
                FTDI.FT232B_EEPROM_STRUCTURE myEEData = new FTDI.FT232B_EEPROM_STRUCTURE();
                // Read the device EEPROM
                ftStatus = myFtdiDevice.ReadFT232BEEPROM(myEEData);
                // This can throw an exception if trying to read a device type that does not
                // match the EEPROM structure being used, so should always use a
                // try - catch block when calling
                try
                {
                    ftStatus = myFtdiDevice.ReadFT232BEEPROM(myEEData);
                }
                catch (FTDI.FT_EXCEPTION)
                {
                    content.Append("Exception thrown when calling ReadFT232BEEPROM" + "\n");
                }

                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    // Wait for a key press
                    content.Append("Failed to read device EEPROM (error " + ftStatus.ToString() + ")" + "\n");
                    // Close the device
                    myFtdiDevice.Close();
                    return;
                }

                // Write common EEPROM elements to our console
                content.Append("EEPROM Contents for device at index 0:" + "\n");
                content.Append("Vendor ID: " + String.Format("{0:x}", myEEData.VendorID) + "\n");
                content.Append("Product ID: " + String.Format("{0:x}", myEEData.ProductID) + "\n");
                content.Append("Manufacturer: " + myEEData.Manufacturer.ToString() + "\n");
                content.Append("Manufacturer ID: " + myEEData.ManufacturerID.ToString() + "\n");
                //content.Append("Description: " + myEEData.Description.ToString() + "\n");
                content.Append("Description: " + encode + "\n");
                content.Append("Serial Number: " + myEEData.SerialNumber.ToString() + "\n");
                content.Append("Max Power: " + myEEData.MaxPower.ToString() + "mA" + "\n");
                content.Append("Self Powered: " + myEEData.SelfPowered.ToString() + "\n");
                content.Append("Remote Wakeup Enabled: " + myEEData.RemoteWakeup.ToString() + "\n");

                // Change our serial number to write back to device
                // By setting to an empty string, we allow the FTD2XX DLL
                // to generate a serial number
                //myEEData.SerialNumber = String.Empty;
                myEEData.Description = encode;

                // Write our modified data structure back to the device EEPROM
                // This can throw an exception if trying to write a device type that does not
                // match the EEPROM structure being used, so should always use a
                // try - catch block when calling
                try
                {
                    ftStatus = myFtdiDevice.WriteFT232BEEPROM(myEEData);
                }
                catch (FTDI.FT_EXCEPTION)
                {
                    content.Append("Exception thrown when calling WriteFT232BEEPROM" + "\n");
                }

                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    // Wait for a key press
                    content.Append("Failed to write device EEPROM (error " + ftStatus.ToString() + "\n");
                    // Close the device
                    myFtdiDevice.Close();
                    return;
                }
            }


            // Use cycle port to force a re-enumeration of the device.
            // In the FTD2XX_NET class library, the cycle port method also
            // closes the open handle so no need to call the Close method separately.
            ftStatus = myFtdiDevice.CyclePort();

            UInt32 newFtdiDeviceCount = 0;

            do
            {
                // Wait for device to be re-enumerated
                // The device will have the same location since it has not been
                // physically unplugged, so we will keep trying to open it until it succeeds
                ftStatus = myFtdiDevice.OpenByLocation(keyPro.LocId);
                Thread.Sleep(1000);
            } while (ftStatus != FTDI.FT_STATUS.FT_OK);

            // Close the device
            myFtdiDevice.Close();

            // Re-create our device list
            ftStatus = myFtdiDevice.GetNumberOfDevices(ref newFtdiDeviceCount);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                // Wait for a key press
                content.Append("Failed to get number of devices (error " + ftStatus.ToString() + ")" + "\n");
                return;
            }
        }
示例#15
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[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;
            }


            // Create our device EEPROM structure based on the type of device we have open
            if (ftdiDeviceList[0].Type == FTDI.FT_DEVICE.FT_DEVICE_232R)
            {
                // We have an FT232R or FT245R so use FT232R EEPROM structure
                FTDI.FT232R_EEPROM_STRUCTURE myEEData = new FTDI.FT232R_EEPROM_STRUCTURE();
                // Read the device EEPROM
                // This can throw an exception if trying to read a device type that does not
                // match the EEPROM structure being used, so should always use a
                // try - catch block when calling
                try
                {
                    ftStatus = myFtdiDevice.ReadFT232REEPROM(myEEData);
                }
                catch (FTDI.FT_EXCEPTION)
                {
                    Console.WriteLine("Exception thrown when calling ReadFT232REEPROM");
                }

                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    // Wait for a key press
                    Console.WriteLine("Failed to read device EEPROM (error " + ftStatus.ToString() + ")");
                    Console.ReadKey();
                    // Close the device
                    myFtdiDevice.Close();
                    return;
                }

                // Write common EEPROM elements to our console
                Console.WriteLine("EEPROM Contents for device at index 0:");
                Console.WriteLine("Vendor ID: " + String.Format("{0:x}", myEEData.VendorID));
                Console.WriteLine("Product ID: " + String.Format("{0:x}", myEEData.ProductID));
                Console.WriteLine("Manufacturer: " + myEEData.Manufacturer.ToString());
                Console.WriteLine("Manufacturer ID: " + myEEData.ManufacturerID.ToString());
                Console.WriteLine("Description: " + myEEData.Description.ToString());
                Console.WriteLine("Serial Number: " + myEEData.SerialNumber.ToString());
                Console.WriteLine("Max Power: " + myEEData.MaxPower.ToString() + "mA");
                Console.WriteLine("Self Powered: " + myEEData.SelfPowered.ToString());
                Console.WriteLine("Remote Wakeup Enabled: " + myEEData.RemoteWakeup.ToString());
                Console.WriteLine("");

                // Change our serial number to write back to device
                // By setting to an empty string, we allow the FTD2XX DLL
                // to generate a serial number
                myEEData.SerialNumber = String.Empty;

                // Write our modified data structure back to the device EEPROM
                // This can throw an exception if trying to write a device type that does not
                // match the EEPROM structure being used, so should always use a
                // try - catch block when calling
                try
                {
                    ftStatus = myFtdiDevice.WriteFT232REEPROM(myEEData);
                }
                catch (FTDI.FT_EXCEPTION)
                {
                    Console.WriteLine("Exception thrown when calling WriteFT232REEPROM");
                }

                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    // Wait for a key press
                    Console.WriteLine("Failed to write device EEPROM (error " + ftStatus.ToString() + ")");
                    Console.ReadKey();
                    // Close the device
                    myFtdiDevice.Close();
                    return;
                }
            }
            else if (ftdiDeviceList[0].Type == FTDI.FT_DEVICE.FT_DEVICE_2232)
            {
                // We have an FT2232 so use FT2232 EEPROM structure
                FTDI.FT2232_EEPROM_STRUCTURE myEEData = new FTDI.FT2232_EEPROM_STRUCTURE();
                // Read the device EEPROM
                ftStatus = myFtdiDevice.ReadFT2232EEPROM(myEEData);
                // This can throw an exception if trying to read a device type that does not
                // match the EEPROM structure being used, so should always use a
                // try - catch block when calling
                try
                {
                    ftStatus = myFtdiDevice.ReadFT2232EEPROM(myEEData);
                }
                catch (FTDI.FT_EXCEPTION)
                {
                    Console.WriteLine("Exception thrown when calling ReadFT2232EEPROM");
                }

                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    // Wait for a key press
                    Console.WriteLine("Failed to read device EEPROM (error " + ftStatus.ToString() + ")");
                    Console.ReadKey();
                    // Close the device
                    myFtdiDevice.Close();
                    return;
                }

                // Write common EEPROM elements to our console
                Console.WriteLine("EEPROM Contents for device at index 0:");
                Console.WriteLine("Vendor ID: " + String.Format("{0:x}", myEEData.VendorID));
                Console.WriteLine("Product ID: " + String.Format("{0:x}", myEEData.ProductID));
                Console.WriteLine("Manufacturer: " + myEEData.Manufacturer.ToString());
                Console.WriteLine("Manufacturer ID: " + myEEData.ManufacturerID.ToString());
                Console.WriteLine("Description: " + myEEData.Description.ToString());
                Console.WriteLine("Serial Number: " + myEEData.SerialNumber.ToString());
                Console.WriteLine("Max Power: " + myEEData.MaxPower.ToString() + "mA");
                Console.WriteLine("Self Powered: " + myEEData.SelfPowered.ToString());
                Console.WriteLine("Remote Wakeup Enabled: " + myEEData.RemoteWakeup.ToString());
                Console.WriteLine("");

                // Change our serial number to write back to device
                // By setting to an empty string, we allow the FTD2XX DLL
                // to generate a serial number
                myEEData.SerialNumber = String.Empty;

                // Write our modified data structure back to the device EEPROM
                // This can throw an exception if trying to write a device type that does not
                // match the EEPROM structure being used, so should always use a
                // try - catch block when calling
                try
                {
                    ftStatus = myFtdiDevice.WriteFT2232EEPROM(myEEData);
                }
                catch (FTDI.FT_EXCEPTION)
                {
                    Console.WriteLine("Exception thrown when calling WriteFT2232EEPROM");
                }

                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    // Wait for a key press
                    Console.WriteLine("Failed to write device EEPROM (error " + ftStatus.ToString() + ")");
                    Console.ReadKey();
                    // Close the device
                    myFtdiDevice.Close();
                    return;
                }
            }
            else if (ftdiDeviceList[0].Type == FTDI.FT_DEVICE.FT_DEVICE_BM)
            {
                // We have an FT232B or FT245B so use FT232B EEPROM structure
                FTDI.FT232B_EEPROM_STRUCTURE myEEData = new FTDI.FT232B_EEPROM_STRUCTURE();
                // Read the device EEPROM
                ftStatus = myFtdiDevice.ReadFT232BEEPROM(myEEData);
                // This can throw an exception if trying to read a device type that does not
                // match the EEPROM structure being used, so should always use a
                // try - catch block when calling
                try
                {
                    ftStatus = myFtdiDevice.ReadFT232BEEPROM(myEEData);
                }
                catch (FTDI.FT_EXCEPTION)
                {
                    Console.WriteLine("Exception thrown when calling ReadFT232BEEPROM");
                }

                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    // Wait for a key press
                    Console.WriteLine("Failed to read device EEPROM (error " + ftStatus.ToString() + ")");
                    Console.ReadKey();
                    // Close the device
                    myFtdiDevice.Close();
                    return;
                }

                // Write common EEPROM elements to our console
                Console.WriteLine("EEPROM Contents for device at index 0:");
                Console.WriteLine("Vendor ID: " + String.Format("{0:x}", myEEData.VendorID));
                Console.WriteLine("Product ID: " + String.Format("{0:x}", myEEData.ProductID));
                Console.WriteLine("Manufacturer: " + myEEData.Manufacturer.ToString());
                Console.WriteLine("Manufacturer ID: " + myEEData.ManufacturerID.ToString());
                Console.WriteLine("Description: " + myEEData.Description.ToString());
                Console.WriteLine("Serial Number: " + myEEData.SerialNumber.ToString());
                Console.WriteLine("Max Power: " + myEEData.MaxPower.ToString() + "mA");
                Console.WriteLine("Self Powered: " + myEEData.SelfPowered.ToString());
                Console.WriteLine("Remote Wakeup Enabled: " + myEEData.RemoteWakeup.ToString());
                Console.WriteLine("");

                // Change our serial number to write back to device
                // By setting to an empty string, we allow the FTD2XX DLL
                // to generate a serial number
                myEEData.SerialNumber = String.Empty;

                // Write our modified data structure back to the device EEPROM
                // This can throw an exception if trying to write a device type that does not
                // match the EEPROM structure being used, so should always use a
                // try - catch block when calling
                try
                {
                    ftStatus = myFtdiDevice.WriteFT232BEEPROM(myEEData);
                }
                catch (FTDI.FT_EXCEPTION)
                {
                    Console.WriteLine("Exception thrown when calling WriteFT232BEEPROM");
                }

                if (ftStatus != FTDI.FT_STATUS.FT_OK)
                {
                    // Wait for a key press
                    Console.WriteLine("Failed to write device EEPROM (error " + ftStatus.ToString() + ")");
                    Console.ReadKey();
                    // Close the device
                    myFtdiDevice.Close();
                    return;
                }
            }


            // Use cycle port to force a re-enumeration of the device.
            // In the FTD2XX_NET class library, the cycle port method also
            // closes the open handle so no need to call the Close method separately.
            ftStatus = myFtdiDevice.CyclePort();

            UInt32 newFtdiDeviceCount = 0;

            do
            {
                // Wait for device to be re-enumerated
                // The device will have the same location since it has not been
                // physically unplugged, so we will keep trying to open it until it succeeds
                ftStatus = myFtdiDevice.OpenByLocation(ftdiDeviceList[0].LocId);
                Thread.Sleep(1000);
            } while (ftStatus != FTDI.FT_STATUS.FT_OK);

            // Close the device
            myFtdiDevice.Close();

            // Re-create our device list
            ftStatus = myFtdiDevice.GetNumberOfDevices(ref newFtdiDeviceCount);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                // Wait for a key press
                Console.WriteLine("Failed to get number of devices (error " + ftStatus.ToString() + ")");
                Console.ReadKey();
                return;
            }

            // Re-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("");
                }
            }

            // Wait for a key press
            Console.WriteLine("Press any key to continue.");
            Console.ReadKey();
            return;
        }