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

来源:互联网 发布:c语言if 编辑:程序博客网 时间:2024/06/07 12:30

      物联网涉及到各种设备、各种传感器、各种数据源、各种协议,并且很难统一,那么就要有一个结构性的框架解决这些问题。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("开始传输>>");
 
        stringfileName=DateTime.Now.ToString("yyMMddHHmmss") +".txt";
        intbufferSize = _sendBufferSize;
        byte[] sendBuffer =new byte[bufferSize];
        fs =new FileStream(_file, FileMode.Open,FileAccess.Read,FileShare.Read);
 
        longlength = fs.Length;
        intcount = 0;
        Stopwatch watch =new Stopwatch();
        watch.Start();
        while(length > 0)
        {
            intsendNum = fs.Read(sendBuffer, 0, sendBuffer.Length);
 
            byte[] package = GetDataPackage(fileName,sendBuffer, sendNum);
 
            count+=_tcpClient.Client.Send(package, 0, package.Length, SocketFlags.None);
 
            length -= sendNum;
 
            floatpercent = ((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();
        }
    }
}
 
staticbyte[] GetDataPackage(stringfileName,byte[] sendBuffer,int sendNum)
{
    byte[] sendPackage =new byte[sendNum + 24];
    sendPackage[0] = 0x35;
    sendPackage[1] = 0x35;
 
    stringcode = "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;
 
    returnsendPackage;
}

 

 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
//设备驱动
 publicclass ReceiveFileDriver:RunDevice
    {
        privateDynamic _Dyn;
        privateParameter _Parameter;
        privateProtocol _Protocol;
        publicReceiveFileDriver() : base()
        {
            _Dyn =new Dynamic();
            _Parameter =new Parameter();
            _Protocol =new Protocol();
        }
 
        publicoverride voidInitialize(intdevid)
        {
            this.Protocol.InitDriver(this,new FixedHeadAndEndReceiveFliter(TransFileDriver.Protocol.Head, TransFileDriver.Protocol.End));   //初始化协议驱动
        }
 
        //省略......
}
 
//协议驱动,并处理数据
publicclass Command : ProtocolCommand
{
        publicCommand()
        {
        }
        publicoverride stringName
        {
            get{ return "writefile"; }
        }
        publicoverride objectAnalysis(byte[] data,object obj)
        {
            try
            {
               //count += data.Length - 24;
               //Console.WriteLine(count.ToString()+","+data[0].ToString() + "," + data[data.Length - 1].ToString());
                 
                stringpath = Path.Combine(Environment.CurrentDirectory, "rev");
                if(!System.IO.Directory.Exists(path))
                {
                    System.IO.Directory.CreateDirectory(path);
                }
                stringfileName = System.Text.Encoding.ASCII.GetString(data, 6, 16);
                path=Path.Combine(path, fileName);
                using(FileStream fs = newFileStream(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;
            }
            return0;
        }
 
        publicoverride byte[] Package(stringcode, objectobj)
        {
            thrownew 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(newServerConfig()
            {
                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的数据信息,稳定性、扩展性都非常不错。

 

 

1.[连载]《C#通讯(串口和网络)框架的设计与实现》

2.[开源]C#跨平台物联网通讯框架ServerSuperIO(SSIO)介绍

2.应用SuperIO(SIO)和开源跨平台物联网框架ServerSuperIO(SSIO)构建系统的整体方案

3.C#工业物联网和集成系统解决方案的技术路线(数据源、数据采集、数据上传与接收、ActiveMQ、Mongodb、WebApi、手机App)

5.ServerSuperIO开源地址:https://github.com/wxzz/ServerSuperIO

物联网&集成技术(.NET) QQ群54256083

0 0