diff --git a/Giselle.Net.EtherNetIP.Test/ImplicitMessaging.cs b/Giselle.Net.EtherNetIP.Test/ImplicitMessaging.cs index 2216056..9af24b9 100644 --- a/Giselle.Net.EtherNetIP.Test/ImplicitMessaging.cs +++ b/Giselle.Net.EtherNetIP.Test/ImplicitMessaging.cs @@ -90,7 +90,7 @@ public static void Identify(ENIPSimpleClient client) public static ForwardOpenResult ForwardOpen(ENIPSimpleClient client) { var openOptions = new ForwardOpenOptions(); - openOptions.T_O_UDPPort = 2222; // Support alternate sending port + openOptions.T_O_UDPPort = 2222; // Support alternate port, Default is 2222 // T : Target // O : Originator diff --git a/Giselle.Net.EtherNetIP/CIP/CIPCodec.cs b/Giselle.Net.EtherNetIP/CIP/CIPCodec.cs index 2a09320..35db6b9 100644 --- a/Giselle.Net.EtherNetIP/CIP/CIPCodec.cs +++ b/Giselle.Net.EtherNetIP/CIP/CIPCodec.cs @@ -3,6 +3,7 @@ using System.IO; using System.Linq; using System.Net; +using System.Net.Sockets; using System.Text; using System.Threading.Tasks; @@ -65,8 +66,9 @@ public IEnumerable CreateForwardOpen(ForwardOpenOptions options) reqProcessor.WriteByte(options.TickTime); reqProcessor.WriteByte(options.TimeoutTicks); - var otConnectionIDReq = options.O_T_Assembly.ConnectionID != 0 ? options.O_T_Assembly.ConnectionID : (ushort)(this.NextConnectionID() + 0); - var toConnectionIDReq = options.T_O_Assembly.ConnectionID != 0 ? options.T_O_Assembly.ConnectionID : (ushort)(this.NextConnectionID() + 1); + var connectionID = this.NextConnectionID(); + var otConnectionIDReq = options.O_T_Assembly.ConnectionID != 0 ? options.O_T_Assembly.ConnectionID : (ushort)(connectionID + 0); + var toConnectionIDReq = options.T_O_Assembly.ConnectionID != 0 ? options.T_O_Assembly.ConnectionID : (ushort)(connectionID + 1); reqProcessor.WriteUInt(otConnectionIDReq); reqProcessor.WriteUInt(toConnectionIDReq); @@ -108,21 +110,20 @@ public IEnumerable CreateForwardOpen(ForwardOpenOptions options) connectionPath.Write(reqProcessor); yield return udRequest; - var requestIPV4EndPoint = new CommandItemEndPoint_T_O + var toEndPoint = new CommandItemEndPoint_T_O { EndPoint = new IPv4EndPoint() { Family = 2, Port = options.T_O_UDPPort } }; if (options.T_O_Assembly.ConnectionType == ConnectionType.Multicast) { - requestIPV4EndPoint.EndPoint.Address = ((int)GetMulticastAddress((uint)options.LocalAddress.ToIPv4Address())).ToIPv4Address(true); + toEndPoint.EndPoint.Address = ((int)GetMulticastAddress((uint)options.LocalAddress.ToIPv4Address())).ToIPv4Address(true); } else { - requestIPV4EndPoint.EndPoint.Address = IPAddress.Any; + toEndPoint.EndPoint.Address = IPAddress.Any; } - - yield return requestIPV4EndPoint; + yield return toEndPoint; } public ForwardOpenResult HandleForwardOpen(CommandItems response, ForwardOpenOptions options) diff --git a/Giselle.Net.EtherNetIP/CIP/ForwardOpenOptions.cs b/Giselle.Net.EtherNetIP/CIP/ForwardOpenOptions.cs index 7246b47..92fe884 100644 --- a/Giselle.Net.EtherNetIP/CIP/ForwardOpenOptions.cs +++ b/Giselle.Net.EtherNetIP/CIP/ForwardOpenOptions.cs @@ -16,6 +16,16 @@ public class ForwardOpenOptions : AbstractOptions /// public AssemblyObject T_O_Assembly { get; private set; } + /// + /// Originator -> Target, Output + /// Originator Side Port For Exchange Message + /// + public ushort O_T_UDPPort { get; set; } + /// + /// Target -> Originator, Input + /// Target Side Port For Exchange Message + /// Not Supported Yet, Fix Default Value (2222) + /// public ushort T_O_UDPPort { get; set; } public IPAddress LocalAddress { get; set; } @@ -24,6 +34,7 @@ public ForwardOpenOptions() this.O_T_Assembly = new AssemblyObject(); this.T_O_Assembly = new AssemblyObject(); + this.O_T_UDPPort = 2222; this.T_O_UDPPort = 2222; this.LocalAddress = IPAddress.Loopback; } @@ -34,6 +45,7 @@ public ForwardOpenOptions(ForwardOpenOptions other) this.O_T_Assembly = new AssemblyObject(other.O_T_Assembly); this.T_O_Assembly = new AssemblyObject(other.T_O_Assembly); + this.O_T_UDPPort = other.O_T_UDPPort; this.T_O_UDPPort = other.T_O_UDPPort; this.LocalAddress = other.LocalAddress; } diff --git a/Giselle.Net.EtherNetIP/ENIP/ENIPSimpleClient.cs b/Giselle.Net.EtherNetIP/ENIP/ENIPSimpleClient.cs index d4f377d..0db92d3 100644 --- a/Giselle.Net.EtherNetIP/ENIP/ENIPSimpleClient.cs +++ b/Giselle.Net.EtherNetIP/ENIP/ENIPSimpleClient.cs @@ -164,11 +164,14 @@ public ForwardOpenResult ForwardOpen(ForwardOpenOptions options) private UdpClient CreateImplictMessagingClient(ForwardOpenResult openResult) { + var udpClient = new UdpClient(); var options = openResult.Options; - var port = 2222; // Receiving port is fixed 2222, in my test + udpClient.Client.Bind(new IPEndPoint(options.LocalAddress, openResult.Options.T_O_UDPPort)); - var udpClient = new UdpClient(); - udpClient.Client.Bind(new IPEndPoint(options.LocalAddress, port)); + if (openResult.Options.O_T_Assembly.ConnectionType == ConnectionType.Multicast) + { + udpClient.JoinMulticastGroup(openResult.O_T_Address.Address, options.LocalAddress); + } if (openResult.Options.T_O_Assembly.ConnectionType == ConnectionType.Multicast) { @@ -218,7 +221,7 @@ private void RunImplicitSend() var udpClient = this.UdpClient; var result = this.LastForwardOpenResult; var options = result.Options; - var targetEndPoint = new IPEndPoint(((IPEndPoint)this.TcpClient.Client.RemoteEndPoint).Address, options.T_O_UDPPort); + var targetEndPoint = new IPEndPoint(((IPEndPoint)this.TcpClient.Client.RemoteEndPoint).Address, options.O_T_UDPPort); try {