Search code examples
timetextboxplctwincat

Setting time variable via textbox in Twincat 3 HMI


With variables such as integer, float or string I used Write To Symbol to write the variable to the PLC with the HMI textbox under .onTextChanged in the properties window (see images below). Write To Symbol Properties window

But it won't work with the Time variable. How can I make this work without changing the PLC code?


Solution

  • I've never worked with javascript before, but that is where I found the sollution. I also used .onUserInteractionFinished instead of .onTextChanged like displayed in image underneath:

    Properties Window

    After that I wrote this javascript code:

    (function (TcHmi) {
    
        var CheckTextboxForNumber = function (Textbox) {
            //get content from the textbox
            var _text = Textbox.getText();
            //convert to time variable in 
            if (!_text.startsWith('PT')) {
                var _value = Number(_text);
                Textbox.setText('PT' + _value.toFixed(3) + 'S');
                return _value.toFixed(3);
            }
        };
    
        TcHmi.Functions.registerFunction('CheckTextboxForNumber', CheckTextboxForNumber);
    })(TcHmi);
    

    I put the code in before the Write To Symbol, with an added rounding, because the rounding is done differently after the 3th decimal: when I tested it without rounding the decimals, starting with the 4th, the PLC would display other decimals then I input in the HMI textbox.

    What I input in the 'actions and conditons' window can be seen in below image:

    Writing variable to PLC

    After that it worked as it was supposed to.