Exemplo n.º 1
0
        public FormOptions(string fileName)
        {
            Init();
            _fileName = fileName;
            CustomSettings settings = LoadSettings(_fileName);

            Settings = settings;
            SelectStopBits.SelectedItem      = settings.StopBits;
            SelectParity.SelectedItem        = settings.Parity;
            comboBoxFlowControl.SelectedItem = settings.FlowControl;
            SelectBits.SelectedItem          = settings.DataBits;
            RefreshButton_Click(this, null);
            foreach (object item in SelectPort.Items)
            {
                if (((COMPortItem)item).Name == settings.ComPort)
                {
                    SelectPort.SelectedItem = item;
                }
            }
            SelectBaudRate.SelectedItem           = settings.BaudRate;
            SelectTimeout.Text                    = settings.TimeOut.ToString(CultureInfo.InvariantCulture);
            SelectNumberRepeat.Text               = settings.NumberRepeat.ToString(CultureInfo.InvariantCulture);
            checkBoxEndingSymbolEnabled.Checked   = settings.EndingSymbolEnable;
            checkBoxEndingSymbolEnabledTx.Checked = settings.EndingSymbolEnableTx;
            textBoxEndingSymbol.Text              = settings.EndingSymbol.ToString(CultureInfo.InvariantCulture);
            textBoxEndingSymbolTx.Text            = settings.EndingSymbolTx.ToString(CultureInfo.InvariantCulture);
            Binding();
        }
Exemplo n.º 2
0
        public static CustomSettings LoadSettings(string fileName)
        {
            var       settings  = new CustomSettings();
            var       xmlSer    = new XmlSerializer(settings.GetType());
            XmlReader xmlReader = new XmlTextReader(fileName);

            try
            {
                var v = (CustomSettings)xmlSer.Deserialize(xmlReader);
                xmlReader.Close();
                return(v);
            }
            catch (Exception exc)
            {
                if (exc.InnerException.GetType() == typeof(FileNotFoundException))
                {
                    settings = new CustomSettings
                    {
                        BaudRate           = SettingsOptions.Default.baudRate,
                        ComPort            = SettingsOptions.Default.comPort,
                        DataBits           = SettingsOptions.Default.dataBits,
                        FlowControl        = SettingsOptions.Default.flowControl,
                        NumberRepeat       = SettingsOptions.Default.numberRepeat,
                        Parity             = SettingsOptions.Default.parity,
                        StopBits           = SettingsOptions.Default.stopBits,
                        TimeOut            = SettingsOptions.Default.timeOut,
                        EndingSymbol       = SettingsOptions.Default.endingSymbol,
                        EndingSymbolEnable = SettingsOptions.Default.endingSymbolEnable
                    };
                    return(settings);
                }
            }
            return(null);
        }
Exemplo n.º 3
0
 private void buttonOK_Click(object sender, EventArgs e)
 {
     if (string.IsNullOrEmpty(_fileName))
     {
         SettingsOptions.Default.stopBits     = (StopBits)SelectStopBits.SelectedItem;
         SettingsOptions.Default.parity       = (Parity)SelectParity.SelectedItem;
         SettingsOptions.Default.flowControl  = (Handshake)comboBoxFlowControl.SelectedItem;
         SettingsOptions.Default.baudRate     = (int)SelectBaudRate.SelectedItem;
         SettingsOptions.Default.dataBits     = (int)SelectBits.SelectedItem;
         SettingsOptions.Default.comPort      = ((COMPortItem)SelectPort.SelectedItem).Name;
         SettingsOptions.Default.timeOut      = int.Parse(SelectTimeout.Text);
         SettingsOptions.Default.numberRepeat = int.Parse(SelectNumberRepeat.Text);
         SettingsOptions.Default.Save();
     }
     else
     {
         var settings = new CustomSettings
         {
             BaudRate     = (int)SelectBaudRate.SelectedItem,
             ComPort      = ((COMPortItem)SelectPort.SelectedItem).Name,
             DataBits     = (int)SelectBits.SelectedItem,
             FlowControl  = (Handshake)comboBoxFlowControl.SelectedItem,
             NumberRepeat = int.Parse(SelectNumberRepeat.Text),
             Parity       = (Parity)SelectParity.SelectedItem,
             StopBits     = (StopBits)SelectStopBits.SelectedItem,
             TimeOut      = int.Parse(SelectTimeout.Text)
         };
         Settings = settings;
         SaveSettings(_fileName, settings);
     }
 }
Exemplo n.º 4
0
        void LoadSettingsPort()
        {
            if (string.IsNullOrEmpty(_fileName))
            {
                _serialPort.PortName = SettingsOptions.Default.comPort;
                _serialPort.BaudRate = SettingsOptions.Default.baudRate;
                _serialPort.DataBits = SettingsOptions.Default.dataBits;
                _serialPort.Parity   = SettingsOptions.Default.parity;
                _serialPort.StopBits = SettingsOptions.Default.stopBits;
                // По умолчанию Handshake = Handshake.None
                // Но если его его не переприсвоить заново
                // Последовательный порт работал не стабильно
                // Наблюдались повторы посылок по ModBus
                // Возможно это связанно с ПО Аркадия (Виртуальный COM порт не принимал правильно настройки от Хоста)
                _serialPort.Handshake = SettingsOptions.Default.flowControl;
                _timeOut         = SettingsOptions.Default.timeOut;
                _numberRepeatMax = SettingsOptions.Default.numberRepeat;
            }
            else
            {
                CustomSettings settings = FormOptions.LoadSettings(_fileName);
                _serialPort.PortName  = settings.ComPort;
                _serialPort.BaudRate  = settings.BaudRate;
                _serialPort.DataBits  = settings.DataBits;
                _serialPort.Parity    = settings.Parity;
                _serialPort.StopBits  = settings.StopBits;
                _serialPort.Handshake = settings.FlowControl;
                _timeOut         = settings.TimeOut;
                _numberRepeatMax = settings.NumberRepeat;
            }

            _timerOut.Interval = _timeOut;
        }
Exemplo n.º 5
0
        public static void SaveSettings(string fileName, CustomSettings settings)
        {
            var xmlSer = new XmlSerializer(settings.GetType());
            var xmlSet = new XmlWriterSettings
            {
                NewLineOnAttributes = true,
                Indent       = true,
                NewLineChars = "\r\n",
                Encoding     = Encoding.UTF8
            };
            XmlWriter xmlWr = XmlWriter.Create(fileName, xmlSet);

            xmlSer.Serialize(xmlWr, settings);
            xmlWr.Close();
        }