## S7 PLC output

The S7 output (1500, 1200, 400 and 300) generates code for SIMATIC controllers. Totally Integrated Automation Portal (TIA Portal) is an IDE for the development and testing of SIMATIC controllers. Using the S7 output, the generated PLC code can be imported in TIA Portal. This allows for tight integration and smooth development cycles when implementing controllers on SIMATIC controllers.

### Generated output

The CIF PLC code generator can generate code files that can be imported in TIA Portal (see S7 usage).

The following option restrictions apply:

- The Convert enumerations option must be set to Automatically decide how to handle enumerations, Convert enumerations to integers or Convert enumerations to constants.

The type of integer and real data types to use depends on the PLC integer type size and PLC real type size options. If Automatic is selected, the following data types are used:

S7 type | Integer type | Real type
--- | --- | ---
S7-1500 | `DINT` (32 bit) | `LREAL` (64 bit)
S7-1200 | `DINT` (32 bit) | `LREAL` (64 bit)
S7-400 | `DINT` (32 bit) | `REAL` (32 bit)
S7-300 | `DINT` (32 bit) | `REAL` (32 bit)

The PLC task name, PLC task cycle time, PLC task priority, PLC project name, PLC configuration name, and PLC resource name options have no effect for this output type.

### Standard compliance

In general, the CIF PLC code generator follows the IEC 61131-3 PLC standard and does not use vendor-specific extensions. For the Siemens S7 targets however, there are some differences. For instance, the persistent variables are stored in a separate database file, named `DB`, and all references to persistent variables are prefixed with `"DB".`. Also, global variables and constants are stored in a tag table, instead of a global variable list. In such cases the output is not compliant with the IEC 61131-3 standard.

### S7 usage

Below, the basic usage of generated S7 code with TIA Portal is explained. The steps assume that TIA Portal v15 is used. If you use a different version of TIA Portal, the steps may be slightly different.

Follow these steps to create a project in TIA Portal, import PLC code in it, and use the PLC code to control the hardware:

- Start TIA Portal.

- Create a new S7 project:

  - In the Portal view, click Create new project.

  - In the Create new project window, choose a project name and enter it in the Project name box.

  - For Path, select the directory in which the directory for the project should be created.

  - Click Create to create the project.

  - Switch to the Project view.

- Add the PLC device:

  - Unfold the project and double click Add new device.

  - Unfold the device type category (that is, S7-1500, S7-1200, S7-400 or S7-300).

  - Select the device with the correct article number.

  - Click OK.

- Import the generated PLC code (PLC tags and constants):

  - Double click Default tag table inside the PLC tags folder.

  - For each generated `.xml` file (e.g., CONSTANTS.xml, INPUTS.xml, OUTPUTS.xml), do the following:

    - Click the Import button.

    - Select the file.

    - Make sure Tags and Constants are both selected.

    - Click OK.

- Import the generated PLC code (Program blocks):

  - Unfold the External source files folder inside the device folder.

  - Double click Add new external files.

  - Select all generated `.scl`, `.udt` and `.db` files, by selecting the first (top) file, and then selecting the last (bottom) file while holding the `Shift` key.

  - Click the Open button.

  - Select all imported files (using `Shift` as above), right-click one of them and select Generate blocks from source.

    - If asked whether it is OK that Blocks can be overwritten, click the OK button.

    - Ignore any warnings or errors.

- Compile the project:

  - Right-click Program blocks and select Compile Software (rebuild all).

  - If there are warnings or errors:

    - Compile again.

    - If any compile errors remain, repeat the steps above.

    - If errors about timers remain:

      - Sometimes, timers are not imported correctly. In that case, compilation fails. To add the timers manually, do the following.

      - Open MAIN inside the Program blocks folder.

      - Type `TON` and click `Enter`. The Call options window pops up.

      - In the Call options window, for each timer, enter its name in the Name box and click OK.

      - Delete the generated lines of code.

      - Compile the project again.

    - If errors about tuples remain, the program blocks need to be created in the right order, based on tuple dependencies:

      - Generate the program blocks for the structs of the tuple types from the `.udt` files first, and create program blocks for the `.scl` and `.db` files last.

      - Generate the program blocks for the tuples in the right order. If one `.udt` file uses a type from another tuple struct of another `.udt` file, the program block for the former needs to be created after the program block for the latter. The PLC code generator writes a text file `derived_type_dependencies.txt` that contains the dependency information between structs, and from structs to enumerations if the latter types are used in a struct.

- Download PLC code to device:

  - Click the Download to device icon in the task bar. The Extended download to device window pops up.

  - Click Start search.

  - When a device has been found, click Load. The Load preview window pops up.

  - Depending on the state of the PLC (idle, running, runtime error, …​), TIA Portal may require you to take certain actions. Consult the TIA Portal documentation for further information.

  - Click Load.

  - Change the Start modules action from No action to Start module.

  - Click Finish.

If a (simulated) PLC indicates a System Fault (SF), try to alter the memory allocation properties (may only apply to S7-400):

- Open the Device Configuration for that PLC.

- Select the CPU, e.g. CPU_1, by clicking on it.

- Click the Properties button to show the properties of the selected CPU.

- On the General tab, select the Memory category.

- Increase the Number of bytes property for Priority class number 1.

- If needed, also increase the Bytes of max.

To debug the PLC code:

- Click Go online in the task bar.

- The values of various variables can now be inspected.

  - The values of input and output variables can be seen by opening Default tag table inside the PLC tags folder. Click the Monitor all icon. The actual values appear in the Monitor value column.

  - The values of state variables can be seen by opening DB under Program blocks. Click the Monitor all icon. Unfold the Static item. The actual values appear in the Monitor value column.

To stop the PLC:

- While online, click STOP in the CPU operation panel in the Testing tab.

- Click Go offline in the task bar.

To reset the PLC and start from the initial state, with the same code and the same configuration:

- While online, click MRES in the CPU operation panel in the Testing tab. The Online & diagnostics window appears.

- Click Yes.

- If the PLC was in run mode, a new Online & diagnostics window appears. Click Yes.

- Click Go online in the task bar.

- Start the PLC by clicking RUN in the CPU operation panel in the Testing tab.

- The PLC is now running, and you can use the system.