开发订阅者 NetLogic 和界面
开发 NetLogic 并将其与接口集成。
- 开发 NetLogic
- 右键单击MainWindow (type),然后选择 。
- 将光标悬停在 NetLogic 上,选择 并输入SubscriberLogic。
- 双击 NetLogic。外部代码编辑器将打开。
- 在代码编辑器中,使用以下代码替换现有代码:#region StandardUsing using System; using FTOptix.CoreBase; using FTOptix.HMIProject; using UAManagedCore; using OpcUa = UAManagedCore.OpcUa; using FTOptix.NetLogic; using FTOptix.UI; using FTOptix.OPCUAServer; #endregion using uPLibrary.Networking.M2Mqtt; using uPLibrary.Networking.M2Mqtt.Messages; public class SubscriberLogic : BaseNetLogic { public override void Start() { var brokerIpAddressVariable = Project.Current.GetVariable("Model/BrokerIpAddress"); // Create a client connecting to the broker (default port is 1883) subscribeClient = new MqttClient(brokerIpAddressVariable.Value); // Connect to the broker subscribeClient.Connect("SubscriberClient"); // Assign a callback to be executed when a message is received from the broker subscribeClient.MqttMsgPublishReceived += SubscribeClientMqttMsgPublishReceived; // Subscribe to the "my_topic" topic with QoS 2 ushort msgId = subscribeClient.Subscribe(new string[] { "/my_topic" }, // topic new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE }); // QoS level messageVariable = Project.Current.GetVariable("Model/Message"); } public override void Stop() { subscribeClient.Unsubscribe(new string[] { "/my_topic" }); subscribeClient.Disconnect(); } private void SubscribeClientMqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e) { messageVariable.Value = "Message received: " + System.Text.Encoding.UTF8.GetString(e.Message); } private MqttClient subscribeClient; private IUAVariable messageVariable; }
- 保存代码。
- 创建界面元素
- 创建消息变量:
- 在项目视图中,右键单击模型,然后选择 。
- 将光标悬停在变量上,选择 并输入Message。
- 在属性中,选择Int32,然后选择String。
- 创建订阅标签:
- 在项目视图中,右键单击MainWindow (type),然后选择 。
- 将光标悬停在标签上,选择 并输入SubscribeLabel。
- 在属性中,在文本和Message变量之间创建动态链接。有关动态链接的更多信息,请参见创建动态链接。
- 排列界面元素。
提供反馈