Skip to content

Commit

Permalink
1. 添加保存配置信息的功能
Browse files Browse the repository at this point in the history
2. 添加自动加载配置信息的功能
  • Loading branch information
situyiran committed Sep 14, 2015
1 parent 0cf5969 commit 6db00d5
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 2 deletions.
159 changes: 159 additions & 0 deletions WPFSerialAssistant/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public partial class MainWindow : Window
/// </summary>
private void InitCore()
{
// 加载配置信息
LoadConfig();

// 其他模块初始化
InitClockTimer();
InitAutoSendDataTimer();
InitSerialPort();
Expand Down Expand Up @@ -141,5 +145,160 @@ private int GetAutoSendDataInterval()

return interval;
}

#region 配置信息
//
// 目前保存的配置信息如下:
// 1. 波特率
// 2. 奇偶校验位
// 3. 数据位
// 4. 停止位
// 5. 字节编码
// 6. 发送区文本内容
// 7. 自动发送时间间隔
// 8. 窗口状态:最大化|高度+宽度
// 9. 面板显示状态
//

/// <summary>
/// 保存配置信息
/// </summary>
private void SaveConfig()
{
// 配置对象实例
Configuration config = new Configuration();

// 保存波特率
AddBaudRate(config);

// 保存奇偶校验位
config.Add("parity", parityComboBox.SelectedIndex);

// 保存数据位
config.Add("dataBits", dataBitsComboBox.SelectedIndex);

// 保存停止位
config.Add("stopBits", stopBitsComboBox.SelectedIndex);

// 字节编码
config.Add("encoding", encodingComboBox.SelectedIndex);

// 保存发送区文本内容
config.Add("sendDataTextBoxText", sendDataTextBox.Text);

// 自动发送时间间隔
config.Add("autoSendDataInterval", autoSendIntervalTextBox.Text);
config.Add("timeUnit", timeUnitComboBox.SelectedIndex);

// 窗口状态信息
config.Add("maxmized", this.WindowState == WindowState.Maximized);
config.Add("windowWidth", this.Width);
config.Add("windowHeight", this.Height);
config.Add("windowLeft", this.Left);
config.Add("windowTop", this.Top);

// 面板显示状态
config.Add("serialPortConfigPanelVisible", serialPortConfigPanel.Visibility == Visibility.Visible);
config.Add("autoSendConfigPanelVisible", autoSendConfigPanel.Visibility == Visibility.Visible);
config.Add("serialCommunicationConfigPanelVisible", serialCommunicationConfigPanel.Visibility == Visibility.Visible);

// 保存配置信息到磁盘中
Configuration.Save(config, @"Config\default.conf");
}

/// <summary>
/// 将波特率列表添加进去
/// </summary>
/// <param name="conf"></param>
private void AddBaudRate(Configuration conf)
{
conf.Add("baudRate", baudRateComboBox.Text);
}

/// <summary>
/// 加载配置信息
/// </summary>
private void LoadConfig()
{
Configuration config = Configuration.Read(@"Config\default.conf");

if (config == null)
{
return;
}

// 获取波特率
string baudRateStr = config.GetString("baudRate");
baudRateComboBox.Text = baudRateStr;

// 获取奇偶校验位
int parityIndex = config.GetInt("parity");
parityComboBox.SelectedIndex = parityIndex;

// 获取数据位
int dataBitsIndex = config.GetInt("dataBits");
dataBitsComboBox.SelectedIndex = dataBitsIndex;

// 获取停止位
int stopBitsIndex = config.GetInt("stopBits");
stopBitsComboBox.SelectedIndex = stopBitsIndex;

// 获取编码
int encodingIndex = config.GetInt("encoding");
encodingComboBox.SelectedIndex = encodingIndex;

// 获取发送区内容
string sendDataText = config.GetString("sendDataTextBoxText");
sendDataTextBox.Text = sendDataText;

// 获取自动发送数据时间间隔
string interval = config.GetString("autoSendDataInterval");
int timeUnitIndex = config.GetInt("timeUnit");
autoSendIntervalTextBox.Text = interval;
timeUnitComboBox.SelectedIndex = timeUnitIndex;

// 窗口状态
if (config.GetBool("maxmized"))
{
this.WindowState = WindowState.Maximized;
}
double width = config.GetDouble("windowWidth");
double height = config.GetDouble("windowHeight");
double top = config.GetDouble("windowTop");
double left = config.GetDouble("windowLeft");
this.Width = width;
this.Height = height;
this.Top = top;
this.Left = left;

// 面板显示状态
if (config.GetBool("serialPortConfigPanelVisible"))
{
serialPortConfigPanel.Visibility = Visibility.Visible;
}
else
{
serialPortConfigPanel.Visibility = Visibility.Collapsed;
}

if (config.GetBool("autoSendConfigPanelVisible"))
{
autoSendConfigPanel.Visibility = Visibility.Visible;
}
else
{
autoSendConfigPanel.Visibility = Visibility.Collapsed;
}

if (config.GetBool("serialCommunicationConfigPanelVisible"))
{
serialCommunicationConfigPanel.Visibility = Visibility.Visible;
}
else
{
serialCommunicationConfigPanel.Visibility = Visibility.Collapsed;
}
}
#endregion
}
}
8 changes: 6 additions & 2 deletions WPFSerialAssistant/EventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ private void saveSerialDataMenuItem_Click(object sender, RoutedEventArgs e)

private void saveConfigMenuItem_Click(object sender, RoutedEventArgs e)
{

SaveConfig();
// 状态栏显示保存成功
Information("配置信息保存成功。");
}

private void loadConfigMenuItem_Click(object sender, RoutedEventArgs e)
{

LoadConfig();
// 状态栏显示加载成功
Information("配置信息加载成功。");
}

private void exitMenuItem_Click(object sender, RoutedEventArgs e)
Expand Down
6 changes: 6 additions & 0 deletions WPFSerialAssistant/configuration/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,12 @@ public static Configuration Read(string path)
{
string confStr = "";
Configuration conf = null;

if (System.IO.File.Exists(path) == false)
{
return null;
}

// 读取配置信息
using (System.IO.StreamReader sr = new System.IO.StreamReader(path, defaultConfEncoding))
{
Expand Down

0 comments on commit 6db00d5

Please sign in to comment.