IEC61499 应用程序-MQTT publish/subscribe
程序员文章站
2022-03-12 15:00:07
...
4diac-IDE 和forte 运行时支持MQTT .
IEC61499 应用程序
subscribe 功能块的ID为
fbdk[].mqtt[tcp://localhost:1883, forte,input]
publish 功能块的ID为
fbdk[].mqtt[tcp://localhost:1883, forte,output]
值得注意的是这里使用了fbdk[] ,他在MQTT message.payload 中采用的是ASN.1 编码方式。这里 4个整形数。payload 的格式为
0x43 0x00 0x00 0x43 0x00 0x00 0x43 0x00 0x00
MQTT broker 使用mosquitto ,在运行程序之前需要运行该程序。方法有两种
sudo mosquitto
sudo service mosquitto start
为了仿真一个外部的MQTT应用。编写了一个python 应用程序.这个程序什么都没有做,只是将三个数循环加1
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()