コード例 #1
0
        private static void SaveEsi(EtherCATInfo etherCATInfo, string esiFileName)
        {
            var xmlSerializer = new XmlSerializer(typeof(EtherCATInfo));

            lock (_lock)
            {
                using (StreamWriter streamWriter = new StreamWriter(esiFileName))
                {
                    try
                    {
                        xmlSerializer.Serialize(streamWriter, etherCATInfo);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception($"Could not write file {esiFileName}. Reason: {ex.Message}");
                    }
                }
            }
        }
コード例 #2
0
        private static (EtherCATInfo etherCATInfo, EtherCATInfoDescriptionsDevice device, EtherCATInfoDescriptionsGroup group) TryFindDevice(List <EtherCATInfo> etherCATInfos, uint manufacturer, uint productCode, uint revision)
        {
            EtherCATInfo info = null;
            EtherCATInfoDescriptionsDevice device = null;

            foreach (var currentInfo in etherCATInfos)
            {
                var vendorId = (uint)EsiUtilities.ParseHexDecString(currentInfo.Vendor.Id);

                if (vendorId != manufacturer)
                {
                    continue;
                }

                device = currentInfo.Descriptions.Devices.FirstOrDefault(currentDevice =>
                {
                    var found = !string.IsNullOrWhiteSpace(currentDevice.Type.ProductCode) &&
                                !string.IsNullOrWhiteSpace(currentDevice.Type.RevisionNo) &&
                                (int)EsiUtilities.ParseHexDecString(currentDevice.Type.ProductCode) == productCode &&
                                (int)EsiUtilities.ParseHexDecString(currentDevice.Type.RevisionNo) == revision;

                    if (found)
                    {
                        info = currentInfo;
                    }

                    return(found);
                });

                if (device != null)
                {
                    break;
                }
            }

            // try to find old revision
            if (device == null)
            {
                etherCATInfos.ToList().ForEach(currentInfo =>
                {
                    device = currentInfo.Descriptions.Devices.Where(currentDevice =>
                    {
                        var found = !string.IsNullOrWhiteSpace(currentDevice.Type.ProductCode) &&
                                    !string.IsNullOrWhiteSpace(currentDevice.Type.RevisionNo) &&
                                    (int)EsiUtilities.ParseHexDecString(currentDevice.Type.ProductCode) == productCode;

                        if (found)
                        {
                            info = currentInfo;
                        }

                        return(found);
                    }).OrderBy(currentDevice => currentDevice.Type.RevisionNo).LastOrDefault();
                });

                // return without success
                if (device == null)
                {
                    return(null, null, null);
                }
            }

            // find group
            if (info == null)
            {
                throw new Exception($"ESI entry for group type '{device}' not found.");
            }

            var group = info.Descriptions.Groups.FirstOrDefault(currentGroup => currentGroup.Type == device.GroupType);

            if (group == null)
            {
                throw new Exception($"ESI entry for group type '{device}' not found.");
            }

            return(info, device, group);
        }