Esempio n. 1
0
        void UpdatePorts()
        {
            FTDI      ftdi = new FTDI();
            FT_STATUS status;
            uint      numDevices = 0;

            status = ftdi.GetNumberOfDevices(ref numDevices);

            // filter valid devices
            // opened devices can not be displayed
            List <string> names = new List <string>((int)numDevices);
            List <FT_DEVICE_INFO_NODE> validDevices = new List <FT_DEVICE_INFO_NODE>((int)numDevices);

            if (numDevices > 0)
            {
                FT_DEVICE_INFO_NODE[] devices = new FT_DEVICE_INFO_NODE[numDevices];
                ftdi.GetDeviceList(devices);
                foreach (var d in devices)
                {
                    if ((d.Type != FT_DEVICE.FT_DEVICE_UNKNOWN) &&
                        (!String.IsNullOrEmpty(d.SerialNumber)))
                    {
                        validDevices.Add(d);
                        names.Add(String.Format("{0} ({1})", d.SerialNumber, d.Description));
                    }
                }
            }

            _Devices  = validDevices.ToArray();
            PortNames = names.ToArray();
        }
Esempio n. 2
0
        FT_DEVICE_INFO_NODE[] getAllDevices()
        {
            uint numOfDevices = 0;
            var  status       = _ftdi.GetNumberOfDevices(ref numOfDevices);
            var  devices      = new FT_DEVICE_INFO_NODE[numOfDevices];

            status = status == FT_STATUS.FT_OK ? _ftdi.GetDeviceList(devices) : status;

            return(status == FT_STATUS.FT_OK ? devices : new FT_DEVICE_INFO_NODE[0]);
        }
        public static FT232HDetectorInformation Detect(string serialNumber = null)
        {
            var r      = new FT232HDetectorInformation();
            var ft232h = new FTD2XX_NET.FTDI();

            UInt32 count = 0;

            Ok(ft232h.GetNumberOfDevices(ref count));

            if (count == 0)
            {
                Console.WriteLine("No FT232H device detected");
                return(r);
            }


            FT_DEVICE_INFO_NODE ft232hDevice = null;
            var devices = new FT_DEVICE_INFO_NODE[count];

            ft232h.GetDeviceList(devices);

            if (serialNumber == null)
            {
                ft232hDevice = devices[0];
            }
            else
            {
                ft232hDevice = devices.ToList().FirstOrDefault(d => d.SerialNumber == serialNumber);
            }

            r.SerialNumber = ft232hDevice.SerialNumber;
            r.DeviceType   = ft232hDevice.Type;
            r.Description  = ft232hDevice.Description;

            if (ft232hDevice == null)
            {
                return(FT232HDetectorInformation.Failed);
            }

            Ok(ft232h.OpenBySerialNumber(ft232hDevice.SerialNumber));
            var ee232h = new FT232H_EEPROM_STRUCTURE();
            var rr     = ft232h.ReadFT232HEEPROM(ee232h);

            if (rr == FT_STATUS.FT_OK)
            {
                r.Description = ee232h.Description;
                r.Properties  = GetDictionary(ee232h);
            }
            ft232h.Close();
            r.Ok = true;
            return(r);
        }
Esempio n. 4
0
        public Relay()
        {
            try
            {
                ftdi.SetBaudRate(9600);
                ftdi.GetNumberOfDevices(ref devcount);
                if (devcount == 0)
                {
                    errMsg = "No devices found";
                    return;
                }
                FT_DEVICE_INFO_NODE[] nodes  = new FT_DEVICE_INFO_NODE[devcount];
                FT_STATUS             status = ftdi.GetDeviceList(nodes);
                uint index   = 0;
                uint nRelays = 0;
                foreach (FT_DEVICE_INFO_NODE node in nodes)
                {
                    string description = node.Type.ToString();
                    //string description = "";
                    if (description.Contains("_245R") || description.Contains("_232R"))
                    //if (true)
                    {
                        nRelays++;
                        ftdi.OpenByIndex(index);
                        //Nothing unique in the EEPROM to show 4 or 8 channel
                        //FT232R_EEPROM_STRUCTURE ee232r = new FT232R_EEPROM_STRUCTURE();
                        //ftdi.ReadFT232REEPROM(ee232r);
                        ftdi.GetCOMPort(out string comport);
                        //Close();
                        comList.Add(description);
                        comIndex.Add(index);
                        serialNums.Add(node.SerialNumber);
                    }
                    ++index;
                }
                devcount = nRelays;
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
            {
                errMsg = ex.Message;
            }
        }
Esempio n. 5
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);
            }
        }
