开源跨平台IOT通讯框架ServerSuperIO,集成到NuGet程序包管理器,以及Demo使用说明

      物联网涉及到各种设备、各种传感器、各种数据源、各种协议,并且很难统一,那么就要有一个结构性的框架解决这些问题。SSIO就是根据时代发展的阶段和现实实际情况的结合产物。

      各种数据信息,如下图:

 

 

          解决方案,配合SIO使用:

一、SSIO特点

  • 轻型高性能通信框架,适用于多种应用场,轮询模式、自控模式、并发模式和单例模式。
  • 不光是通讯框架,是设备驱动、IO通道、控制模式场景的协调机制。
  • 支持协议驱动器,可以按规范写标准协议和自定义协议。
  • 支持发送数据缓存器,支持命令缓存重发和按优先级别发送。
  • 支持协议过滤器,按规则筛选数据,并且可以承继接口,自定义过滤方式。
  • 支持接收数据缓存器,可以缓存不符合过滤器的数据,和下次接收数据进行拼接。
  • 支持按设备命令优先级别进行调度设备,保证有高级别命令的驱动及时发送。
  • 支持一个设备驱动,同时支持串口和网络两种通讯方式,可以监视IO通道数据。
  • 支持一个设备驱动,在网络通讯时可以支持TCP Server和TCP Client两种工作模式。
  • 支持多设备共享同一IO通道进行通讯。
  • 支持定时清理超时的网络IO通道。
  • 支持显示视图接口,满足不同显示需求。
  • 支持服务组件接口,可以自定义完成OPC服务、4-20mA输出、LED大屏显示、短信服务、以及多功能网关服务。
  •  支持创建多服务实例,完成不同业务的拆分。
  •  支持跨平台部署,可以运行在Linux和Windows系统。

二、SSIO发布到NuGet平台

三、搜索SSIO

四、安装SSIO

五、事例代码(Demo)

    Demo下载地址:https://github.com/wxzz/ServerSuperIO/tree/2.0

1.客户端(发送文件)

 

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
static void SendFile()
{
    if (!System.IO.File.Exists(_file))
    {
        Console.WriteLine("文件不存在:"+_file);
        return;
    }
 
    FileStream fs = null;
    try
    {
        Console.WriteLine("开始传输>>");
 
        string fileName=DateTime.Now.ToString("yyMMddHHmmss") + ".txt";
        int bufferSize = _sendBufferSize;
        byte[] sendBuffer = new byte[bufferSize];
        fs = new FileStream(_file, FileMode.Open,FileAccess.Read,FileShare.Read);
 
        long length = fs.Length;
        int count = 0;
        Stopwatch watch = new Stopwatch();
        watch.Start();
        while (length > 0)
        {
            int sendNum = fs.Read(sendBuffer, 0, sendBuffer.Length);
 
            byte[] package = GetDataPackage(fileName,sendBuffer, sendNum);
 
            count+=_tcpClient.Client.Send(package, 0, package.Length, SocketFlags.None);
 
            length -= sendNum;
 
            float percent = ((fs.Length - length)/(float) fs.Length)*100.0f;
            Console.WriteLine("已传:" + percent.ToString("0.00")  + "%");
        }
        watch.Stop();
         
        Console.WriteLine("传输完毕!总数:" + count.ToString()+",耗时:"+ watch.Elapsed.TotalSeconds.ToString(CultureInfo.InvariantCulture));
    }
    catch
    {
        throw;
    }
    finally
    {
        if (fs != null)
        {
            fs.Close();
            fs.Dispose();
        }
    }
}
 
