Some time ago, I undertook a project, software development, time is tight, so I used VB to deduct one. Later, the user proposed to transmit the data to their MQTT server for distribution, and used the VB 6.0 code for a few days to get a VB 6.0 program that can communicate with the MQTT Broker.
In order to support other people's rapid development and use. A program with Socket communication port is made.
MQTT Client for VB 6 service program
All codes are written in VB6.0.
Four API functions
Sleep
Shell_NotifyIcon
SendMessage
GetPrivateProfileString are called. The
first three are to make a tray icon, and the latter is to read and write the configured INI file.
Use Winsocket control, Timer control, Textbox control, Frame control, Label control.
There are no third-party controls.
There are no third-party controls.
There are no third-party controls.
The main interface is as follows:
Note: The MQTT Broker port we tested by ourselves is 1884, while the conventional MQTT Broker port is 1883. After the
software is running, the interface
The program usually exists as a tray icon
The icon is also drawn by myself.
Development idea: Developed in the session layer of the OSI 7-layer protocol. It is SOCKET programming.
The user-developed VB program uses TCP SOCKET to communicate with the MQTT Client for VB 6, and transmits a specific message structure through the socket, and then it can send Topic to MQTT Broker, subscribe to the Topic, and accept the content of the subscribed Topic.
On Error Resume Next
Dim INIFileName As String
Dim MQTTServerIP As String
Dim MQTTServerPort As Long
Dim SocketServerPort As Long
Dim i As Long
'初始化页面代码
Me.TextFasong = ""
Me.TextNeiRongD = ""
Me.ListDingYue.Clear
Me.Label1.Caption = "接收的数据"
Me.Label2.Caption = "发送的数据"
Me.Label3.Caption = "订阅的主题"
Caption = "MQTT Client for VB6 服务程序"
'启动后,缩小为图标
Me.WindowState = vbMinimized
If WindowState = vbMinimized Then
LastState = vbNormal
Else
LastState = WindowState
End If
With mydata
.cbSize = Len(mydata)
.hwnd = Me.hwnd
.uID = 0
.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
.uCallbackMessage = WM_MOUSEMOVE
.hIcon = Me.Icon.Handle '默认为窗口图标
.szTip = "MQTT client for VB6 服务程序" & vbNullChar
End With
Shell_NotifyIcon NIM_ADD, mydata
'读取配置文件打开MQTT链接
INIFileName = FormatFileName("config.ini")
MQTTServerIP = ReadINI("MQTTBroker", "BrokerADD ", INIFileName)
MQTTServerPort = Val(ReadINI("MQTTBroker", "BrokerPort", INIFileName))
MQTTpublisherName = ReadINI("MQTTBroker", "MQTTpublisherName", INIFileName)
MQTTTopicquantity = Val(ReadINI("MQTTBroker", "Topicquantity", INIFileName))
MQTTPublish = ReadINI("MQTTBroker", "MQTTPublish", INIFileName)
For i = 0 To MQTTTopicquantity - 1
MQTTTopic(i) = ReadINI("MQTTBroker", "Topic" & i + 1, INIFileName)
Me.ListDingYue.AddItem MQTTTopic(i), 0
Next i
'MQTT Socket 初始化
Me.LabelMQTTBrokerName = "MQTT Broker:" & MQTTServerIP
Me.LabelMQTTPort = "MQTT Broker Port:" & MQTTServerPort
Me.LabelMQTTpublisher = "MQTT publisher:" & MQTTpublisherName
MQTTBrokerFlag = True
WinsockMQTT.RemoteHost = MQTTServerIP
WinsockMQTT.RemotePort = MQTTServerPort
WinsockMQTT.Protocol = sckTCPProtocol
TimerMQTTState.Enabled = True
MQTTFlag = False
If WinsockMQTT.State = 0 And MQTTBrokerFlag Then
WinsockMQTT.Connect
End If
'SocketServer 初始化
SocketServerPort = ReadINI("SocketServer", "SocketServerPort", INIFileName)
Me.TimerServerState.Enabled = True
If SocketServerPort >= 1000 Then
WinsockServer.LocalPort = SocketServerPort
WinsockServer.Protocol = sckTCPProtocol
WinsockServer.Listen
Me.LabelSocketServerPort = "Socket Server Port:" & SocketServerPort
End If
End Sub
This is the code initiated by the main From.
The biggest difficulty in developing this is that the message structure in the MQTT protocol is very special and requires a lot of conversion work.
It took 5 days to write about it, about 30 hours.
For the time being, this MQTT Client for VB 6 can only communicate with servers based on MQTT 3.1.1, such as Mobile OneNet, Alibaba Cloud, and Tencent Cloud. Our own projects are run by Huawei Cloud.
When you have time, you can add encryption to support MQTT Broker for encrypted transmission.
Those who are interested, we can discuss together.