• 大小: 0.23M
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2024-04-25
  • 语言: C#
  • 标签: Client  master  CLI  tcl  IOT  

资源简介

这是一个物联网设备通讯协议实现客户端,将包括主流PLC通信读取、ModBus协议、Bacnet协议等常用工业通讯协议。

资源截图

代码片段和文件信息

using IoTClient.Models;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Text;
using System.Threading;

namespace IoTClient
{
    /// 
    /// SerialPort基类
    /// 

    public abstract class SerialPortbase
    {
        /// 
        /// 串行端口对象
        /// 

        protected SerialPort serialPort;

        /// 
        /// 是否自动打开关闭
        /// 

        protected bool isAutoOpen = true;

        /// 
        /// 获取设备上的COM端口集合
        /// 

        /// 
        public static string[] GetPortNames()
        {
            return SerialPort.GetPortNames();
        }

        /// 
        /// 连接
        /// 

        /// 
        protected Result Connect()
        {
            var result = new Result();
            serialPort?.Close();
            try
            {
                serialPort.Open();
            }
            catch (Exception ex)
            {
                result.IsSucceed = false;
                result.Err = ex.Message;
            }
            return result;
        }

        /// 
        /// 打开连接
        /// 

        /// 
        public Result Open()
        {
            isAutoOpen = false;
            return Connect();
        }

        /// 
        /// 关闭连接
        /// 

        /// 
        protected Result Dispose()
        {
            var result = new Result();
            try
            {
                serialPort.Close();
            }
            catch (Exception ex)
            {
                result.IsSucceed = false;
                result.Err = ex.Message;
            }
            return result;
        }

        /// 
        /// 关闭连接
        /// 

        /// 
        public Result Close()
        {
            isAutoOpen = true;
            return Dispose();
        }

        /// 
        /// 读取
        /// 

        /// 
        /// 
        protected byte[] SerialPortRead(SerialPort serialPort)
        {
            //延时处理
            if (serialPort.BytesToRead == 0) Thread.Sleep(20);
            if (serialPort.BytesToRead == 0) Thread.Sleep(40);
            if (serialPort.BytesToRead == 0) Thread.Sleep(80);
            byte[] buffer = new byte[serialPort.BytesToRead];
            var length = serialPort.Read(buffer 0 buffer.Length);
            return buffer;
        }
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2020-09-11 07:41  IoTClient-master\
     文件        4593  2020-09-11 07:41  IoTClient-master\.gitignore
     目录           0  2020-09-11 07:41  IoTClient-master\IoTClient.Tests\
     目录           0  2020-09-11 07:41  IoTClient-master\IoTClient.Tests\Helper\
     文件        5203  2020-09-11 07:41  IoTClient-master\IoTClient.Tests\Helper\CRCTests.cs
     文件        1453  2020-09-11 07:41  IoTClient-master\IoTClient.Tests\Helper\ConfigurationManager.cs
     文件        1422  2020-09-11 07:41  IoTClient-master\IoTClient.Tests\IoTClient.Tests.csproj
     目录           0  2020-09-11 07:41  IoTClient-master\IoTClient.Tests\ModBus\
     文件        3000  2020-09-11 07:41  IoTClient-master\IoTClient.Tests\ModBus\ModBusRtuClient_tests.cs
     文件        3716  2020-09-11 07:41  IoTClient-master\IoTClient.Tests\ModBus\ModBusTcpClient_tests.cs
     目录           0  2020-09-11 07:41  IoTClient-master\IoTClient.Tests\PLCTests\
     文件        7693  2020-09-11 07:41  IoTClient-master\IoTClient.Tests\PLCTests\SiemensClient_Tests.cs
     目录           0  2020-09-11 07:41  IoTClient-master\IoTClient.Tests\Server_tests\
     文件         775  2020-09-11 07:41  IoTClient-master\IoTClient.Tests\Server_tests\ModBusTcpServer_test.cs
     文件        6198  2020-09-11 07:41  IoTClient-master\IoTClient.Tests\SocketTempTests.cs
     目录           0  2020-09-11 07:41  IoTClient-master\IoTClient.Tests\TempTest\
     文件        1887  2020-09-11 07:41  IoTClient-master\IoTClient.Tests\TempTest\temp.cs
     目录           0  2020-09-11 07:41  IoTClient-master\IoTClient.Tool\
     文件         788  2020-09-11 07:41  IoTClient-master\IoTClient.Tool\App.config
     目录           0  2020-09-11 07:41  IoTClient-master\IoTClient.Tool\Common\
     目录           0  2020-09-11 07:41  IoTClient-master\IoTClient.Tool\Common\Helper\
     文件        1878  2020-09-11 07:41  IoTClient-master\IoTClient.Tool\Common\Helper\StringExtension.cs
     目录           0  2020-09-11 07:41  IoTClient-master\IoTClient.Tool\Controls\
     文件        9470  2020-09-11 07:41  IoTClient-master\IoTClient.Tool\Controls\BACnetControl.Designer.cs
     文件       16123  2020-09-11 07:41  IoTClient-master\IoTClient.Tool\Controls\BACnetControl.cs
     文件        5698  2020-09-11 07:41  IoTClient-master\IoTClient.Tool\Controls\BACnetControl.resx
     文件       14191  2020-09-11 07:41  IoTClient-master\IoTClient.Tool\Controls\MQTTControl.Designer.cs
     文件        7233  2020-09-11 07:41  IoTClient-master\IoTClient.Tool\Controls\MQTTControl.cs
     文件        5698  2020-09-11 07:41  IoTClient-master\IoTClient.Tool\Controls\MQTTControl.resx
     文件       20351  2020-09-11 07:41  IoTClient-master\IoTClient.Tool\Controls\MitsubishiMCControl.Designer.cs
     文件       10483  2020-09-11 07:41  IoTClient-master\IoTClient.Tool\Controls\MitsubishiMCControl.cs
............此处省略112个文件信息

评论

共有 条评论