• 大小: 100KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-12
  • 语言: C#
  • 标签: MQTT  

资源简介

winForm中使用MQTT收发消息,不懂的可以联系qq:502701291

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;

namespace MQTTWin
{
    public partial class Form1 : Form
    {
        private delegate void AddListCallback(String str);
        MqttClient client;
        public Form1()
        {
            InitializeComponent();

            string[] str = SerialPort.GetPortNames();
            if (str == null)
            {
                MessageBox.Show(“本机没有串口!“ “Error“);
                return;
            }
            //添加本机串口到控件  
            foreach (string s in SerialPort.GetPortNames())
            {
                comname.Items.Add(s);
            }

            client = new MqttClient(IPAddress.Parse(“106.14.67.89“));
            string clientId = Guid.NewGuid().ToString();
            client.Connect(clientId “admin“ “password“ false 30);

            // 注册消息接收处理事件,还可以注册消息订阅成功、取消订阅成功、与服务器断开等事件处理函数  
            client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;
            //生成客户端ID并连接服务器  
            client.Subscribe(new string[] { “/topic/w_res_1“ } new byte[] { MqttMsgbase.QOS_LEVEL_EXACTLY_ONCE });
        }

        void client_MqttMsgPublishReceived(object sender MqttMsgPublishEventArgs e)
        {
            //处理接收到的消息  

            string msg = System.Text.Encoding.Default.GetString(e.Message);
            AddInfoDemo(msg);
            if (msg == “1“)
            {
                send_Command(“1“);
            }
            if (msg == “2“)
            {
                send_Command(“2“);
            }
            if (msg == “3“)
            {
                send_Command(“3“);
            }
            if (msg == “4“)
            {
                send_Command(“4“);
            }

        }


        public void send_Command(string command)
        {
            if (serialPort1.IsOpen)
            {
                serialPort1.Write(command);
            }
            else
            {

                AddInfoDemo(“请先打开串口“);
            }
        }

        private void button1_Click(object sender EventArgs e)
        {
            client.Publish(“/topic/w_res_1“ Encoding.UTF8.GetBytes(textBox1.Text) MqttMsgbase.QOS_LEVEL_EXACTLY_ONCE false);
        }

        private void AddInfoDemo(String str)   //在listView1中显示操作记录
        {

            if (listView1.InvokeRequired)
            {
                AddListCallback d = new AddListCallback(AddInfoDemo);
                listView1.Invoke(d str);
            }
            else
            {
                listView1.Items.Insert(0 String.Format(“{0}:{1}“ DateTime.Now.ToString() str));
            }
        }

        private void open_Click(object sender

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        161  2017-11-26 21:00  MQTTWin\app.config

     文件      57344  2017-11-26 21:16  MQTTWin\bin\Debug\M2Mqtt.Net.dll

     文件     175616  2017-11-26 21:16  MQTTWin\bin\Debug\M2Mqtt.Net.pdb

     文件      12800  2017-11-26 21:16  MQTTWin\bin\Debug\MQTTWin.exe

     文件        161  2017-11-26 21:00  MQTTWin\bin\Debug\MQTTWin.exe.config

     文件      22016  2017-11-26 21:16  MQTTWin\bin\Debug\MQTTWin.pdb

     文件      24216  2017-11-26 21:17  MQTTWin\bin\Debug\MQTTWin.vshost.exe

     文件        161  2017-11-26 21:00  MQTTWin\bin\Debug\MQTTWin.vshost.exe.config

     文件        490  2017-03-19 05:00  MQTTWin\bin\Debug\MQTTWin.vshost.exe.manifest

     文件       3852  2017-11-26 21:16  MQTTWin\Form1.cs

     文件       7135  2017-11-26 21:16  MQTTWin\Form1.Designer.cs

     文件       6016  2017-11-26 21:16  MQTTWin\Form1.resx

     文件       4136  2017-11-26 21:00  MQTTWin\MQTTWin.csproj

     文件       1161  2017-11-26 21:00  MQTTWin\obj\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       6945  2017-11-26 21:00  MQTTWin\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件        573  2017-11-26 21:17  MQTTWin\obj\Debug\MQTTWin.csproj.FileListAbsolute.txt

     文件        977  2017-11-26 21:16  MQTTWin\obj\Debug\MQTTWin.csproj.GenerateResource.Cache

     文件       6086  2017-11-26 21:16  MQTTWin\obj\Debug\MQTTWin.csprojResolveAssemblyReference.cache

     文件      12800  2017-11-26 21:16  MQTTWin\obj\Debug\MQTTWin.exe

     文件        180  2017-11-26 21:16  MQTTWin\obj\Debug\MQTTWin.Form1.resources

     文件      22016  2017-11-26 21:16  MQTTWin\obj\Debug\MQTTWin.pdb

     文件        180  2017-11-26 21:16  MQTTWin\obj\Debug\MQTTWin.Properties.Resources.resources

     文件          0  2017-11-26 21:00  MQTTWin\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs

     文件          0  2017-11-26 21:00  MQTTWin\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs

     文件          0  2017-11-26 21:00  MQTTWin\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs

     文件       4608  2017-11-26 21:00  MQTTWin\obj\Debug\TempPE\Properties.Resources.Designer.cs.dll

     文件        488  2017-11-26 20:54  MQTTWin\Program.cs

     文件       1338  2017-11-26 20:54  MQTTWin\Properties\AssemblyInfo.cs

     文件       2854  2017-11-26 21:00  MQTTWin\Properties\Resources.Designer.cs

     文件       5612  2017-11-26 20:54  MQTTWin\Properties\Resources.resx

............此处省略12个文件信息

评论

共有 条评论