PublishByteArray

TwinCAT TIA
Network MQTT Client ActiveClients One client

ns=2;s=DEPRAG Smart/Network/MQTT/ActiveClients/0/PublishByteArray

The command sends a message with associated topic to the MQTT broker.

Parameters:

    MessageData
    Contents of the transmitting message as a byte array.
    QualityOfServiceLevel
    Configures the Quality of Service level of the transmission:
    AtMostOnce(0): The message is published exactly once and no acknowledgement of receipt from the broker is awaited.
    AtLeastOnce(1): The message is sent until the client receives an acknowledgement from the MQTT broker. This ensures that the message has arrived at the receiver at least once.
    ExactlyOnce(2): The message is transmitted exactly once from the client to the broker.
    WithRetainFlag
    Marks the Retain flag in the MQTT message. The broker is instructed to cache the last message sent to the topic. Clients that connect to the broker at a later time will receive this message.

Returns:

Examples:

In addition to the parameters already explained, the following variables must be created / transferred for the implementation in TwinCAT 3:

Beckhoff parameter:

    bMqttPublishByteArray
    A boolean variable that can be used to trigger the execution of the PublishByteArray command.
    OutputDataMqttPublishByteArray
    An instance of the ST_VoidOutputData structure in which the return value of the PublishByteArray method is written.
    hrMqttPublishByteArray
    This can be used to monitor the execution of the command. Error codes have the type HRESULT.
    fbMqtt
    An instance of the "FB_Mqtt" function block supplied with the DEPRAG Smart library.
    sTopic
    A string(255) variable representing the topic of the MQTT message.
    nMessageLength
    A UINT variable specifying the length of the message to be sent.
    QualityOfServiceLevel
    An enum of type "MqttQualityOfServiceLevel" which specifies the Quality of Service level of the transmission.
    bWithRetainFlag
    A boolean variable that sets the retain flag in the MQTT message.
    sClientId
    A String(255) variable that specifies the client that should execute the command.
    MessageData
    A variable of type "ARRAY[*] OF BYTE" containing the message to be sent.
    ConnectionHandle
    "ConnectionHandle" received when establishing a connection between DEPRAG Smart Server and your OPC UA client.


VAR
   bMqttPublishByteArray        : BOOL;
   OutputDataMqttPublishByteArray      : ST_VoidOutputData;
   hrMqttPublishByteArray        : HRESULT;
   testLongByteArray : ARRAY[1..1000] OF BYTE := [84, 101, 115, 116, 255];

   fbMqtt  : FB_Mqtt;
END_VAR

hrMqttPublishByteArray := fbMqtt.PublishByteArray(
     bExecute        := bMqttPublishByteArray, 
     ConnectionHandle      := depragSmartConnection.ConnectionHandle, 
     sTopic         := 'TestSmartTwinCat', 
     nMessageLength       := 5, 
     QualityOfServiceLevel     := MqttQualityOfServiceLevel.ExactlyOnce, 
     bWithRetainFlag       := FALSE, 
     sClientId        := '0', 
     MessageData        := testLongByteArray, 
     OutputData        => OutputDataMqttPublishByteArray);
IF hrMqttPublishByteArray <> S_FALSE THEN 
    bMqttPublishByteArray := FALSE;
END_IF
                                                    

For examples and explanations regarding the calls of nodes via Siemens TIA Portal please refer to the sub-menu "Siemens TIA Portal" in section "Getting Started".