public override void StartService() { string devId = "DataTransmitServerDriver"; DataTransmitServerDriver dev = new DataTransmitServerDriver(); dev.DeviceParameter.DeviceName = "转发数据服务驱动"; dev.DeviceParameter.DeviceAddr = 0; dev.DeviceParameter.DeviceID = devId; dev.DeviceParameter.DeviceCode = ""; dev.DeviceDynamic.DeviceID = devId; dev.DeviceParameter.NET.RemoteIP = "127.0.0.1"; dev.DeviceParameter.NET.RemotePort = 9600; dev.DeviceParameter.NET.ControllerGroup = "LocalGroup"; dev.CommunicateType = CommunicateType.NET; dev.Initialize(devId); _server = new ServerManager().CreateServer(new ServerSuperIO.Config.ServerConfig() { ServerName = "控制设备服务", ListenPort = _transmitServer == null ? 7007:_transmitServer.ListenPort, MaxConnects = _transmitServer == null ? 5 : _transmitServer.ConnectNum, ComReadTimeout = 1000, ComWriteTimeout = 1000, NetReceiveTimeout = 5000, NetSendTimeout = 5000, ControlMode = ControlMode.Singleton, SocketMode = SocketMode.Tcp, ReceiveDataFliter = false, ClearSocketSession = false, CheckPackageLength = false, CheckSameSocketSession = false, }); _server.SocketConnected += server_SocketConnected; _server.SocketClosed += server_SocketClosed; _server.Start(); _server.AddDevice(dev); StartTransmit(); }
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; } }