CogEthernetPort ClassCognex VisionPro

The CogEthernetPort class supports configuring an Ethernet port present on a device.

Use the CreateEthernetPort  method of CogEthernetPortAccess to factory/initialize instances of the CogEthernetPort class.

Ethernet settings are written to the Comm Card using WriteSettings(CogEthernetPortSettings). These settings are stored in flash memory directly on the Comm Card.

When a VisionPro application first connects to a Comm Card, the Comm Card automatically attempts to bring the Ethernet port interface "UP" using the stored settings. Later, when the VisionPro application exits, the Comm Card will automatically bring the Interface "DOWN", removing the card from the network.

Both the Link (physical connection present) and the Interface (IP settings activated) need to be in the "UP" state for the Ethernet port to participate in an IP network. When the link or interface is "DOWN", the Ethernet port cannot communicate over an IP network.

Use IsLinkUp to see if the Link is in the "UP" state. When the Link is up, the Ethenet port is physically connected to a network and can physically transmit and receive data.

Use IsInterfaceUp to see if the Interface is in the "UP" state. When the Interface is up, the Ethernet port has been activated with IP network settings and can participate in an IP network.

Use WriteSettings(CogEthernetPortSettings) and ReadSettings  to read and write network settings to the Comm Card.

Use BringInterfaceUpAsync  and BringInterfaceDownAsync  to manually bring the interface "UP" and "DOWN" using the settings stored on the Card.

Use ReadActiveSettings  to read the currently active settings while the Interface is "UP". The currently active settings may be different from the settings stored on the card in cases where the card is configured for DHCP, or if something (i.e. PROFINET) changes the active settings without also writing them to the storage location on the card.

Note that CogEthernetPort does not have exclusive control over the Ethernet port hardware. The Ethernet port interface may already be "UP" when a CogEthernetPort is created. Furthermore, the state of the Ethernet interface may report changes and events that were _not_ initiated from user calls into its own properties and methods.

Note also that the lifetime of this object is not related to the state ("UP" or "DOWN") of the Ethernet port interface. Disposing the CogEthernetPort object will not affect the state of actual Ethernet settings running on the card (even though exiting the _process_ will bring the network interface "DOWN").

Inheritance Hierarchy

System Object
  System MarshalByRefObject
    Cognex.VisionPro.Implementation CogObjectBase
      Cognex.VisionPro.Implementation CogChangedEventBase
        Cognex.VisionPro.Comm CogEthernetPort

Namespace: Cognex.VisionPro.Comm
Assembly: Cognex.VisionPro.Comm (in Cognex.VisionPro.Comm.dll) Version: 65.1.0.0
Syntax

public class CogEthernetPort : CogChangedEventBase, 
	IDisposable

The CogEthernetPort type exposes the following members.

Methods

  NameDescription
Public methodBringInterfaceDownAsync
Manually bring down the Ethernet port interface.

This method returns immediately. You can wait on the returned task to be notified when the interface actually comes down. The task completes the next time the interface comes down. Note that if the interface is already down the task will never complete. This makes it important to include a timeout when waiting on the returned task.

Public methodBringInterfaceUpAsync
Manually bring up the Ethernet port interface using the Comm Card's stored network settings.

This method returns immediately. You can wait on the returned task to be notified when the interface actually comes up. The task completes the next time the interface comes up. Note that if the interface never comes up the task will never complete. This may happen if DHCP is unable to resolve the settings. This makes it important to include a timeout when waiting on the returned task.

