标签:
物联网涉及到各种设备、各种传感器、各种数据源、各种协议,并且很难统一,那么就要有一个结构性的框架解决这些问题。SSIO就是根据时代发展的阶段和现实实际情况的结合产物。
各种数据信息,如下图:
解决方案,配合SIO使用:
一、SSIO特点
二、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的数据信息,稳定性、扩展性都非常不错。
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
开源跨平台IOT通讯框架ServerSuperIO,集成到NuGet程序包管理器,以及Demo使用说明
标签:
原文地址:http://www.cnblogs.com/lsjwq/p/5787292.html