Character Case Conversion with SCL Language for Siemens S7-1200 PLCs

 To perform character case conversion using SCL (Structured Control Language), one needs to write the corresponding logic. Below is a detailed example code demonstrating how to convert the case of characters in an input string.

Assuming we want to convert a string containing up to 20 characters (this depends on your actual requirements and can be adjusted in length as needed).

Overview of Steps:

  1. Iterate through each character of the input string.
  2. Check if the character is an uppercase letter (A-Z).
  3. If it is an uppercase letter, convert it to a lowercase letter (a-z).
  4. Check if the character is a lowercase letter (a-z).
  5. If it is a lowercase letter, convert it to an uppercase letter (A-Z).
  6. Non-alphabetic characters remain unchanged.

```scl FUNCTION_BLOCK FB_CharCaseConverter VAR_INPUT InputString : ARRAY[1..20] OF CHAR; // Input string, up to 20 characters END_VAR VAR_OUTPUT OutputString : ARRAY[1..20] OF CHAR; // Output string, up to 20 characters END_VAR VAR i : INT; // Loop variable END_VAR BEGIN // Initialize the output string to empty spaces FOR i := 1 TO 20 DO OutputString[i] := ' '; END_FOR; // Iterate through each character of the input string FOR i := 1 TO LEN(InputString) DO CASE InputString[i] OF // Check and convert uppercase letters (A-Z) 'A'..'Z': OutputString[i] := CHAR_TO_BYTE(BYTE_TO_CHAR(ORD(InputString[i]) + 32 - ($'A' - $'a'))); // Note: The above line can be simplified to: // OutputString[i] := CHAR_TO_LOWER(InputString[i]); if SCL supports such a function directly. // However, using ASCII manipulation is shown here for clarity. // Check and convert lowercase letters (a-z) 'a'..'z': OutputString[i] := CHAR_TO_BYTE(BYTE_TO_CHAR(ORD(InputString[i]) - 32 + ($'A' - $'a'))); // Note: Similarly, the above line can be simplified to: // OutputString[i] := CHAR_TO_UPPER(InputString[i]); if such a function is available. // Non-alphabetic characters remain unchanged ELSE: OutputString[i] := InputString[i]; END_CASE; END_FOR; END_FUNCTION_BLOCK

Code Explanation

  1. Input and Output Variables:

    • InputString: The input string, of type ARRAY[1..20] OF CHAR.
    • OutputString: The output string, of type ARRAY[1..20] OF CHAR.

  2. Initializing the Output String: Use a loop to initialize the output string with space characters.

  3. Iterating Through the Input String: Use a FOR loop to iterate through each character of the input string.

  4. Character Conversion:

    • Use a CASE statement to check if the character is within the range of uppercase letters ('A'..'Z'). If so, convert it to a lowercase letter by adding 32 (the ASCII difference between lowercase and uppercase letters).

    • Similarly, check if the character is within the range of lowercase letters ('a'..'z'). If so, convert it to an uppercase letter by subtracting 32.

    • If the character is not a letter, leave it unchanged.

  5. Storing the Converted Characters: Store the converted characters in the corresponding positions of the output string.

Notes

• This example assumes that the input string will not exceed 20 characters in length. If the input string may be longer, you will need to adjust the array size accordingly or use dynamic string handling.

• The CHAR_TO_BYTE and BYTE_TO_CHAR functions are used for conversion between characters and bytes, as the ORD function returns a value of byte type.

• Character handling in SCL programming may require additional type conversions, depending on the specific implementation and version of the PLC.