Beispiel #1
0
        public SerialPortEx(SerialConfig config)
        {
            serialPort = new SerialPort()
            {
                Parity          = config.Parity,
                ReadBufferSize  = config.ReadBufferSize,
                WriteBufferSize = config.WriteBufferSize,
                ReadTimeout     = config.ReadTimeout,
                WriteTimeout    = config.WriteTimeout,
                PortName        = config.PortName,
                BaudRate        = config.BaudRate,
                DataBits        = config.DataBits,
                StopBits        = config.StopBits
            };

            //receivedDatas = new Queue<string>();
            serialPort.ErrorReceived += new SerialErrorReceivedEventHandler(serialPort_ErrorReceived);
            serialPort.DataReceived  += new SerialDataReceivedEventHandler(serialPort_DataReceived);
        }
Beispiel #2
0
        /// <summary>
        /// 加载配置文件
        /// </summary>
        private void LoadConfig()
        {
            string strCount = ConfigurationManager.AppSettings["channelcount"];

            int.TryParse(strCount, out channelCount);
            Configuration cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            System.Diagnostics.Debug.Assert(cfg != null);
            var  group = cfg.GetSectionGroup("COM");
            Type type  = typeof(SerialConfig);

            try
            {
                PropertyInfo[] propertyInfos = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
                if (group != null && group.Sections.Count > 0)
                {
                    foreach (DefaultSection section in group.Sections)
                    {
                        IDictionary dict         = ConfigurationManager.GetSection(section.SectionInformation.SectionName) as IDictionary;
                        var         serialConfig = new SerialConfig();
                        if (dict != null && dict.Count > 0)
                        {
                            foreach (DictionaryEntry item in dict)
                            {
                                var propertyInfo = propertyInfos.ToList().Find(p => p.Name.Equals(item.Key));
                                if (propertyInfo != null)
                                {
                                    if (propertyInfo.PropertyType == typeof(string))
                                    {
                                        propertyInfo.SetValue(serialConfig, item.Value, null);
                                    }
                                    else if (propertyInfo.PropertyType == typeof(int))
                                    {
                                        propertyInfo.SetValue(serialConfig, Convert.ToInt32(item.Value), null);
                                    }
                                    else if (propertyInfo.PropertyType == typeof(StopBits))
                                    {
                                        StopBits stopBits;
                                        Enum.TryParse <StopBits>(item.Value.ToString(), out stopBits);
                                        propertyInfo.SetValue(serialConfig, stopBits, null);
                                    }
                                    else if (propertyInfo.PropertyType == typeof(Parity))
                                    {
                                        Parity parity;
                                        Enum.TryParse <Parity>(item.Value.ToString(), out parity);
                                        propertyInfo.SetValue(serialConfig, parity, null);
                                    }
                                }
                            }

                            //App.config不支持以数字为名称的节点,添加数字节点时需添加前缀"A_"
                            if (section.SectionInformation.Name.StartsWith("A_"))
                            {
                                serialConfig.CardNumber = section.SectionInformation.Name.Substring(2);
                            }
                            else
                            {
                                serialConfig.CardNumber = section.SectionInformation.Name;
                            }

                            serialConfigs.Add(serialConfig);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }
        }