Skip to main content

Scenario: Virtual Control with VLAN

Requirements

Configuring the switch

Important

If you configure your device incorrectly, then you may lose access to the device.

  1. Connect the adapter (where the VLANs should be defined) to the switch. In this example, two VLAN interfaces are added to the following adapter:

    enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
        link/ether f0:b2:b9:11:b8:b3 brd ff:ff:ff:ff:ff:ff
        inet 192.168.2.2/24 brd 192.168.2.255 scope global noprefixroute enp1s0
           valid_lft forever preferred_lft forever
        inet6 fe80::2e96:178c:586a:b97b/64 scope link noprefixroute
           valid_lft forever preferred_lft forever
  2. Open the configuration interface of the switch. For instructions about how to open the interface and the credentials, refer to the instructions of the vendor.

  3. After you log in, open the configuration page for VLAN. If your switch supports different protocols, then select 802.1Q VLAN.

  4. On the Port Config page, configure the Link Type of the previously connected port as a Trunk.

  5. Connect all fieldbus devices to the switch.

  6. On the VLAN Config page, add two new VLANs. In this example, you are working with VLAN IDs 100 and 200. Add the previously connected port and configured as a Trunk to each VLAN. Add the port of a fieldbus device to each VLAN.

In the following sample configuration, the connection from the controller to the switch was established on Port 14. The Link Type of this port has been configured as a Trunk. A fieldbus device is connected each to Port 11 and to Port 12.

VLAN ID

Description

Ports

100

VLAN 100

11, 14

200

VLAN 200

12, 14

Configuring the VLAN interfaces

Before you make permanent changes to your network configuration, you should test the settings and modify them to your needs.

  1. Create a new adapter of type VLAN.

    The command has the following format:

    sudo ip link add link <physical_adapter> name <virtual_adapter_name> type vlan id <vlan_id>

    The convention is to append the VLAN ID to the name of the virtual adapter. In the case of the adapter and switch configuration shown above, the command looks like this:

    sudo ip link add link enp1s0 name enp1s0.100 type vlan id 100

    Replace enp1s0 and the VLAN ID with the values on your system.

    Now when you run ip a, you should see something like the following output:

    enp1s0.100@enp1s0: <BROADCAST,MULTICAST,PROMISC> mtu 1500 qdisc noop state DOWN group default qlen 1000
  2. Execute this command for the second VLAN as well.

Testing the configuration

  1. Open the CODESYS Control SL Deploy Tool and establish a connection to your controller.

    For more information, see: Establishing a Connection.

  2. Switch to the Operation tab and select the vPLC1 instance.

  3. In the configuration view on the right side, click the Nic line and on the right edge click the _visu_icon_three_dots.png button.

    The Nic configuration dialog opens.

  4. In the list of available adapters, click enp1s0.100 and select the Map to Instance option.

  5. If necessary, configure an IP address and network mask. The network mask has the format of the CIDR notation (example: 24).

    For more information, see: https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing

  6. Click the OK and Save buttons.

  7. Select the vPLC2 instance and perform Steps 3-6 with the enp1s0.200 adapter.

  8. Start the instances and a gateway on your controller.

    For more information, see: Starting the Runtime System

  9. Load a small application onto each of the two instances to check the functionality of the network. For more information, see: Downloading and Starting the CODESYS Application on the Controller

Persisting the adapter configuration

If your configuration is correct, you still need to save it persistently. For instructions about how to persistently save the configuration of your switch, see the instructions of the vendor. The configuration of the two instances vPLC1 and vPLC2 are already persistent. Here you only need to set the Autostart setting to Yes.

The following steps describe how to configure the two VLAN adapters on your controller so that they are available when the system is booted.

Important

These settings can be potentially harmful to your system.

  • Create a backup of all important files.

  • Make a copy of all files which are modified beforehand so that you can easily undo changes.

  1. Open /etc/network/interfaces in an editor (example: sudo nano /etc/network/interfaces).

  2. Add the following lines (modify the adapter names and VLAN IDs to your controller):

    # enp1s0.100 vlan (vPLC1)
    auto enp1s0.100
    iface enp1s0.100 inet dhcp
        vlan-raw-device enp1s0
    # enp1s0.200 vlan (vPLC2)
    auto enp1s0.200
    iface enp1s0.200 inet dhcp
        vlan-raw-device enp1s0

    For more information about this file, see: https://wiki.debian.org/NetworkConfiguration

  3. Restart your system or execute the command sudo systemctl restart networking for the changes to apply.

Note

This method of configuration was tested on Debian 12. You may need to select a different method for your device or operating system in order to save the configuration persistently.

Other examples are netplan (https://netplan.readthedocs.io/en/stable/structure-id/) or NetworkManager (https://networkmanager.dev/). The appropriate instructions for your controller can be found in the respective documentation.