基于HslCommunication.dll的 MQTT客户端测试

mndsoft 9月前 967

HslCommunication.dll是一个.NET库,支持MQTT功能,根据来自 https://www.chengxu.xyz/t/461 网站的帖子做了个测试,增加了用户名密码验证和界面。

支持连接,订阅等。还有一些慢慢研究。

不知为何 在mqttClient.OnMqttMessageReceived 函数里收到数据想 winform.txtLog.appendText( textColor = 0xFF0000,topic ++ '\r\n'; ) 到富文本框里,就会出现闪崩。

只能 log 或 winform.edit.text =topic;

感觉这个库体积比较大,如果单独只需要MQTT的话,还是 MQTTnet 这个专门库好,体积小,功能丰富,接下来研究这个库。

Code AardioLine:235复制
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.
  • 214.
  • 215.
  • 216.
  • 217.
  • 218.
  • 219.
  • 220.
  • 221.
  • 222.
  • 223.
  • 224.
  • 225.
  • 226.
  • 227.
  • 228.
  • 229.
  • 230.
  • 231.
  • 232.
  • 233.
  • 234.
  • 235.
    • import win.ui;
    • import enet.richeditEx;
    • import fonts.fontAwesome;
    • /*DSG{{*/
    • var winform = win.form(text="MQTT 基于HslCommunication 测试";right=701;bottom=492)
    • winform.add(
    • btnConnect={cls="plus";text="连接";left=546;top=34;right=660;bottom=65;bgcolor=5329231;border={radius=8};color=16777215;dl=1;dt=1;font=LOGFONT(h=-18;name='微软雅黑');iconStyle={align="left";font=LOGFONT(h=-20;name='FontAwesome');padding={left=10;top=2}};iconText='\uF0C1';notify=1;tabstop=1;textPadding={right=-35};x=8;y=5;z=14};
    • btnPM={cls="plus";text="发布";left=578;top=273;right=692;bottom=304;bgcolor=5329231;border={radius=8};color=16777215;dl=1;dt=1;font=LOGFONT(h=-18;name='微软雅黑');iconStyle={align="left";font=LOGFONT(h=-20;name='FontAwesome');padding={left=10;top=2}};iconText='\uF0C1';notify=1;tabstop=1;textPadding={right=-35};x=8;y=5;z=16};
    • btnSM={cls="plus";text="订阅";left=238;top=220;right=352;bottom=251;bgcolor=5329231;border={radius=8};color=16777215;dl=1;dt=1;font=LOGFONT(h=-18;name='微软雅黑');iconStyle={align="left";font=LOGFONT(h=-20;name='FontAwesome');padding={left=10;top=2}};iconText='\uF0C1';notify=1;tabstop=1;textPadding={right=-35};x=8;y=5;z=15};
    • button={cls="button";text="Button";left=619;top=193;right=683;bottom=213;z=27};
    • checkbox={cls="checkbox";left=113;top=177;right=184;bottom=203;z=19};
    • checkbox2={cls="checkbox";text="HEX";left=520;top=279;right=569;bottom=298;checked=1;z=25};
    • cmbSM={cls="combobox";left=101;top=222;right=216;bottom=248;edge=1;items={"B003"};mode="dropdown";z=20};
    • combobox2={cls="combobox";left=479;top=221;right=630;bottom=247;edge=1;items={};mode="dropdown";z=23};
    • edit={cls="edit";text="Edit";left=546;top=85;right=682;bottom=178;edge=1;multiline=1;z=26};
    • plus={cls="plus";left=14;top=14;right=507;bottom=211;border={color=-5846288;radius=15;width=2};z=2};
    • plus3={cls="plus";text="连接参数";left=40;top=0;right=118;bottom=28;font=LOGFONT(h=-16);z=5};
    • plus9={cls="plus";text="55 AA AA AA AA AA 00 01 2F D7 16";left=88;top=272;right=504;bottom=307;align="left";border={color=-5846288;radius=4;width=1};editable=1;font=LOGFONT(h=-14;name='微软雅黑');notify=1;textPadding={left=6;top=6};z=17};
    • static={cls="static";text="主机:";left=38;top=50;right=86;bottom=72;transparent=1;z=3};
    • static2={cls="static";text="主机:";left=266;top=51;right=314;bottom=73;transparent=1;z=6};
    • static3={cls="static";text="客户端 ID:";left=27;top=92;right=86;bottom=114;transparent=1;z=8};
    • static4={cls="static";text="用户名:";left=33;top=138;right=97;bottom=160;transparent=1;z=10};
    • static5={cls="static";text="密码:";left=267;top=138;right=315;bottom=160;transparent=1;z=12};
    • static6={cls="static";text="清除会话:";left=36;top=177;right=101;bottom=199;transparent=1;z=18};
    • static7={cls="static";text="订阅主题:";left=19;top=227;right=84;bottom=249;transparent=1;z=21};
    • static8={cls="static";text="发布主题:";left=395;top=225;right=460;bottom=247;transparent=1;z=22};
    • static9={cls="static";text="发布内容:";left=23;top=280;right=88;bottom=302;transparent=1;z=24};
    • txtClientID={cls="plus";text="PC002";left=93;top=86;right=247;bottom=115;align="left";border={color=-2302756;radius=20;width=1};editable=1;font=LOGFONT(h=-14;name='微软雅黑');notify=1;textPadding={left=10;top=5;right=10};z=9};
    • txtIP={cls="plus";text="101.43.104.144";left=91;top=45;right=245;bottom=74;align="left";border={color=-16728032;radius=15;width=1};editable=1;font=LOGFONT(h=-14;name='微软雅黑');notify=1;textPadding={left=10;top=5;right=10};z=4};
    • txtLog={cls="richedit";left=7;top=308;right=697;bottom=486;bgcolor=0;db=1;dl=1;dr=1;dt=1;edge=1;font=LOGFONT(h=-13;name='微软雅黑');multiline=1;readonly=1;vscroll=1;wrap=1;z=1};
    • txtPW={cls="plus";text="12345678";left=323;top=132;right=477;bottom=161;align="left";border={color=-2302756;radius=20;width=1};editable=1;font=LOGFONT(h=-14;name='微软雅黑');notify=1;textPadding={left=10;top=5;right=10};z=13};
    • txtPort={cls="plus";text="1883";left=315;top=45;right=469;bottom=74;align="left";border={color=-2302756;radius=20;width=1};editable=1;font=LOGFONT(h=-14;name='微软雅黑');notify=1;textPadding={left=10;top=5;right=10};z=7};
    • txtUN={cls="plus";text="PC002";left=98;top=132;right=252;bottom=161;align="left";border={color=-2302756;radius=20;width=1};editable=1;font=LOGFONT(h=-14;name='微软雅黑');notify=1;textPadding={left=10;top=5;right=10};z=11}
    • )
    • /*}}*/
    • import thread.command;
    • //log信息
    • var r = enet.richeditEx(winform.txtLog)
    • //显示Log信息
    • logview = function(data,color,bkcolor){
    • winform.txtLog.appendText( tostring(time(," [%H:%M:%S] ") ),data,'\r\n');
    • var line = winform.txtLog.lineCount-1;
    • var start = winform.txtLog.lineToChar(line);
    • winform.txtLog.setsel(start,-1);
    • winform.txtLog.setSelCharformat( textColor = color;backColor = bkcolor)
    • winform.txtLog.deselect();
    • winform.txtLog.lineScroll();
    • if (line>100) {
    • winform.txtLog.text="";
    • }
    • }
    • //调用C#程序集
    • import dotNet;
    • //加载应用程序目录下的程序集
    • var HSIdll = dotNet.load("\HslCommunication.dll");
    • var options = HSIdll.new("HslCommunication.MQTT.MqttConnectionOptions");
    • var credential = HSIdll.new("HslCommunication.MQTT.MqttCredential",winform.txtUN.text, winform.txtPW.text);
    • options.CleanSession = true;
    • options.IpAddress=winform.txtIP.text;
    • options.Port=1883;
    • options.ClientId=winform.txtClientID.text;
    • //创建 MqttCredential 对象并设置用户名和密码
    • options.Credentials = credential;
    • //options.UseRSAProvider = true // 通信加密
    • mqttClient= HSIdll.new("HslCommunication.MQTT.MqttClient",options);
    • //按钮前景色、背景色可以都改为图像路径
    • var skin = {
    • background={
    • active=0xFF4B739A;
    • default=0xFF4F5151;
    • hover=0xFF4B739A;
    • }
    • foreground = {
    • disabled = 0xFFCCCCCC;
    • };
    • color = {
    • };
    • border = {
    • active = {bottom=3;color=0xFF8FC31F;padding=8;radius=10;}
    • };
    • }
    • winform.txtIP.skin(
    • border = {
    • focus = {width=1;color=0xFF6AB100;radius=15};
    • }
    • )
    • winform.txtPort.skin(
    • border = {
    • focus = {width=1;color=0xFF6AB100;radius=15};
    • }
    • )
    • winform.btnConnect.oncommand = function(id,event){
    • //mqttClient.ConnectClose( )
    • if winform.btnConnect.text="断开" {
    • mqttClient.ConnectClose();
    • winform.btnConnect.iconText = '\uF0C1'
    • winform.btnConnect.text ="连接"
    • r.log(0,"断开连接!" ,0xFFDD254C); //前景色蓝色,背景色
    • return ;
    • }
    • winform.btnConnect.disabledText = {'\uF254';'\uF251';'\uF252';'\uF253';'\uF250';text='请稍候...'}
    • //连接服务器
    • var connect = mqttClient.ConnectServer();
    • //判断是否成功
    • if (!connect.IsSuccess)
    • {
    • winform.btnConnect.disabledText =null
    • winform.btnConnect.iconText = '\uF0C1'
    • winform.btnConnect.text ="连接"
    • r.log(0,"连接失败:" ++ connect.Message ,0xFFFF0000);
    • }else {
    • winform.btnConnect.disabledText =null
    • winform.btnConnect.iconText = '\uF127'
    • winform.btnConnect.text = "断开"
    • r.log(0,"连接成功:" ++ connect.Message ,0xFF14B383);
    • }
    • }
    • thread.command.instance().updateStatic = function(stopic){
    • winform.edit.text =stopic
    • winform.txtLog.appendText( textColor = 0xFF0000,stopic ++ '\r\n'; )
    • }
    • //接收信息
    • import console
    • mqttClient.OnMqttMessageReceived = function (client,topic,payload) {
    • /*握手完成触发*/
    • //import thread.command;
    • //thread.command.post("updateStatic",tx);
    • //r.log(0,"主题:" ++ topic ,0xFF14B383);
    • //刷新界面
    • winform.edit.text =topic;
    • //dv(tx);
    • //winform.txtLog.appendText( textColor = 0xFF0000,topic ++ '\r\n'; )
    • //r.log(0,"主题:" ++ topic ,0xFF14B383);
    • //logview("AT接收-->" ++ string.str(topic) ,0x00FF00);
    • //console.log("client:",client.ToString())
    • //console.log("topic:",topic)
    • //console.log("payload:",payload)
    • // 在主线程中更新 UI
    • /*
    • winform.post(function(){
    • var currentText = winform.edit.text;
    • var newMessage = string.format("Topic: %s\r\n Payload: %s\r\n\r\n", topic, payload);
    • winform.edit.text = currentText + newMessage;
    • // 滚动到文本框底部
    • winform.edit.scrollCaret();
    • }); */
    • }
    • //mqttClient.OnMqttMessageReceived=function(topic,payload) begin
    • // Console.log( "Topic:" + topic );
    • //end;
    • dv = function (txt) {
    • winform.edit.text =txt;
    • winform.txtLog.appendText( textColor = 0xFF0000,txt ++ '\r\n'; )
    • //r.log(0,"主题:" ++ txt ,0xFF14B383);
    • }
    • //订阅主题
    • winform.btnSM.oncommand = function(id,event){
    • var sub = mqttClient.SubscribeMessage(winform.cmbSM.text); // mqttClient.SubscribeMessage( "/Pub" );
    • if (sub.IsSuccess)
    • {
    • r.log(0,"订阅成功:" ++ sub.Message ,0xFF14B383);
    • }
    • else
    • {
    • r.log(0,"订阅失败:" ++ sub.Message ,0xFFFF0000);
    • }
    • }
    • //发布主题 http://api.hslcommunication.cn/html/90929e44-65c0-951f-9b09-d45133bcdd3b.htm
    • winform.btnPM.oncommand = function(id,event){
    • var message = HSIdll.new("HslCommunication.MQTT.MqttApplicationMessage");
    • message.Topic = "A003"; // 主题
    • message.QualityOfServiceLevel =0 // HSIdll.enum("HslCommunication.MQTT.MqttQualityOfServiceLevel").AtMostOnce; // 消息等级
    • message.Payload ="123333" // HSIdll.System.Text.Encoding.UTF8.GetBytes("This is test message!"); // 数据
    • message.Retain = false; // 是否保留
    • var result = mqttClient.PublishMessage(message);
    • if (result.IsSuccess) {
    • r.log(0, "发布成功", 0xFF14B383);
    • } else {
    • r.log(0, "发布失败:" ++ result.Message, 0xFFFF0000);
    • }
    • /*
    • mqttClient.PublishMessage(new MqttApplicationMessage()
    • {
    • Topic = "A", // 主题
    • QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce, // 消息等级
    • Payload = Encoding.UTF8.GetBytes("This is test message!"), // 数据
    • Retain = false, // 是否保留
    • });
    • */
    • }
    • winform.button.oncommand = function(id,event){
    • winform.txtLog.appendText( textColor = 0xFF0000,"dddd" ++ '\r\n'; )
    • r.log(0, "发布成功", 0xFF14B383);
    • }
    • winform.btnConnect.skin(skin);
    • winform.btnSM.skin(skin);
    • winform.btnPM.skin(skin);
    • winform.show();
    • win.loopMessage();


    最新回复 (7)
    • popde 9月前
      0 2
      appendtext参数如果要定义文本颜色, 参数必须是个表, winform.richedit.appendText({textColor = 0xFF0000,topic ++ '\r\n';}); 这样就行了
    • mndsoft 9月前
      0 3
      popde appendtext参数如果要定义文本颜色, 参数必须是个表, winform.richedit.appendText({textColor = 0xFF0000,topic ++ '\r\n';}) ...
      也可以这样写的。不是这个问题哈 /* 下面这样写也可以,等于传了一个表参数并省略了外层大括号{}。 函数会自动展开参数表的数组成员作为其他调用参数,可指定多个文本参数。 */ winform.richedit.appendText( textColor = 0xFF0000, "蓝色文本" ) //下面这样写作用一样 winform.richedit.appendText( { textColor = 0xFF0000, "蓝色文本"} )
    • 小光芒 9月前
      0 4
      MQTTnet 才300多K
    • mndsoft 9月前
      0 5
      小光芒 MQTTnet 才300多K
      是啊,这个HslCommunication.dll 太大了,不过包含了很多功能。如果这个能封住aardio库,基本工控上的功能都可以搞定了
    • mndsoft 9月前
      0 6

      增加修改了发布功能:

      Code AardioLine:25复制
    • 1.
    • 2.
    • 3.
    • 4.
    • 5.
    • 6.
    • 7.
    • 8.
    • 9.
    • 10.
    • 11.
    • 12.
    • 13.
    • 14.
    • 15.
    • 16.
    • 17.
    • 18.
    • 19.
    • 20.
    • 21.
    • 22.
    • 23.
    • 24.
    • 25.
      • // 创建 MqttApplicationMessage 对象
      • var message = HSIdll.new("HslCommunication.MQTT.MqttApplicationMessage");
      • // 设置主题
      • message.Topic = "A003";
      • // 设置消息等级为 AtMostOnce
      • //message.QualityOfServiceLevel = HSIdll.get("HslCommunication.MQTT.MqttQualityOfServiceLevel", "AtMostOnce");
      • // 设置消息等级为 AtMostOnce
      • message.QualityOfServiceLevel =0 // HSIdll.get("HslCommunication.MQTT.MqttQualityOfServiceLevel.AtMostOnce");
      • // 设置负载
      • //var payload = dotNet.encoding.UTF8.GetBytes("This is test message!");
      • var payload = System.Text.Encoding.UTF8.GetBytes("This is test message!");
      • message.Payload = payload; // 直接设置 Payload 属性
      • // 设置是否保留消息
      • message.Retain = false;
      • // 发布消息
      • var publish = mqttClient.PublishMessage(message);
      • if (publish.IsSuccess) {
      • r.log(0,"发布成功:" ++ publish.Message ,0xFF14B383);
      • } else {
      • r.log(0,"发布失败:" ++ publish.Message ,0xFFFF0000);
      • }


    • jerry2cool 9月前
      0 7
      感谢分享,学习学习
    • mndsoft 9月前
      0 8
      有关闪崩的问题找到了,不要用最新版的HslCommunication.dll,用11.1这个版本的,最近最新版的好像增加的限制次数。
    返回