コード例 #1
0
        private void AddDeviceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddForm addForm = new AddForm(new DeviceProperty());

            addForm.ShowDialog();
            if (addForm.ConfigObject != null)
            {
                TreeNode tn = this.treeView1.SelectedNode;
                if (tn != null)
                {
                    DeviceProperty property = (DeviceProperty)addForm.ConfigObject;
                    Config.Device  device   = new Config.Device
                    {
                        Caption         = property.Caption,
                        DeviceID        = property.DeviceID,
                        AssemblyFile    = property.AssemblyFile,
                        CommunicateType = property.CommunicateType,
                        DeviceType      = property.DeviceType,
                        Instance        = property.Instance,
                        Remarks         = property.Remarks
                    };

                    try
                    {
                        IObjectBuilder builder = new TypeCreator();
                        IRunDevice     runDev  = builder.BuildUp <IRunDevice>(property.AssemblyFile, property.Instance);

                        runDev.DeviceParameter.DeviceID = property.DeviceID;
                        runDev.DeviceDynamic.DeviceID   = property.DeviceID;
                        runDev.CommunicateType          = property.CommunicateType;
                        if (runDev.CommunicateType == CommunicateType.COM)
                        {
                            runDev.DeviceParameter.COM.Port = ComUtils.PortToInt(property.IoParameter1);
                            runDev.DeviceParameter.COM.Baud = property.IoParameter2;
                        }
                        else if (runDev.CommunicateType == CommunicateType.NET)
                        {
                            runDev.DeviceParameter.NET.RemoteIP   = property.IoParameter1;
                            runDev.DeviceParameter.NET.RemotePort = property.IoParameter2;
                        }
                        runDev.DeviceParameter.DeviceCode   = property.DeviceCode;
                        runDev.DeviceParameter.DeviceAddr   = property.DeviceAddr;
                        runDev.DeviceParameter.DeviceName   = property.DeviceName;
                        runDev.DeviceParameter.NET.WorkMode = property.WorkMode;
                        runDev.Initialize(runDev.DeviceParameter.DeviceID);

                        TreeNode parentNode = GetRootNode(tn);
                        AddDevice(parentNode.Tag.ToString(), device);
                    }
                    catch (Exception ex)
                    {
                        WriteLog(ex.Message);
                    }
                }
            }
        }
コード例 #2
0
        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;
            }
        }
コード例 #3
0
        /// <summary>
        /// 增加设备
        /// </summary>
        /// <param name="dev"></param>
        /// <returns>设备ID</returns>
        public string AddDevice(IRunDevice dev)
        {
            if (dev == null)
            {
                throw new ArgumentNullException("设备驱动", "参数为空");
            }

            if (dev.DeviceParameter.DeviceID != dev.DeviceDynamic.DeviceID)
            {
                throw new NotEqualException("参数和动态数据的设备ID不相等");
            }

            String devid = String.Empty;

            if (dev.DeviceParameter.DeviceID == String.Empty ||
                dev.DeviceDynamic.DeviceID == String.Empty)
            {
                devid = this.DeviceManager.BuildDeviceID();
                dev.DeviceParameter.DeviceID = devid;
                dev.DeviceDynamic.DeviceID   = devid;
                try
                {
                    dev.Initialize(devid);
                }
                catch { throw; }
            }

            if (DeviceManager.ContainDevice(devid))
            {
                throw new ArgumentException("已经有相同的设备ID存在");
            }

            if (ServerConfig.ControlMode == ControlMode.Singleton)
            {
                if (dev.CommunicateType == CommunicateType.NET)
                {
                    IRunDevice[] netDevices = DeviceManager.GetDevices(CommunicateType.NET);

                    if (netDevices.Length >= 1) //如果是Singleton模式只能有一个网络设备驱动
                    {
                        throw new IndexOutOfRangeException("当为Singleton模式时,不能增加多个网络设备驱动");
                    }
                }
            }

            string desc = String.Empty;

            if (this.DeviceManager.AddDevice(dev.DeviceParameter.DeviceID, dev))
            {
                dev.Setup(this);

                this.BindDeviceHandler(dev, dev.DeviceType, true);

                if (dev.DeviceType == DeviceType.Virtual)
                {
                    desc = "增加虚拟设备";
                }
                else
                {
                    IController controller = null;
                    if (dev.CommunicateType == CommunicateType.COM)
                    {
                        #region 串口
                        string   key     = ComUtils.PortToString(dev.DeviceParameter.COM.Port);
                        IChannel channel = ChannelManager.GetChannel(key);
                        if (channel == null)
                        {
                            IComSession comChannel = new ComSession(dev.DeviceParameter.COM.Port, dev.DeviceParameter.COM.Baud);
                            comChannel.Setup(this);
                            comChannel.Initialize();
                            comChannel.COMOpen  += ComChannel_COMOpen;
                            comChannel.COMClose += ComChannel_COMClose;
                            comChannel.COMError += ComChannel_COMError;
                            comChannel.Open();
                            channel = (IChannel)comChannel;

                            ChannelManager.AddChannel(key, channel);
                        }

                        controller = ControllerManager.GetController(key);
                        if (controller == null)
                        {
                            controller = new ComController((IComSession)channel);
                            controller.Setup(this);
                            if (ControllerManager.AddController(controller.Key, controller))
                            {
                                controller.StartController();
                            }
                        }
                        else
                        {
                            IComController comController = (IComController)controller;
                            if (comController.ComChannel.GetHashCode() != channel.GetHashCode())
                            {
                                comController.ComChannel = (IComSession)channel;
                            }
                        }

                        desc = String.Format("增加'{0}'串口设备,串口:{1} 波特率:{2}", dev.DeviceParameter.DeviceName, dev.DeviceParameter.COM.Port.ToString(), dev.DeviceParameter.COM.Baud.ToString());
                        #endregion
                    }
                    else if (dev.CommunicateType == CommunicateType.NET)
                    {
                        #region 网络
                        controller = ControllerManager.GetController(SocketController.ConstantKey);
                        if (controller == null)
                        {
                            controller = new SocketController();
                            controller.Setup(this);
                            if (ControllerManager.AddController(controller.Key, controller))
                            {
                                controller.StartController();
                            }
                        }

                        desc = String.Format("增加'{0}'网络设备,IP地址:{1} 端口:{2}", dev.DeviceParameter.DeviceName, dev.DeviceParameter.NET.RemoteIP, dev.DeviceParameter.NET.RemotePort.ToString());
                        #endregion
                    }
                    else
                    {
                        desc = "无法识别设备的通讯类型";
                    }
                }

                desc += ",成功";
                OnAddDeviceCompleted(dev.DeviceParameter.DeviceID, dev.DeviceParameter.DeviceName, true);
            }
            else
            {
                desc += ",失败";
                OnAddDeviceCompleted(dev.DeviceParameter.DeviceID, dev.DeviceParameter.DeviceName, false);
            }

            this.Logger.Info(true, desc);

            return(devid != String.Empty ? devid : dev.DeviceParameter.DeviceID);
        }