chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//            Copyright Keysight Technologies 2012-2019
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;
 
namespace OpenTap
{
    /// <summary>
    /// Object representing a port on an instrument or DUT. Ports are connected with <see cref="Connection"/> objects.
    /// </summary>
    public class Port : IConstResourceProperty
    {
        /// <summary>
        /// The device (usually an <see cref="Instrument"/> or <see cref="Dut"/>) on which this port exists.
        /// </summary>
        [XmlIgnore] // avoid potential cycle in XML.
        public IResource Device { get; private set; }
 
        /// <summary>
        /// List of <see cref="Connection"/>s connected to this port.
        /// </summary>
        public IEnumerable<Connection> Connections => ConnectionSettings.Current.Where(con => con.HasPort(this));
 
        IEnumerable<Connection> ActiveConnections => Connections.Where(con => con.IsActive);
            
 
        /// <summary>
        /// The name of this port. (Should be unique among <see cref="Port"/> objects on the same device/resource).
        /// </summary>
        public string Name { get; internal set; }
 
        /// <summary>
        /// Initializes a new instance of the <see cref="Port"/> class.
        /// </summary>
        public Port(IResource device, string name)
        {
            Device = device;
            Name = name;
        }
 
        /// <summary>
        /// Returns a list of the ports that this port has connections to.
        /// </summary>
        public IEnumerable<Port> GetConnectedPorts()
        {
            return Connections.SelectMany(c => new[] {c.Port1, c.Port2}).Where(p => p != this && p != null);
        }
 
        /// <summary>
        /// Returns a list of the ports that this port has connections to and that are active.
        /// </summary>
        public IEnumerable<Port> GetActiveConnectedPorts()
        {
            return ActiveConnections
                .SelectMany(c => new[] {c.Port1, c.Port2})
                .Where(p => p != this && p != null);
        }
 
        /// <summary>
        /// Returns a list of connections from this port to a specified device.
        /// </summary>
        public IEnumerable<Connection> GetConnectionsTo(IResource device)
        {
            if (device == null) throw new ArgumentNullException(nameof(device));
            return Connections.Where(x =>
                x?.Port1?.Device == device || x?.Port2?.Device == device || (x?.Via?.Any(v => v.Device == device) ??
                false));
        }
 
        /// <summary>
        /// Returns a list of connections from this port to a specified port.
        /// </summary>
        public IEnumerable<Connection> GetConnectionsTo(Port otherPort)
        {
            return Connections.Where(c => c.HasPort(otherPort));
        }
 
        /// <summary>
        /// Returns a string describing this port.
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return $"{Name} on {(Device != null ? Device.Name : "<NULL>")}";
        }
    }
 
    /// <summary>
    /// An unidirectional port that is always an input.
    /// </summary>
    public class InputPort : Port
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="InputPort"/> class.
        /// </summary>
        public InputPort(IResource device, string name) : base(device, name)
        {
        }
    }
 
    /// <summary>
    /// An unidirectional port that is always an output.
    /// </summary>
    public class OutputPort : Port
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="OutputPort"/> class.
        /// </summary>
        public OutputPort(IResource device, string name) : base(device, name)
        {
        }
    }
}