Skip to content

Commit

Permalink
Add T_O_Port comments
Browse files Browse the repository at this point in the history
  • Loading branch information
gisellevonbingen committed Jul 26, 2023
1 parent 7a83555 commit 319a02c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Giselle.Net.EtherNetIP.Test/ImplicitMessaging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 8 additions & 7 deletions Giselle.Net.EtherNetIP/CIP/CIPCodec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

Expand Down Expand Up @@ -65,8 +66,9 @@ public IEnumerable<CommandItem> 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);

Expand Down Expand Up @@ -108,21 +110,20 @@ public IEnumerable<CommandItem> 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)
Expand Down
12 changes: 12 additions & 0 deletions Giselle.Net.EtherNetIP/CIP/ForwardOpenOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ public class ForwardOpenOptions : AbstractOptions
/// </summary>
public AssemblyObject T_O_Assembly { get; private set; }

/// <summary>
/// Originator -> Target, Output
/// Originator Side Port For Exchange Message
/// </summary>
public ushort O_T_UDPPort { get; set; }
/// <summary>
/// Target -> Originator, Input
/// Target Side Port For Exchange Message
/// Not Supported Yet, Fix Default Value (2222)
/// </summary>
public ushort T_O_UDPPort { get; set; }
public IPAddress LocalAddress { get; set; }

Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
11 changes: 7 additions & 4 deletions Giselle.Net.EtherNetIP/ENIP/ENIPSimpleClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
{
Expand Down

0 comments on commit 319a02c

Please sign in to comment.