开发发布者 NetLogic 和接口

开发 NetLogic 并将其与接口集成。
  1. 开发 NetLogic
  2. 在
    项目视图
    中,展开
    UI
    。
  3. 右键单击
    MainWindow (type)
    ,然后选择
    新建
    运行时 NetLogic
    。
  4. 将光标悬停在 NetLogic 上,选择
    Edit
    并输入
    PublisherLogic
    。
  5. 双击 NetLogic。
    外部代码编辑器将打开。
  6. 在代码编辑器中,使用以下代码替换现有代码:
    #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 PublisherLogic : BaseNetLogic { public override void Start() { var brokerIpAddressVariable = Project.Current.GetVariable("Model/BrokerIpAddress"); // Create a client connecting to the broker (default port is 1883) publishClient = new MqttClient(brokerIpAddressVariable.Value); // Connect to the broker publishClient.Connect("
    PublisherClient
    "); // Assign a callback to be executed when a message is published to the broker publishClient.MqttMsgPublished += PublishClientMqttMsgPublished; } public override void Stop() { publishClient.Disconnect(); publishClient.MqttMsgPublished -= PublishClientMqttMsgPublished; } private void PublishClientMqttMsgPublished(object sender, MqttMsgPublishedEventArgs e) { Log.Info("Message " + e.MessageId + " - published = " + e.IsPublished); } [ExportMethod] public void PublishMessage() { var variable1 = Project.Current.GetVariable("Model/Variable1"); variable1.Value = new Random().Next(0, 101); // Publish a message ushort msgId = publishClient.Publish("/my_topic", // topic System.Text.Encoding.UTF8.GetBytes(((int)variable1.Value).ToString()), // message body MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, // QoS level false); // retained } private MqttClient publishClient; }
    提醒事项: 上述代码将检索要从稍后创建的
    模型
    变量1
    变量发布的值。
  7. 保存代码。
  8. 创建界面元素
  9. 在
    项目视图
    中,右键单击
    模型
    ,然后选择
    新建
    变量
    。
    将创建
    变量1
    。可通过
    PublisherLogic
    脚本检索变量值。
  10. 添加“发布”按钮:
    1. 在
      项目视图
      中,右键单击
      MainWindow (type)
      ,然后选择
      新建
      基本控件
      按钮
      。
    2. 将光标悬停在按钮上,选择
      Edit
      并输入
      PublishButton
      。
    3. 在
      属性
      中,将
      文本
      设置为
      Publish
    4. 在
      事件
      中,选择
      MouseClick 事件
      旁边的
      Add
      ,然后选择
      ProjectName
      UI
      MainWindow (type)
      PublisherLogic
      PublishMessage
      。
提供反馈
对本文档有问题或反馈吗? 请在这里提交您的反馈 。
Normal