Beispiel #1
0
        public static string[] GetComPortList()
        {
            List <string> comPorts = new List <string>();

            try
            {
                List <FTDeviceInfo> ftdiDevices = new List <FTDeviceInfo>();

                uint         deviceNumber;
                FTStatusCode ftStatus = Ftdi.FT_CreateDeviceInfoList(out deviceNumber);

                if (deviceNumber > 0)
                {
                    FTDeviceInfoNode[] devices = new FTDeviceInfoNode[deviceNumber];

                    for (int k = 0; k < deviceNumber; k++)
                    {
                        devices[k].SerialNumber = new byte[16];
                        devices[k].Description  = new byte[64];
                    }

                    ftStatus = Ftdi.FT_GetDeviceInfoList(devices, ref deviceNumber);

                    for (int k = 0; k < deviceNumber; k++)
                    {
                        FTDeviceInfo ftDeviceInfo = new FTDeviceInfo();
                        ftDeviceInfo.Flags = devices[k].Flags;
                        ftDeviceInfo.Type  = devices[k].Type;
                        ftDeviceInfo.ID    = devices[k].ID;
                        ftDeviceInfo.LocId = devices[k].LocId;

                        ftDeviceInfo.Description  = ASCIIEncoding.ASCII.GetString(devices[k].Description);
                        ftDeviceInfo.Description  = ftDeviceInfo.Description.Substring(0, ftDeviceInfo.Description.IndexOf("\0"));
                        ftDeviceInfo.SerialNumber = ASCIIEncoding.ASCII.GetString(devices[k].SerialNumber);
                        ftDeviceInfo.SerialNumber = ftDeviceInfo.SerialNumber.Substring(0, ftDeviceInfo.SerialNumber.IndexOf("\0"));

                        ftdiDevices.Add(ftDeviceInfo);
                    }

                    foreach (FTDeviceInfo ftdiDevice in ftdiDevices)
                    {
                        string comPort = Ftdi.GetComPortNumber(ftdiDevice.SerialNumber);
                        if (string.IsNullOrEmpty(comPort) == false)
                        {
                            comPorts.Add(comPort);
                        }
                    }
                }
            }
            catch (DllNotFoundException)
            {
            }

            comPorts.Sort();
            return(comPorts.ToArray());
        }
Beispiel #2
0
        public static string[] GetComPortList()
        {
            List<string> comPorts = new List<string>();
            try
            {
                List<FTDeviceInfo> ftdiDevices = new List<FTDeviceInfo>();

                uint deviceNumber;
                FTStatusCode ftStatus = Ftdi.FT_CreateDeviceInfoList(out deviceNumber);

                if (deviceNumber > 0)
                {
                    FTDeviceInfoNode[] devices = new FTDeviceInfoNode[deviceNumber];

                    for (int k = 0; k < deviceNumber; k++)
                    {
                        devices[k].SerialNumber = new byte[16];
                        devices[k].Description = new byte[64];
                    }

                    ftStatus = Ftdi.FT_GetDeviceInfoList(devices, ref deviceNumber);

                    for (int k = 0; k < deviceNumber; k++)
                    {
                        FTDeviceInfo ftDeviceInfo = new FTDeviceInfo();
                        ftDeviceInfo.Flags = devices[k].Flags;
                        ftDeviceInfo.Type = devices[k].Type;
                        ftDeviceInfo.ID = devices[k].ID;
                        ftDeviceInfo.LocId = devices[k].LocId;

                        ftDeviceInfo.Description = ASCIIEncoding.ASCII.GetString(devices[k].Description);
                        ftDeviceInfo.Description = ftDeviceInfo.Description.Substring(0, ftDeviceInfo.Description.IndexOf("\0"));
                        ftDeviceInfo.SerialNumber = ASCIIEncoding.ASCII.GetString(devices[k].SerialNumber);
                        ftDeviceInfo.SerialNumber = ftDeviceInfo.SerialNumber.Substring(0, ftDeviceInfo.SerialNumber.IndexOf("\0"));

                        ftdiDevices.Add(ftDeviceInfo);
                    }

                    foreach (FTDeviceInfo ftdiDevice in ftdiDevices)
                    {
                        string comPort = Ftdi.GetComPortNumber(ftdiDevice.SerialNumber);
                        if (string.IsNullOrEmpty(comPort) == false)
                        {
                            comPorts.Add(comPort);
                        }
                    }
                }
            }
            catch (DllNotFoundException)
            {
            }

            comPorts.Sort();
            return comPorts.ToArray();
        }