Пример #1
0
        /// <summary>
        /// 生成串口对象
        /// </summary>
        /// <returns></returns>
        public SerialPort CreateSerialPortObject(string devTag)
        {
            // 从配置文件读取串口信息
            string filePath = Application.StartupPath + "\\XML\\MySettings.xml";

            try
            {
                // 打开XML文件
                XmlSerializeHelper <SCommItem, SCommXml> .OpenXmlFile(filePath);

                // 读取XML文件
                SCommXml xml = XmlSerializeHelper <SCommItem, SCommXml> .ReadXML();

                // 获得指定名称的串口信息
                SCommItem item = (SCommItem)xml[devTag];

                // 校验位转换
                Parity parity = ConvertToParity(item.Parity);
                // 停止位
                StopBits stopBits = ConvertToStopBits(item.StopBits);

                // 生成串口
                return(new SerialPort(item.Name, item.Baud, parity, item.DataBits, stopBits));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Пример #2
0
        /// <summary>
        /// 打开Xml文件
        /// </summary>
        /// <returns>包含配置信息的类实例</returns>
        private SCommXml OpenSCommXml()
        {
            string fileName = Application.StartupPath + "\\XML\\MySettings.xml";

            // 打开XML文件
            XmlSerializeHelper <SCommItem, SCommXml> .OpenXmlFile(fileName);

            // 读取XML文件
            return(XmlSerializeHelper <SCommItem, SCommXml> .ReadXML());
        }