物联网框架ServerSuperIO(SSIO)更新、以及增加宿主程序和配置工具,详细介绍

一、更新内容

1.修改*Server类,以及承继关系。
2.增加IRunDevice的IServerProvider接口继承。
3.修复增加COM设备驱动可能造成的异常。
4.修复网络发送数据可能引发的异常。
5.完善协议驱动器。

6.修改协议驱动接口。
7.修改协议命令接口。
8.修复协议命令,抽象基类情况下的异常BUG。

9.增加协议接口GetPackageLength,数据交互更灵活。
10.修复一些BUG。
11.优化代码。

二、GetPackageLength接口的使用

     这个接口主要的使用场景是:当协议中有请求发送数据长度的命令,例如先向服务器发送数据包长度命令,得到返回确定后,再发送实际数据包信息。在连接发送大块数据的时候,例如文件内容、序列化后的内容等,内容有可能包含协议的头和尾,会影响数据包的完整性。主要用于交互连续的较大数据块内容。

      1.接口参数

1
2
3
4
5
6
7
8
/// <summary>
       /// 获得应该接收的数据长度,如果当前接收的数据小于这个返回值,那么继续接收数据,直到大于等于这个返回长度。如果接收数据超时,则直接返回当前已经接收的数据。
       /// </summary>
       /// <param name="data">接收的数据</param>
       /// <param name="channel">IO通道,用于返回确认数据</param>
       /// <param name="readTimeout">返回读数据超时间隔时间</param>
       /// <returns></returns>
       public abstract int GetPackageLength( byte [] data, IChannel channel, ref int readTimeout);

      2.接口使用

         (1)设置配置参数

1
2
3
4
5
6
7
8
9
IServer server = new ServerFactory().CreateServer( new ServerConfig()
            {
                ServerName = "服务1" ,
                SocketMode = SocketMode.Tcp,
                ControlMode = ControlMode.Loop,
                CheckSameSocketSession = false ,
                StartCheckPackageLength = true , //开启检测数据包长度
                NetReceiveBufferSize = 20,
            });

          (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
public override int GetPackageLength( byte [] data, IChannel channel, ref int readTimeout)
{
     if (data == null || data.Length <= 0)
         return 0;
 
     readTimeout = 2000;
 
     if (CheckData(data))
     {
         try
         {
             int length = BitConverter.ToInt32( new byte [] {data[3], data[4], data[5], data[6]}, 0);
 
             byte [] okBytes = System.Text.Encoding.ASCII.GetBytes( "ok" );
             int num = channel.Write(okBytes);
             if (num > 0)
             {
                 Console.WriteLine( "返回数据" );
             }
             return length;
         }
         catch (Exception)
         {
 
             return 0;
         }
     }
     else
     {
         Console.WriteLine( "校验错误" );
         return 0;
     }
}

 三、增加宿主程序(ServerSuperIO.Host)

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
static IServerFactory _serverFactory = null ;
         static void Main( string [] args)
         {
             Console.ForegroundColor = ConsoleColor.Green;
             ConsoleUtil.SetConsoleCtrlHandler( new ConsoleUtil.ControlCtrlDelegate(HandlerRoutine), true );
             bool success = true ;
             Console.WriteLine( "正在初始化服务程序......" );
             IObjectBuilder builder = new TypeCreator();
             _serverFactory = new ServerFactory();
             try
             {
                 GlobalConfig gc = GlobalConfigTool.Load();
                 foreach (ServerSuperIO.Config.Server serverCfg in gc.Servers)
                 {
                     IServer server = _serverFactory.CreateServer(serverCfg.ServerConfig);
                     server.AddDeviceCompleted += server_AddDeviceCompleted;
                     server.DeleteDeviceCompleted += server_DeleteDeviceCompleted;
                     server.Start();
                     _serverFactory.AddServer(server);
 
                     foreach (Config.Device devCfg in serverCfg.Devices)
                     {
                         try
                         {
                             IRunDevice runDev = builder.BuildUp<IRunDevice>(devCfg.AssemblyFile, devCfg.Instance);
 
                             runDev.DeviceParameter.DeviceID = devCfg.DeviceID;
                             runDev.DeviceDynamic.DeviceID = devCfg.DeviceID;
                             runDev.CommunicateType = devCfg.CommunicateType;
                             runDev.Initialize(devCfg.DeviceID);
 
                             if (server.AddDevice(runDev) != devCfg.DeviceID)
                             {
                                 Console.WriteLine( "增加设备:" + devCfg.DeviceID + " 失败!" );
                             }
                         }
                         catch (Exception ex)
                         {
                             Console.WriteLine(ex.Message);
                             continue ;
                         }
                     }
                 }
             }
             catch (Exception ex)
             {
                 success = false ;
                 Console.WriteLine(ex.Message);
             }
 
             if (success)
             {
                 Console.WriteLine( "初始化服务程序完成" );
             }
 
             while ( "exit" == Console.ReadLine())
             {
                 _serverFactory.RemoveAllServer();
                 break ;
             }
         }
 
         private static bool HandlerRoutine( int ctrlType)
         {
             if (ctrlType == 0 || ctrlType == 2)
             {
                 _serverFactory.RemoveAllServer();
             }
             return false ;
         }
}

 四、增加配置工具(ServerSuperIO.Tool)

1.增加服务,如下图:

 

2.增加设备,如下图:

 

3.单击树型菜单,修改配置属性。

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

iNeuOS工业互联网

提升动力值

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值