Programming workflow

This section describes how to use the function blocks of the TC3 IoT Functions PLC library from a best practice point-of-view. The programming workflow includes the following steps:

The code snippets in this section are based on the following declarations:

PROGRAM MAIN
VAR
    fbConnector : FB_IotFunctions_Connector;
    fbRead      : FB_IotFunctions_Message;
    fbWrite     : FB_IotFunctions_Message;
    nReadError  : UINT;
    nWriteError : UINT;
    nReadData   : UINT;
    nIn         : UINT;
    nOut        : UINT;
    bWrite      : BOOL;
END_VAR

Invoking FB_IotFunctions_Connector.Execute()

It is highly recommended to invoke the Execute method on the FB_IotFunctions_Connector function block instance as one of the first instructions. This method is responsible for Online Change handling, timeout handling and the communication with TC3 IoT Data Agent.

fbConnector.Execute();

Checking for general errors

Once the Execute method has been triggered, check the bError output of the FB_IotFunctions_Connector function block to figure out if any error has occurred during the communication with TC3 IoT Data Agent.

IF fbConnector.bError THEN
  ...
END_IF

Checking for read/write errors

If the bError output of the connector’s function block instance is TRUE, check the individual error message of the request function block for errors to handle them properly. To acknowledge an error and to prevent it from recurring, call the Reset method of the function block.

IF fbConnector.bError THEN
  IF fbRead.bError THEN
    nReadError := nReadError + 1;
    fbRead.Reset();
  END_IF
  IF fbWrite.bError THEN
    nWriteError := nWriteError + 1;
    fbWrite.Reset();
  END_IF
END_IF

Before starting a new operation like read or write, check all relevant status queries, as these operations will reset all status information in the structures ST_IotFunctionsMessage and ST_IotFunctionsRequest.

Reading data

The Read operation is receiving data from the buffer and stores this data in a symbol. The value of the specified symbol is used to compare new data with previous data. When new data has been received, bDataAvailable is set to TRUE. If the current value of the symbol is different from the previous value, the data has changed and bDataChanged is set to TRUE.

This means, if you want to act on new data that differs from the previously received data, check the bDataChanged output. This output will only be set to TRUE if the receive buffer indicates a change to the previously received packet.

IF fbRead.bDataChanged THEN
  ...
END_IF

If you are interested in receiving data regardless of the data differing from the previously received payload, check the bDataAvailable output instead.

IF fbRead.bDataAvailable THEN
  ...
END_IF

Writing data

The following sample demonstrates how to set up a conditional write operation that will only be executed if the bWrite flag is set to TRUE. Before calling the Write method ensure that the function block is not busy. If you write to a busy function block instance, the call will return without starting the write operation.

IF bWrite THEN
  IF NOT fbWrite.bBusy THEN
    bWrite := FALSE;
    fbWrite.Write(ADR(nOut), SIZEOF(nOut));
  END_IF
 END_IF