CogEthernetPort WriteSettings Method Cognex VisionPro
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.

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

public void WriteSettings(
	CogEthernetPortSettings settings
)
Exceptions

ExceptionCondition
ArgumentException The domain name must be either null, or the empty string.
Remarks

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").

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