static byte[] GetDataPackage(string fileName,byte[] sendBuffer, int sendNum)
{
    byte[] sendPackage = new byte[sendNum + 24];
    sendPackage[0] = 0x35;
    sendPackage[1] = 0x35;
 
    string code = "0001";
    byte[] codeBytes = System.Text.Encoding.ASCII.GetBytes(code);
    Buffer.BlockCopy(codeBytes, 0, sendPackage, 2, 4);
 
    byte[] fileBytes= System.Text.Encoding.ASCII.GetBytes(fileName);
    Buffer.BlockCopy(fileBytes, 0, sendPackage, 6, 16);
 
    Buffer.BlockCopy(sendBuffer, 0, sendPackage, 22, sendNum);
 
    sendPackage[sendPackage.Length - 2] = 0x33;
    sendPackage[sendPackage.Length - 1] = 0x33;
 
    return sendPackage;
}

 

 2.设备驱动

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
//设备驱动
 public class ReceiveFileDriver:RunDevice
    {
        private Dynamic _Dyn;
        private Parameter _Parameter;
        private Protocol _Protocol;
        public ReceiveFileDriver() : base()
        {
            _Dyn = new Dynamic();
            _Parameter = new Parameter();
            _Protocol = new Protocol();
        }
 
        public override void Initialize(int devid)
        {
            this.Protocol.InitDriver(this, new FixedHeadAndEndReceiveFliter(TransFileDriver.Protocol.Head, TransFileDriver.Protocol.End));    //初始化协议驱动
        }
 
        //省略......
}
 
//协议驱动,并处理数据
public class Command : ProtocolCommand
{
        public Command()
        {
        }
        public override string Name
        {
            get { return "writefile"; }
        }
        public override object Analysis(byte[] data, object obj)
        {
            try
            {
               //count += data.Length - 24;
               //Console.WriteLine(count.ToString()+","+data[0].ToString() + "," + data[data.Length - 1].ToString());
                 
                string path = Path.Combine(Environment.CurrentDirectory, "rev");
                if (!System.IO.Directory.Exists(path))
                {
                    System.IO.Directory.CreateDirectory(path);
                }
                string fileName = System.Text.Encoding.ASCII.GetString(data, 6, 16);
                path=Path.Combine(path, fileName);
                using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write))
                {
                    fs.Seek(fs.Length, SeekOrigin.Current);
                    byte[] content = new byte[data.Length - 24];
                    Buffer.BlockCopy(data, 22, content, 0, content.Length);
                    fs.Write(content, 0, content.Length);
                    fs.Flush();
                }
 
            }
            catch
            {
                return -1;
            }
            return 0;
        }
 
        public override byte[] Package(string code, object obj)
        {
            throw new NotImplementedException();
        }
    }

 3.宿主程序

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
static void Main(string[] args)
{
             
            ReceiveFileDriver dev = new ReceiveFileDriver();
            dev.DeviceParameter.DeviceName = "设备4";
            dev.DeviceParameter.DeviceAddr = 0;
            dev.DeviceParameter.DeviceCode = "0001";
            dev.DeviceParameter.DeviceID = 0;
            dev.DeviceDynamic.DeviceID = 0;
            dev.DeviceParameter.NET.RemoteIP = "127.0.0.1";
            dev.DeviceParameter.NET.RemotePort = 9600;
            dev.CommunicateType = CommunicateType.NET;
            dev.Initialize(0);
 
            IServer server = new ServerFactory().CreateServer(new ServerConfig()
            {
                ServerName = "接收文件服务",
                ListenPort = 6699,
                NetReceiveBufferSize = 2048,
                ControlMode = ControlMode.Self,
                SocketMode = SocketMode.Tcp,
                DeliveryMode = DeliveryMode.DeviceCode,
                StartReceiveDataFliter = true,
                ClearSocketSession = false,
            });
 
            server.AddDeviceCompleted += server_AddDeviceCompleted;
            server.DeleteDeviceCompleted += server_DeleteDeviceCompleted;
            server.Start();
 
            server.AddDevice(dev);
 
            while ("exit" == Console.ReadLine())
            {
                server.Stop();
            }
}

 六、实验效果

 

      两天的时间,将近3GB的数据信息,稳定性、扩展性都非常不错。

 

 

posted @   iNeuOS工业互联网系统  阅读(1537)  评论(2编辑  收藏  举报
编辑推荐:
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
点击右上角即可分享
微信分享提示