Esempio n. 1
0
        public static void ConnDevice()
        {
            //util.Connect();
            //XmlConfigurator.Configure();//Log4N 需要
            var configFile = Directory.GetCurrentDirectory() + "\\" + "Device.config";
            ConfigurationFileMap fileMap = new ConfigurationFileMap(configFile);
            DeviceConfiguration  config  = ConfigurationManager.OpenMappedMachineConfiguration(fileMap).GetSection("deviceSettingGroup/deviceConfig") as DeviceConfiguration;

            Console.WriteLine();
            deviceMap.Clear();

            runMode = RunMode.Normal;
            foreach (DeviceSettingElement foo in config.DeviceSettings)
            {
                IDevice dvcCtrl;

                if (!foo.Enable.Equals("1"))
                {
                    Console.WriteLine("---Device:" + foo.Name + " is disabled !-------------------");
                    continue;
                }
                DeviceConfig dc = new DeviceConfig();
                dc.DeviceName     = foo.Name;
                dc.ConnectionType = foo.Conn_Type;
                dc.Vendor         = foo.Vendor;
                if (foo.Conn_Type.Equals("Socket"))
                {
                    dc.IPAdress = foo.Conn_Address;
                    dc.Port     = Int32.Parse(foo.Conn_Port);
                    dvcCtrl     = new DeviceController(dc);
                    dvcCtrl.AssignedRecevicedEvent(ReceivedEventMessage);
                    dvcCtrl.start();

                    Marco.deviceMap.Add(foo.Name, dvcCtrl);
                }
                else if (foo.Conn_Type.Equals("ComPort"))
                {
                    if (foo.Conn_Address.Equals(""))
                    {
                        string[] ports = SerialPort.GetPortNames();
                        if (ports.Count() == 0)
                        {
                            dc.PortName = "";
                        }
                        else
                        {
                            dc.PortName = ports[0];
                        }
                    }
                    else
                    {
                        dc.PortName = foo.Conn_Address;
                    }
                    dc.BaudRate  = foo.Com_Baud_Rate;
                    dc.DataBits  = foo.Com_Data_Bits;
                    dc.ParityBit = foo.Com_Parity_Bit;
                    dc.StopBit   = foo.Com_Stop_Bit;
                    dvcCtrl      = new DeviceController(dc);
                    dvcCtrl.AssignedRecevicedEvent(ReceivedEventMessage);
                    dvcCtrl.start();
                    Marco.deviceMap.Add(foo.Name, dvcCtrl);
                }
                else if (foo.Conn_Type.Equals("ICPDeviceNet"))
                {
                    if (foo.Conn_Address.Equals(""))
                    {
                        string[] ports = SerialPort.GetPortNames();
                        if (ports.Count() == 0)
                        {
                            dc.PortName = "";
                        }
                        else
                        {
                            dc.PortName = ports[0];
                        }
                    }
                    else
                    {
                        dc.PortName = foo.Conn_Address;
                    }

                    //暫時固定路徑
                    if (machineType == MachineType.Normal)
                    {
                        dc.File = "mini_18port.xls";
                    }
                    else
                    {
                        dc.File = "mini.xls";
                    }
                    //dc.File = foo.File;
                    dvcCtrl = new I7565DNM(dc);
                    if (!dvcCtrl.start() && foo.Conn_Address.Equals(""))
                    {
                        string[] ports = SerialPort.GetPortNames();

                        for (int i = 1; i < ports.Count(); i++)
                        {
                            dc.PortName = ports[i];
                            //dvcCtrlarray[].= ports[0];
                            dvcCtrl = new I7565DNM(dc);

                            if (dvcCtrl.start())
                            {
                                Marco.deviceMap.Add(foo.Name, dvcCtrl);
                                break;
                            }
                        }
                    }
                    else
                    {
                        Marco.deviceMap.Add(foo.Name, dvcCtrl);
                    }
                }
            }
            ////設定停用
            //var xmlDoc = new XmlDocument();
            //xmlDoc.Load(configFile);
            //xmlDoc.SelectSingleNode("//deviceSettingGroup/deviceConfig/devices/device[@name='Robot02']").Attributes["enable"].Value = "0";
            //xmlDoc.Save(configFile);
            //ConfigurationManager.RefreshSection("deviceSettingGroup/deviceConfig");
        }