• 大小: 16.82MB
    文件类型: .rar
    金币: 2
    下载: 0 次
    发布日期: 2024-02-03
  • 语言: C#
  • 标签: onenet  mqtt  

资源简介

基于mqttnet2.7.5编写的C#基础代码,仅适用于中国移动onenet平台的客户端代码!

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using MQTTnet;
using MQTTnet.Client;
using MQTTnet.Implementations;
using MQTTnet.ManagedClient;
using MQTTnet.Protocol;


namespace Mqttnet
{
    public partial class Form1 : Form
    {
        private IMqttClient mqttClient = null;
        List payload1;
        public Form1()
        {
            InitializeComponent();
        }

        private void BtnConnect_Click(object sender EventArgs e)
        {
            Task.Run(async () => { await ConnectMqttServerAsync(); });
        }

        private void BtnDisconnect_Click(object sender EventArgs e)
        {
            Task.Run(async () => { await mqttClient.DisconnectAsync(); });
        }

        private void MqttClient_Connected(object sender EventArgs e)
        {
            Invoke((new Action(() =>
                {
                    txtMessage.Clear();
                    txtMessage.AppendText(“已连接到MQTT服务器!“ + Environment.NewLine);
                    btnConnect.Enabled = false;
                }
            )));
            
        }

        private void MqttClient_Disconnected(object sender EventArgs e)
        {
            Invoke((new Action(() =>
                {
                    txtMessage.Clear();
                    txtMessage.AppendText(“已断开MQTT连接!“ + Environment.NewLine);
                    btnConnect.Enabled = true;
                }
            )));
        }

        private async Task ConnectMqttServerAsync()
        {
            if (mqttClient == null)
            {
                var factory = new MqttFactory();
                mqttClient = factory.CreateMqttClient();
                mqttClient.ApplicationMessageReceived += MqttClient_ApplicationMessageReceived;
                mqttClient.Connected += MqttClient_Connected;
                mqttClient.Disconnected += MqttClient_Disconnected;
            }
            try
            {
                var options = new MqttClientOptionsBuilder()
                    .WithClientId(txtMachineID.Text)
                    .WithTcpServer(“183.230.40.39“ 6002)
                    .WithCredentials(txtProductID.Text txtPassword.Text)
                    .WithCleanSession()
                    .Build();

                await mqttClient.ConnectAsync(options);

            }
            catch (Exception)
            {
                MessageBox.Show(“连接失败!“);
                throw;
            }
        }
        private async Task Publish()
        {
            string topic = “$dp“;

            byte[] variableName = BitConverter.GetBytes(ushort.Parse(txtVariableName.Text.Trim()));

            byte[] data = BitConverter.GetBytes(float.Parse(txtSendData.Text));

            if (string.IsNullOrEmpty(

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

    ..A..H.    121856  2019-07-26 15:31  Mqttnet\Mqttnet\.vs\Mqttnet\v16\.suo

     文件          0  2019-07-23 13:55  Mqttnet\Mqttnet\.vs\Mqttnet\v16\Server\sqlite3\db.lock

     文件     786432  2019-07-26 15:31  Mqttnet\Mqttnet\.vs\Mqttnet\v16\Server\sqlite3\storage.ide

     文件        189  2019-07-23 13:55  Mqttnet\Mqttnet\App.config

     文件     169472  2018-04-28 20:24  Mqttnet\Mqttnet\bin\Debug\MQTTnet.dll

     文件      20992  2019-07-26 15:30  Mqttnet\Mqttnet\bin\Debug\Mqttnet.exe

     文件        189  2019-07-23 13:55  Mqttnet\Mqttnet\bin\Debug\Mqttnet.exe.config

     文件      44544  2019-07-26 15:30  Mqttnet\Mqttnet\bin\Debug\Mqttnet.pdb

     文件       5626  2019-07-26 15:30  Mqttnet\Mqttnet\Form1.cs

     文件      14572  2019-07-26 15:30  Mqttnet\Mqttnet\Form1.Designer.cs

     文件       5817  2019-07-26 15:30  Mqttnet\Mqttnet\Form1.resx

     文件       7972  2019-07-23 19:55  Mqttnet\Mqttnet\Mqttnet.csproj

     文件       1119  2019-07-23 13:55  Mqttnet\Mqttnet\Mqttnet.sln

     文件       1435  2019-07-24 10:03  Mqttnet\Mqttnet\obj\Debug\DesignTimeResolveAssemblyReferences.cache

     文件      20943  2019-07-23 19:55  Mqttnet\Mqttnet\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件          0  2019-07-26 15:30  Mqttnet\Mqttnet\obj\Debug\Mqttnet.csproj.CopyComplete

     文件         42  2019-07-26 15:30  Mqttnet\Mqttnet\obj\Debug\Mqttnet.csproj.CoreCompileInputs.cache

     文件        885  2019-07-26 15:30  Mqttnet\Mqttnet\obj\Debug\Mqttnet.csproj.FileListAbsolute.txt

     文件       1012  2019-07-26 15:30  Mqttnet\Mqttnet\obj\Debug\Mqttnet.csproj.GenerateResource.cache

     文件      20992  2019-07-26 15:30  Mqttnet\Mqttnet\obj\Debug\Mqttnet.exe

     文件        180  2019-07-26 15:30  Mqttnet\Mqttnet\obj\Debug\Mqttnet.Form1.resources

     文件      44544  2019-07-26 15:30  Mqttnet\Mqttnet\obj\Debug\Mqttnet.pdb

     文件        180  2019-07-26 15:30  Mqttnet\Mqttnet\obj\Debug\Mqttnet.Properties.Resources.resources

     文件       9482  2018-12-17 16:10  Mqttnet\Mqttnet\packages\Microsoft.NETCore.Platforms.1.1.0\.signature.p7s

     文件       9451  2016-11-05 04:57  Mqttnet\Mqttnet\packages\Microsoft.NETCore.Platforms.1.1.0\dotnet_library_license.txt

     文件          0  2016-11-05 04:57  Mqttnet\Mqttnet\packages\Microsoft.NETCore.Platforms.1.1.0\lib\netstandard1.0\_._

     文件      17960  2019-07-23 14:00  Mqttnet\Mqttnet\packages\Microsoft.NETCore.Platforms.1.1.0\Microsoft.NETCore.Platforms.1.1.0.nupkg

     文件      10048  2016-11-05 04:57  Mqttnet\Mqttnet\packages\Microsoft.NETCore.Platforms.1.1.0\runtime.json

     文件       1617  2016-11-05 04:57  Mqttnet\Mqttnet\packages\Microsoft.NETCore.Platforms.1.1.0\ThirdPartyNotices.txt

     文件       9465  2018-11-16 17:04  Mqttnet\Mqttnet\packages\MQTTnet.2.7.5\.signature.p7s

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

评论

共有 条评论