AutoSMTRXYSetting.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Windows.Forms;
  3. namespace FileWatcher
  4. {
  5. public partial class AutoSMTRXYSetting : Form
  6. {
  7. /// <summary>
  8. /// 设置参数解锁密码
  9. /// </summary>
  10. private const string SettingPassword = "1234";
  11. /// <summary>
  12. /// 是否已通过密码解锁
  13. /// </summary>
  14. private bool UnLocked = false;
  15. public AutoSMTRXYSetting()
  16. {
  17. InitializeComponent();
  18. StartPosition = FormStartPosition.CenterScreen;
  19. }
  20. private void AutoSMTRXYSetting_Load(object sender, EventArgs e)
  21. {
  22. ComPort.Text = GetCacheString("ComPort");
  23. BaudRate.Text = GetCacheString("BaudRate");
  24. Read.Text = GetCacheString("Read");
  25. Write.Text = GetCacheString("Write");
  26. Stop.Text = GetCacheString("Stop");
  27. ToZero.Text = GetCacheString("ToZero");
  28. UseScan.Text = GetCacheString("UseScan");
  29. CountType.Text = GetCacheString("CountType");
  30. if (BaudRate.Text == "")
  31. {
  32. BaudRate.Text = "9600";
  33. }
  34. if (CountType.Text == "")
  35. {
  36. CountType.Text = "PLC";
  37. }
  38. LockControls();
  39. }
  40. private string GetCacheString(string ParamName)
  41. {
  42. object value = BaseUtil.GetCacheData(ParamName);
  43. return value == null ? "" : value.ToString();
  44. }
  45. /// <summary>
  46. /// 锁定设置项(未输入密码前不可编辑)
  47. /// </summary>
  48. private void LockControls()
  49. {
  50. UnLocked = false;
  51. ComPort.Enabled = false;
  52. BaudRate.Enabled = false;
  53. Read.Enabled = false;
  54. Write.Enabled = false;
  55. Stop.Enabled = false;
  56. ToZero.Enabled = false;
  57. UseScan.Enabled = false;
  58. CountType.Enabled = false;
  59. Password.Text = "";
  60. Confirm.Text = "确定";
  61. }
  62. /// <summary>
  63. /// 密码正确后解锁设置项
  64. /// </summary>
  65. private void UnLockControls()
  66. {
  67. UnLocked = true;
  68. ComPort.Enabled = true;
  69. BaudRate.Enabled = true;
  70. Read.Enabled = true;
  71. Write.Enabled = true;
  72. Stop.Enabled = true;
  73. ToZero.Enabled = true;
  74. UseScan.Enabled = true;
  75. CountType.Enabled = true;
  76. Password.Text = "";
  77. Confirm.Text = "保存";
  78. }
  79. private void Confirm_Click(object sender, EventArgs e)
  80. {
  81. if (!UnLocked)
  82. {
  83. //未解锁时先校验密码
  84. if (Password.Text == SettingPassword)
  85. {
  86. UnLockControls();
  87. }
  88. else
  89. {
  90. MessageBox.Show(this, "密码不对", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  91. }
  92. return;
  93. }
  94. //解锁状态下点击则保存并关闭
  95. BaseUtil.SetCacheData("ComPort", ComPort.Text);
  96. BaseUtil.SetCacheData("BaudRate", BaudRate.Text);
  97. BaseUtil.SetCacheData("Read", Read.Text);
  98. BaseUtil.SetCacheData("Write", Write.Text);
  99. BaseUtil.SetCacheData("Stop", Stop.Text);
  100. BaseUtil.SetCacheData("ToZero", ToZero.Text);
  101. BaseUtil.SetCacheData("UseScan", UseScan.Text);
  102. BaseUtil.SetCacheData("CountType", CountType.Text);
  103. MessageBox.Show(this, "保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  104. DialogResult = DialogResult.OK;
  105. Close();
  106. }
  107. private void Cancel_Click(object sender, EventArgs e)
  108. {
  109. Close();
  110. }
  111. }
  112. }