Esempio n. 6
0
 public Relay()
 {
     try
     {
         ftdi.SetBaudRate(9600);
         ftdi.GetNumberOfDevices(ref devcount);
         if (devcount == 0)
         {
             errMsg = "No devices found";
             return;
         }
         FT_DEVICE_INFO_NODE[] nodes  = new FT_DEVICE_INFO_NODE[devcount];
         FT_STATUS             status = ftdi.GetDeviceList(nodes);
         uint index   = 0;
         uint nRelays = 0;
         foreach (FT_DEVICE_INFO_NODE node in nodes)
         {
             if (node.Description.Contains("FT245R"))
             {
                 nRelays++;
                 ftdi.OpenByIndex(index);
                 //Nothing unique in the EEPROM to show 4 or 8 channel
                 //FT232R_EEPROM_STRUCTURE ee232r = new FT232R_EEPROM_STRUCTURE();
                 //ftdi.ReadFT232REEPROM(ee232r);
                 ftdi.GetCOMPort(out string comport);
                 //Close();
                 comList.Add(comport);
                 comIndex.Add(index);
                 serialNums.Add(node.SerialNumber);
             }
             ++index;
         }
         devcount = nRelays;
     }
     catch (Exception ex)
     {
         errMsg = ex.Message;
     }
 }
Esempio n. 7
0
        //**************************************************************************
        // GetDeviceList
        //**************************************************************************
        // Intellisense comments
        /// <summary>
        /// Gets information on all of the FTDI devices available.  
        /// </summary>
        /// <returns>FT_STATUS value from FT_GetDeviceInfoDetail in FTD2XX.DLL</returns>
        /// <param name="devicelist">An array of type FT_DEVICE_INFO_NODE to contain the device information for all available devices.</param>
        /// <exception cref="FT_EXCEPTION">Thrown when the supplied buffer is not large enough to contain the device info list.</exception>
        public FT_STATUS GetDeviceList(FT_DEVICE_INFO_NODE[] devicelist)
        {
            // Initialise ftStatus to something other than FT_OK
            FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;
            FT_ERROR ftErrorCondition = FT_ERROR.FT_NO_ERROR;

            // If the DLL hasn't been loaded, just return here
            if (hFTD2XXDLL == IntPtr.Zero)
                return ftStatus;

            // Check for our required function pointers being set up
            if ((pFT_CreateDeviceInfoList != IntPtr.Zero) & (pFT_GetDeviceInfoDetail != IntPtr.Zero))
            {
                UInt32 devcount = 0;

                tFT_CreateDeviceInfoList FT_CreateDeviceInfoList = (tFT_CreateDeviceInfoList)Marshal.GetDelegateForFunctionPointer(pFT_CreateDeviceInfoList, typeof(tFT_CreateDeviceInfoList));
                tFT_GetDeviceInfoDetail FT_GetDeviceInfoDetail = (tFT_GetDeviceInfoDetail)Marshal.GetDelegateForFunctionPointer(pFT_GetDeviceInfoDetail, typeof(tFT_GetDeviceInfoDetail));

                // Call FT_CreateDeviceInfoList
                ftStatus = FT_CreateDeviceInfoList(ref devcount);

                // Allocate the required storage for our list

                byte[] sernum = new byte[16];
                byte[] desc = new byte[64];

                if (devcount > 0)
                {
                    // Check the size of the buffer passed in is big enough
                    if (devicelist.Length < devcount)
                    {
                        // Buffer not big enough
                        ftErrorCondition = FT_ERROR.FT_BUFFER_SIZE;
                        // Throw exception
                        ErrorHandler(ftStatus, ftErrorCondition);
                    }

                    // Instantiate the array elements as FT_DEVICE_INFO_NODE
                    for (UInt32 i = 0; i < devcount; i++)
                    {
                        devicelist[i] = new FT_DEVICE_INFO_NODE();
                        // Call FT_GetDeviceInfoDetail
                        ftStatus = FT_GetDeviceInfoDetail(i, ref devicelist[i].Flags, ref devicelist[i].Type, ref devicelist[i].ID, ref devicelist[i].LocId, sernum, desc, ref devicelist[i].ftHandle);
                        // Convert byte arrays to strings
                        devicelist[i].SerialNumber = Encoding.ASCII.GetString(sernum);
                        devicelist[i].Description = Encoding.ASCII.GetString(desc);
                        // Trim strings to first occurrence of a null terminator character
                        devicelist[i].SerialNumber = devicelist[i].SerialNumber.Substring(0, devicelist[i].SerialNumber.IndexOf("\0"));
                        devicelist[i].Description = devicelist[i].Description.Substring(0, devicelist[i].Description.IndexOf("\0"));
                    }
                }
            }
            else
            {
                if (pFT_CreateDeviceInfoList == IntPtr.Zero)
                {
                    MessageBox.Show("Failed to load function FT_CreateDeviceInfoList.");
                }
                if (pFT_GetDeviceInfoDetail == IntPtr.Zero)
                {
                    MessageBox.Show("Failed to load function FT_GetDeviceInfoListDetail.");
                }
            }
            return ftStatus;
        }
