Generating Tables within Panels

This page outlines a straightforward method for managing a "Check Details" table within WinCC OA. While this example demonstrates basic functionality, it's important to remember that it should not be used for storing sensitive or critical information due to its basic nature and the use of text files for data persistence.

Understanding the Approach

The method provided here is intended for simple applications where complex data handling and high security are not paramount. The data is stored in a plain text file, which inherently does not offer any encryption or protection against unauthorized access. Therefore, it's crucial to ensure that:

  • The text file is stored securely, with access permissions set appropriately.
  • Regular backups are made to prevent data loss.
  • Any sensitive data is encrypted or handled by a more secure method.

Graphical Considerations

While the code snippets provided handle the logic for managing the table's data, setting up the actual table in terms of rows, columns, and appearance must be done separately within the WinCC OA GEDI. Ensure that the graphical table object is configured correctly to match the expectations of the code.

This example of how to manage a simple table in WinCC OA that logs equipment details such as IP addresses, ports,voltages, sequences, anything that you want really is going to be a simple "Check Details" Table with the ability to read from and write to a text file.

Initializing the Table

To initialize the table and set up the headers:


    void initialize() {
        // Assuming 'tableExample' is the name of your table shape in the panel
        shape tableShape = getShape("tableExample");
        tableShape.namedColumnHeader("#1", "Equipment");
        tableShape.namedColumnHeader("#2", "Port");
        tableShape.namedColumnHeader("#3", "IP");
        PopulateTableFromFile();
    }
    

Adding a Row to the Table

To add a new row to the table with IP, port, and equipment data:


    void AddRowToTable(string ip, string port, string equipment) {
        shape tableShape = getShape("tableExample");
        tableShape.appendLine("#3", ip, "#2", port, "#1", equipment);
    }
    

Populating Table from File

To populate the table from a file, reading each line and adding it as a row:


    void PopulateTableFromFile() {
        // File path should be relative to project directory for portability
        string filePath = "data/IPPorts.txt";
        file f = fopen(filePath, "a+");
        if (f)
        {
            shape tableShape = getShape("tableExample");

            tableShape.namedColumnHeader("#1", "Equipo");
            tableShape.namedColumnHeader("#2", "Puerto");
            tableShape.namedColumnHeader("#3", "IP");
            tableShape.deleteAllLines();

            bytesRead = fscanf(f, "%[^\n]\n", line);

            while (bytesRead > 0)
            {
                string ip, port, equipo;

                bytesRead = fscanf(f, "%s %s %[^\n]\n", ip, port, equipo);

                if (bytesRead == 3)
                {
                    AddRowToTable(ip, port, equipo);
                }
            }

            fclose(f);
        }
        else
        {
            DebugN("Error opening the file.");
        }
    }
    

Saving Changes to File

To save changes made to the table back to the file:


    void SaveTableToFile() {
        shape tableShape = getShape("tableExample");
        string filePath = "data/IPPorts.txt";
        tableShape.writeToFile(filePath, TABLE_WRITE_ALL_COLUMNS | TABLE_WRITE_COLUMN_HEADER, " ");
    }
    

Remember, this guide focuses on the data logic behind the table. The graphical user interface, including the table's rows, columns, and overall layout, will need to be set up in the GEDI environment of WinCC OA. This separation allows for flexibility in designing the user interface while maintaining a simple backend logic for data operations.

Note: File operations in WinCC OA are similar to those in C++ using the standard C library. Use file functions like fopen, fgets, and fclose to manage file I/O.