Form1.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Threading.Tasks;
  5. using System.Windows.Forms;
  6. using Core.ONH.Collection.comm;
  7. namespace Core.ONH.Collection.Send
  8. {
  9. public partial class Form1 : Form
  10. {
  11. private string path = "";
  12. public Form1(string[] Args)
  13. {
  14. if (Args.Length <= 0)
  15. {
  16. if (MessageBox.Show("无法找到文件路径,是否手动选择文件", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
  17. {
  18. OpenFileDialog openFileDialog = new OpenFileDialog();
  19. openFileDialog.InitialDirectory = "c://";
  20. openFileDialog.Filter = "数据文件|*.xml";
  21. openFileDialog.RestoreDirectory = true;
  22. openFileDialog.FilterIndex = 1;
  23. if (openFileDialog.ShowDialog() == DialogResult.OK)
  24. {
  25. path = openFileDialog.FileName;
  26. }
  27. else
  28. {
  29. this.Close();
  30. }
  31. }
  32. else
  33. {
  34. this.Close();
  35. }
  36. }
  37. else
  38. {
  39. foreach (string args in Args)
  40. {
  41. path += " " + args.Trim();
  42. }
  43. }
  44. InitializeComponent();
  45. }
  46. private void Form1_Load(object sender, EventArgs e)
  47. {
  48. Task t3 = new Task(AnalysisData);
  49. t3.Start();
  50. }
  51. private void AnalysisData()
  52. {
  53. try
  54. {
  55. string data;
  56. // 分析ONH原始文件中有效数据
  57. ONHDataHelper.AnalysisXml(path, out data);
  58. //通过串口发送数据
  59. SerialPortHelper.Instance().SendData(data, Environment.CurrentDirectory + "\\Send.ini");
  60. SafeRefreshControl(this, Hide);
  61. }
  62. catch (Exception ex)
  63. {
  64. SafeRefreshControl(this, Hide);
  65. MessageBox.Show(ex.Message);
  66. LogHelper.WriteException(typeof(Program), ex);
  67. }
  68. finally
  69. {
  70. //删除临时文件数据
  71. if (File.Exists(path))
  72. File.Delete(path);
  73. Process.GetCurrentProcess().Kill();
  74. this.Close();
  75. }
  76. }
  77. /// <summary>
  78. /// 安全刷新界面控件 当为子线程时委托刷新界面
  79. /// </summary>
  80. /// <param name="control"></param>
  81. /// <param name="action"></param>
  82. public void SafeRefreshControl(Control control, Action action)
  83. {
  84. if (control.InvokeRequired)
  85. {
  86. control.Invoke(action);
  87. }
  88. else
  89. action();
  90. }
  91. }
  92. }