Esempio n. 8
0
        public TBalancerGroup(ISettings settings)
        {
            uint numDevices;
              try {
            if (FTD2XX.FT_CreateDeviceInfoList(out numDevices) != FT_STATUS.FT_OK) {
              report.AppendLine("Status: FT_CreateDeviceInfoList failed");
              return;
            }
              } catch (DllNotFoundException) { return; }
            catch (ArgumentNullException) { return; }
            catch (EntryPointNotFoundException) { return; }
            catch (BadImageFormatException) { return; }

              FT_DEVICE_INFO_NODE[] info = new FT_DEVICE_INFO_NODE[numDevices];
              if (FTD2XX.FT_GetDeviceInfoList(info, ref numDevices) != FT_STATUS.FT_OK)
              {
            report.AppendLine("Status: FT_GetDeviceInfoList failed");
            return;
              }

              // make sure numDevices is not larger than the info array
              if (numDevices > info.Length)
            numDevices = (uint)info.Length;

              for (int i = 0; i < numDevices; i++) {
            report.Append("Device Index: ");
            report.AppendLine(i.ToString(CultureInfo.InvariantCulture));
            report.Append("Device Type: ");
            report.AppendLine(info[i].Type.ToString());

            // the T-Balancer always uses an FT232BM
            if (info[i].Type != FT_DEVICE.FT_DEVICE_232BM) {
              report.AppendLine("Status: Wrong device type");
              continue;
            }

            FT_HANDLE handle;
            FT_STATUS status = FTD2XX.FT_Open(i, out handle);
            if (status != FT_STATUS.FT_OK) {
              report.AppendLine("Open Status: " + status);
              continue;
            }

            FTD2XX.FT_SetBaudRate(handle, 19200);
            FTD2XX.FT_SetDataCharacteristics(handle, 8, 1, 0);
            FTD2XX.FT_SetFlowControl(handle, FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, 0x11,
              0x13);
            FTD2XX.FT_SetTimeouts(handle, 1000, 1000);
            FTD2XX.FT_Purge(handle, FT_PURGE.FT_PURGE_ALL);

            status = FTD2XX.Write(handle, new byte[] { 0x38 });
            if (status != FT_STATUS.FT_OK) {
              report.AppendLine("Write Status: " + status);
              FTD2XX.FT_Close(handle);
              continue;
            }

            bool isValid = false;
            byte protocolVersion = 0;

            int j = 0;
            while (FTD2XX.BytesToRead(handle) == 0 && j < 2) {
              Thread.Sleep(100);
              j++;
            }
            if (FTD2XX.BytesToRead(handle) > 0) {
              if (FTD2XX.ReadByte(handle) == TBalancer.STARTFLAG) {
            while (FTD2XX.BytesToRead(handle) < 284 && j < 5) {
              Thread.Sleep(100);
              j++;
            }
            int length = FTD2XX.BytesToRead(handle);
            if (length >= 284) {
              byte[] data = new byte[285];
              data[0] = TBalancer.STARTFLAG;
              for (int k = 1; k < data.Length; k++)
                data[k] = FTD2XX.ReadByte(handle);

              // check protocol version 2X (protocols seen: 2C, 2A, 28)
              isValid = (data[274] & 0xF0) == 0x20;
              protocolVersion = data[274];
              if (!isValid) {
                report.Append("Status: Wrong Protocol Version: 0x");
                report.AppendLine(
                  protocolVersion.ToString("X", CultureInfo.InvariantCulture));
              }
            } else {
              report.AppendLine("Status: Wrong Message Length: " + length);
            }
              } else {
            report.AppendLine("Status: Wrong Startflag");
              }
            } else {
              report.AppendLine("Status: No Response");
            }

            FTD2XX.FT_Purge(handle, FT_PURGE.FT_PURGE_ALL);
            FTD2XX.FT_Close(handle);

            if (isValid) {
              report.AppendLine("Status: OK");
              hardware.Add(new TBalancer(i, protocolVersion, settings));
            }

            if (i < numDevices - 1)
              report.AppendLine();
              }
        }