Public methodCreateObjRef
Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.
(Inherited from MarshalByRefObject.)
Public methodDispose 
Protected methodDispose(Boolean)
Public methodEquals
Determines whether the specified Object is equal to the current Object.
(Inherited from Object.)
Protected methodFinalize (Overrides Object Finalize .)
Protected methodGetAttributes
Implements the corresponding member of the ICustomTypeDescriptor interface. May be overridden in derived classes to provide custom type information.
(Inherited from CogObjectBase.)
Protected methodGetClassName
Implements the corresponding member of the ICustomTypeDescriptor interface. May be overridden in derived classes to provide custom type information.
(Inherited from CogObjectBase.)
Protected methodGetComponentName
Implements the corresponding member of the ICustomTypeDescriptor interface. May be overridden in derived classes to provide custom type information.
(Inherited from CogObjectBase.)
Protected methodGetConverter
Implements the corresponding member of the ICustomTypeDescriptor interface. May be overridden in derived classes to provide custom type information.
(Inherited from CogObjectBase.)
Protected methodGetDefaultEvent
Implements the corresponding member of the ICustomTypeDescriptor interface. May be overridden in derived classes to provide custom type information.
(Inherited from CogObjectBase.)
Protected methodGetDefaultProperty
Implements the corresponding member of the ICustomTypeDescriptor interface. May be overridden in derived classes to provide custom type information.
(Inherited from CogObjectBase.)
Protected methodGetEditor
Implements the corresponding member of the ICustomTypeDescriptor interface. May be overridden in derived classes to provide custom type information.
(Inherited from CogObjectBase.)
Protected methodGetEvents 
Implements the corresponding member of the ICustomTypeDescriptor interface. May be overridden in derived classes to provide custom type information.
(Inherited from CogObjectBase.)
Protected methodGetEvents( Attribute )
Implements the corresponding member of the ICustomTypeDescriptor interface. May be overridden in derived classes to provide custom type information.
(Inherited from CogObjectBase.)
Public methodGetHashCode
Serves as a hash function for a particular type.
(Inherited from Object.)
Public methodGetLifetimeService
Retrieves the current lifetime service object that controls the lifetime policy for this instance.
(Inherited from MarshalByRefObject.)
Protected methodGetProperties 
Implements the corresponding member of the ICustomTypeDescriptor interface. May be overridden in derived classes to provide custom type information.
(Inherited from CogObjectBase.)
Protected methodGetProperties( Attribute )
Implements the corresponding member of the ICustomTypeDescriptor interface. May be overridden in derived classes to provide custom type information.
(Inherited from CogObjectBase.)
Protected methodGetPropertyOwner
Implements the corresponding member of the ICustomTypeDescriptor interface. May be overridden in derived classes to provide custom type information.
(Inherited from CogObjectBase.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodInitializeLifetimeService
Obtains a lifetime service object to control the lifetime policy for this instance.
(Inherited from MarshalByRefObject.)
Protected methodMemberwiseClone 
Creates a shallow copy of the current Object.
(Inherited from Object.)
Protected methodMemberwiseClone(Boolean)
Creates a shallow copy of the current MarshalByRefObject object.
(Inherited from MarshalByRefObject.)
Protected methodOnChanged(Int64)
This method should be called internally whenever the object's state may have changed.
(Inherited from CogChangedEventBase.)
Protected methodOnChanged(CogChangedEventArgs)
This method may be called internally whenever a derived object's state may have changed and the derived object expresses this change via a derived CogChangedEventArgs class.
(Inherited from CogChangedEventBase.)
Public methodReadActiveSettings
Returns the "active" Ethernet port settings from the hardware.

Public methodReadSettings
Returns the Ethernet port settings stored on the hardware.
Public methodResumeAndRaiseChangedEvent
Re-enables raising of the Changed event after SuspendChangedEvent has been called, and raises the Changed event if the ChangedEventSuspended count is reduced to zero and any changes were made while events were suspended. Must be called once for each call to SuspendChangedEvent.
(Inherited from CogChangedEventBase.)
Public methodSuspendChangedEvent
Temporarily suspends the raising of the Changed event. May be called more than once, and a corresponding call to ResumeAndRaiseChangedEvent must be made for each call to SuspendChangedEvent.
(Inherited from CogChangedEventBase.)
Public methodToString
Returns a String that represents the current Object.
(Inherited from Object.)
Public methodWriteSettings
Writes the Ethernet port settings to storage on the hardware.

You must call BringInterfaceDownAsync  followed by BringInterfaceUpAsync  (or restart your application) for the new settings to actually take effect on the network.

Top
Fields

  NameDescription
Public fieldStatic memberSfIsInterfaceUp
This bit will be set in the EventArgs of a Changed event every time the value returned by IsInterfaceUp may have changed.
Public fieldStatic memberSfIsLinkUp
This bit will be set in the EventArgs of a Changed event every time the value returned by IsLinkUp may have changed.
Protected fieldStatic memberSfNextSf
Top
Properties

  NameDescription
Public propertyChangedEventSuspended
If nonzero, indicates that the raising of the Changed event has been suspended. This value is incremented when SuspendChangedEvent is called and decremented when ResumeAndRaiseChangedEvent is called.
(Inherited from CogChangedEventBase.)
Public propertyIndex
Gets the index of the physical Ethernet port.
Public propertyIsInterfaceUp
Gets the state of the Ethernet port interface. An interface is "UP" if it has been successfully configured with an IP address, sub-net mask, etc...
Public propertyIsLinkUp
Gets the state of the Ethernet port link. A link is "up" if the Ethernet port is electrically connected.
Public propertyMacAddress
Gets the MAC address of the Ethernet port.
Public propertyStateFlags
Returns the complete set of state flags supported on this object. The flags may be indexed by name as shown in the following C# code snippet: if (changedObject.StateFlags["Color"] & eventArgs.StateFlags) { ... }
(Inherited from CogChangedEventBase.)
Top
Events

  NameDescription
Public eventChanged
This event is raised when one or more parts of the object's state may have changed.
(Inherited from CogChangedEventBase.)
Public eventInterfaceUp
This event is raised whenever the interface comes up. The event args contain the currently active settings.
Top
Examples

using Cognex.VisionPro;
using Cognex.VisionPro.Comm;

// Change the IP address on a Comm Card Ethernet port. 
public void ChangeIPAddress(
  string newAddress,
  string newSubnet,
  string newDefaultGateway)
{
CogCommCards commCardCollection = new CogCommCards();

Console.WriteLine("Found: {0} Comm Cards", commCardCollection.Count);
if (commCardCollection.Count == 0) return;

// Get a reference to the the comm card object.
CogCommCard card = commCardCollection[0];

Console.WriteLine("Name: {0}", card.Name);
Console.WriteLine("Serial: {0}", card.SerialNumber);

Console.WriteLine("NumEthernetPorts: {0}", card.EthernetPortAccess.NumPorts);
if (card.EthernetPortAccess.NumPorts == 0) return;

// Create a software object to interact with Ethernet port.
CogEthernetPort etherPort = card.EthernetPortAccess.CreateEthernetPort(0);

Console.WriteLine("Current Ethernet Port Settings:");
Console.WriteLine("  link up: {0}", etherPort.IsLinkUp);
Console.WriteLine("  iface up: {0}", etherPort.IsInterfaceUp);

// Read the persistant settings from the Comm Card.
CogEthernetPortSettings settings = etherPort.ReadSettings();

Console.WriteLine("  ip: {0}", settings.IPAddress);
Console.WriteLine("  subnet: {0}", settings.SubnetMask);
Console.WriteLine("  default gateway: {0}", settings.DefaultGateway);
Console.WriteLine("  host name: {0}", settings.HostName);
Console.WriteLine("  domain name: {0}", settings.DomainName);
Console.WriteLine("  dhcpEnable: {0}", settings.DHCPEnable);

if (etherPort.IsInterfaceUp)
{
  Console.WriteLine("Bringing Interface DOWN...");

  bool timedOut = !etherPort.BringInterfaceDownAsync().Wait(2000);
  if (timedOut)
  {
    Console.WriteLine("Bringing Interface Down TIMED OUT!!");
    return;
  }
  Console.WriteLine("The Interface is DOWN.");
}

Console.WriteLine("Writing new IP address to Comm Card.");

// Change the settings to the new ip address.
settings.IPAddress = IPAddress.Parse(newAddress);
settings.SubnetMask = IPAddress.Parse(newSubnet);
settings.DefaultGateway = IPAddress.Parse(newDefaultGateway);
settings.HostName = "hostname";
settings.DomainName = "";
settings.DHCPEnable = false;

// Write the new settings to the card.
etherPort.WriteSettings(settings);

// Bring up the interface with the new IP address. 
if (!etherPort.IsInterfaceUp)
{
  Console.WriteLine("Bringing Interface UP...");

  bool timedOut = !etherPort.BringInterfaceUpAsync().Wait(2000);
  if (timedOut)
  {
    Console.WriteLine("Bringing Interface UP TIMED OUT!!");
    return;
  }
  Console.WriteLine("The Interface is UP.");
}

Console.WriteLine("New Ethernet Port Settings:");
Console.WriteLine("  link up: {0}", etherPort.IsLinkUp);
Console.WriteLine("  iface up: {0}", etherPort.IsInterfaceUp);

// Read the current settings from the Comm Card.
settings = etherPort.ReadActiveSettings();

Console.WriteLine("  ip: {0}", settings.IPAddress);
Console.WriteLine("  subnet: {0}", settings.SubnetMask);
Console.WriteLine("  default gateway: {0}", settings.DefaultGateway);
Console.WriteLine("  host name: {0}", settings.HostName);
Console.WriteLine("  domain name: {0}", settings.DomainName);
Console.WriteLine("  dhcpEnable: {0}", settings.DHCPEnable);
}


------
Output
------

Found: 1 Comm Cards
Name: Cognex Communications Card 24C
Serial: 1A1410XN002183
NumEthernetPorts: 1
Current Ethernet Port Settings:
  link up: False
  iface up: True
  ip: 192.168.1.42
  subnet: 255.255.0.0 
  default gateway: 192.168.1.3
  host name: HostName
  domain name: 
  dhcpEnable: False
Bringing Interface DOWN...
The Interface is DOWN.
Writing new IP address to Comm Card.
Bringing Interface UP...
The Interface is UP.
New Ethernet Port Settings:
  link up: False
  iface up: True
  ip: 192.168.1.100
  subnet: 255.255.255.255 
  default gateway: 192.168.1.1
  host name: hostname
  domain name: 
  dhcpEnable: False
See Also