zoukankan      html  css  js  c++  java
  • 开源跨平台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.客户端(发送文件)

            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.设备驱动

    //设备驱动
     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.宿主程序

     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的数据信息,稳定性、扩展性都非常不错。

  • 相关阅读:
    第3章 文件I/O(4)_dup、dup2、fcntl和ioctl函数
    第3章 文件I/O(3)_内核数据结构、原子操作
    MyBatis Geneator详解<转>
    MapReduce原理<转>
    maven配置nexus
    myeclipse 上安装 Maven3<转>
    Lucene 工作原理<转>
    获取本工程的真实路径
    webservice文件上传下载
    fastjson常用操作
  • 原文地址:https://www.cnblogs.com/lsjwq/p/5787292.html
Copyright © 2011-2022 走看看