Ejemplo n.º 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);
                    }
                }
            }
        }