4diac-IDE and forte runtime support MQTT.
IEC61499 application
The ID of the subscribe function block is
fbdk[].mqtt[tcp://localhost:1883, forte,input]
The ID of the publish function block is
fbdk[].mqtt[tcp://localhost:1883, forte,output]
It is worth noting that fbdk[] is used here, and it uses ASN.1 encoding in MQTT message.payload. Here are 4 integer numbers. The format of the payload is
0x43 0x00 0x00 0x43 0x00 0x00 0x43 0x00 0x00
MQTT broker uses mosquitto, which needs to be run before running the program. There are two ways
sudo mosquitto
sudo service mosquitto start
In order to simulate an external MQTT application. Wrote a python application. This program does nothing, just add 1 to three numbers in a loop
import time
import paho.mqtt.client as mqtt
# The callback for when the client receives a CONNACK response from the server.
def bytesToHexString(bs):
return ''.join(['%02X ' % b for b in bs])
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("连接成功")
print("Connected with result code " + str(rc))
data=bytearray(12)
def on_message(client, userdata, msg):
#pprint(msg.topic + " " + bytesToHexString(msg.payload))
for i in range(12):
data[i]=msg.payload[i]
data[2]=data[2]+1
if data[2]==255:
data[2]=0
data[5]=data[5]+1
if data[5]==255:
data[5]=0
data[8]=data[8]+1
if data[8]==255:
data[8]=0
data[11]=data[11]+1
if data[11]==255:
data[11]=0
print(msg.topic + " " + bytesToHexString(data))
client.publish(topic="input", payload=bytes(data), qos=1, retain=False)
client = mqtt.Client(protocol=3)
client.on_connect = on_connect
client.on_message = on_message
client.connect(host="localhost", port = 1883, keepalive=60) # 订阅频道
time.sleep(1)
# client.subscribe("public")
client.subscribe([("output", 0), ("test", 2)])
client.loop_forever()