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;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;var
r = enet.richeditEx(winform.txtLog)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=""
; } } 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; options.Credentials = credential; 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){ 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
consolemqttClient.OnMqttMessageReceived = function
(client,topic,payload) { winform.edit.text =topic; } dv = function
(txt) { winform.edit.text =txt; winform.txtLog.appendText( textColor = 0xFF0000,txt ++ '\r\n'
; ) } winform.btnSM.oncommand = function
(id,event){ var
sub = mqttClient.SubscribeMessage(winform.cmbSM.text); if
(sub.IsSuccess) { r.log(0
,"订阅成功:"
++ sub.Message ,0xFF14B383); } else
{ r.log(0
,"订阅失败:"
++ sub.Message ,0xFFFF0000); } } winform.btnPM.oncommand = function
(id,event){ var
message = HSIdll.new("HslCommunication.MQTT.MqttApplicationMessage"
); message.Topic = "A003"
; message.QualityOfServiceLevel =0
message.Payload ="123333"
message.Retain = false
; var
result = mqttClient.PublishMessage(message); if
(result.IsSuccess) { r.log(0
, "发布成功"
, 0xFF14B383); } else
{ r.log(0
, "发布失败:"
++ result.Message, 0xFFFF0000); } } 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();