VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之并发通讯模式开发及注意事项(2)

6.4    宿主程序服务实例配置注意事项

     在宿主程序中创建服务实例的时候,需要把服务实例的配置参数设置为并发通讯模式,并启动服务实例,把实例化的设备驱动增加到该服务实例中。代码如下:

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
static void Main(string[] args)
{
            IServer server = new ServerFactory().CreateServer(new ServerConfig()
            {
                ServerName = "服务1",
                ComReadTimeout = 1000,
                ComWriteTimeout = 1000,
                NetReceiveTimeout = 1000,
                NetSendTimeout = 1000,
                ControlMode = ControlMode.Parallel,
                SocketMode = SocketMode.Tcp,
                StartReceiveDataFliter = false,
                ClearSocketSession = false,
                StartCheckPackageLength = false,
                CheckSameSocketSession = false,
                DeliveryMode = DeliveryMode.DeviceCode,
                ParallelInterval = 1000
            });
            server.AddDeviceCompleted += server_AddDeviceCompleted;
            server.DeleteDeviceCompleted += server_DeleteDeviceCompleted;
            server.Start();
 
            string devCode = "0";
            DeviceDriver dev1 = new DeviceDriver();
            dev1.DeviceParameter.DeviceName = "设备驱动"+ devCode.ToString();
            dev1.DeviceParameter.DeviceAddr = int.Parse(devCode);
            dev1.DeviceParameter.DeviceCode = devCode.ToString();
            dev1.DeviceParameter.DeviceID = devCode.ToString();
            dev1.DeviceDynamic.DeviceID = devCode.ToString();
            dev1.DeviceParameter.NET.RemoteIP = "127.0.0.1";
            dev1.DeviceParameter.NET.RemotePort = 9600;
            dev1.CommunicateType = CommunicateType.NET;
            dev1.Initialize(devCode.ToString());
            server.AddDevice(dev1);
 
            devCode = "1";
            DeviceDriver dev2 = new DeviceDriver();
            dev2.DeviceParameter.DeviceName = "设备驱动" + devCode.ToString();
            dev2.DeviceParameter.DeviceAddr = int.Parse(devCode);
            dev2.DeviceParameter.DeviceCode = devCode.ToString();
            dev2.DeviceParameter.DeviceID = devCode.ToString();
            dev2.DeviceDynamic.DeviceID = devCode.ToString();
            dev2.DeviceParameter.NET.RemoteIP = "192.168.1.102";
            dev2.DeviceParameter.NET.RemotePort = 9600;
            dev2.CommunicateType = CommunicateType.NET;
            dev2.Initialize(devCode.ToString());
            server.AddDevice(dev2);
 
            while ("exit" == Console.ReadLine())
            {
                server.Stop();
            }
}

相关教程