Aufbau

Eine Funktion wird innerhalb des Namensraumes implementiert. Über diesen Namensraum wird sie innerhalb des System angesprochen. Die Funktion wird über die Framework-API-Funktion „registerFunctionEx“ (ab Version 1.12) bzw. „registerFunction“ im Framework registriert, damit sie im zur Laufzeit zur Verfügung steht.

Im folgenden Beispiel wird die Konvertierung einer Temperatur von Fahrenheit nach Celsius implementiert und die aufrufende Stelle zurückgegeben.

Grundaufbau einer TypeScript-Function per ES Module

Die Nutzung des Namensraum ist bei ES Modulen nur innerhalb des Register-Aufrufs relevant, da die Module selbst innerhalb dieser Datei eine saubere Kapselung bieten.

function FahrToCels(TempInFahr: number | string) {
   // variable for the return value
   var value = TempInFahr;

   // check the type of the parameter
   if (typeof TempInFahr === 'number') {
      // parameter is already a number

      // convert the parameter value to Celsius
      value = ((TempInFahr - 32) * (5 / 9));
   } else if (typeof TempInFahr === 'string') {
      // parameter is a string

      // convert the parameter type and the parameter value
      value = ((parseFloat(TempInFahr) - 32) * (5 / 9));
   }

   // return the converted parameter
   return value;
}
TcHmi.Functions.registerFunctionEx('FahrToCels', 'TcHmi.Functions.TcHmiProject40', FahrToCels);

Mehr Informationen zu Javascript Modulen finden Sie auf einer separaten Seite.

Grundaufbau einer TypeScript-Function

namespace TcHmi {
   export namespace Functions {
      export namespace TcHmiProject41 {
         export function FahrToCels(TempInFahr: number | string) {
            // variable for the return value
            var value = TempInFahr;

            // check the type of the parameter
            if (typeof TempInFahr === 'number') {
               // parameter is already a number

               // convert the parameter value to Celsius
               value = ((TempInFahr - 32) * (5 / 9));
            } else if (typeof TempInFahr === 'string') {
               // parameter is a string

               // convert the parameter type and the parameter value
               value = ((parseFloat(TempInFahr) - 32) * (5 / 9));
            }

            // return the converted parameter
            return value;
         }
      }
   }
}
TcHmi.Functions.registerFunctionEx('FahrToCels', 'TcHmi.Functions.TcHmiProject40', TcHmiProject41.FahrToCels);

Grundaufbau einer JavaScript-Function (ab Version 1.12)

(function (/** @type {globalThis.TcHmi} */ TcHmi) {
   var Functions;
    (function (/** @type {globalThis.TcHmi.Functions} */ Functions) {
      var TcHmiProject40;
      (function (TcHmiProject40) {
         /**
          * @param {string|number} TempInFahr
          */
         function FahrToCels(TempInFahr) {
            // variable for the return value
            var value = TempInFahr;

            // check the type of the parameter
            if (typeof TempInFahr === 'number') {

               // parameter is already a number

               // convert the parameter value to celsius
               value = ((TempInFahr - 32) * (5 / 9));
            }else if (typeof TempInFahr === 'string') {
               // parameter is a string

               // convert the parameter type and the parameter value
               value = ((parseFloat(TempInFahr) - 32) * (5 / 9));
            }

            // return the converted parameter
            return value;
         }
         TcHmiProject40.FahrToCels = FahrToCels;
      })(TcHmiProject40 = Functions.TcHmiProject40 || (Functions.TcHmiProject40 = {}));
   })(Functions = TcHmi.Functions || (TcHmi.Functions = {}));
})(TcHmi);
TcHmi.Functions.registerFunctionEx('FahrToCels', 'TcHmi.Functions.TcHmiProject40', TcHmiProject40.FahrToCels);

Grundaufbau einer JavaScript-Function (ab Version 1.8)

// required namespace TcHmi
(function (/** @type {globalThis.TcHmi} */ TcHmi) {
   // name and parameters of the function defined in json
   var FahrToCels = function (TempInFahr) {
      // variable for the return value
      var value = TempInFahr;

      // check the type of the parameter
      if (typeof TempInFahr === 'number') {
         // parameter is already a number

         // convert the parameter value to celsius
         value = ((TempInFahr - 32) * (5 / 9));
      }
      else if (typeof TempInFahr === 'string') {
         // parameter is a string

         // convert the parameter type and the parameter value
         value = ((parseFloat(TempInFahr) - 32) * (5 / 9));
      }

      // return the converted parameter
      return value;
   };
})(TcHmi);
// register the function in the framework
TcHmi.Functions.registerFunction('FahrToCels', FahrToCels);

Für einen besseren Support von IntelliSense werden Referenzen auf das Framework und jQuery innerhalb der JavaScript-Datei der Funktion erstellt. Dies ist bei TypeScript nicht nötig.

// TcHmi Version 1.12
/// <reference path="../Packages/Beckhoff.TwinCAT.HMI.Framework.12.742.1/runtimes/native1.12-tchmi/TcHmi.d.ts" />

// TcHmi Version 1.8
// Provider for a best effort Intellisense of Visual Studio 2017/2019.
/// <reference path="C:\TwinCAT\Functions\TE2000-HMI-Engineering\Infrastructure\TcHmiFramework\Latest\Lib\jquery.d.ts" />
/// <reference path="C:\TwinCAT\Functions\TE2000-HMI-Engineering\Infrastructure\TcHmiFramework\Latest\TcHmi.d.ts" />
/// <reference path="C:\TwinCAT\Functions\TE2000-HMI-Engineering\Infrastructure\TcHmiFramework\Latest\Controls\System\TcHmiControl\Source.d.ts" />

// Provider for a best effort Intellisense of Visual Studio 2013/2015.
/// <reference path="C:\TwinCAT\Functions\TE2000-HMI-Engineering\Infrastructure\TcHmiFramework\Latest\Lib\jquery\jquery.js" />
/// <reference path="C:\TwinCAT\Functions\TE2000-HMI-Engineering\Infrastructure\TcHmiFramework\Latest\TcHmi.js" />