当前位置:   article > 正文

C# 串口升级简易版_c# 添加串口的刷新按钮

c# 添加串口的刷新按钮

一、串口升级思路

1)第一步先能发送文件,并实现进度条功能;
2)制定升级协议,升级需要开始帧、结束校验帧、文件数据的校验等内容;
3)发送文件加上起始帧、结束校验帧和升级文件校验,增加容错性;
4)升级.bin文件测试。

二、代码实现

1)打开文件,文件路径显示在文本框

  1. //声明变量
  2. static bool flagUpdata = true;
  3. static bool flagUpgradeTimer = true;
  4. static byte[] startFrame = { 0x5A, 0x01, 0x02, 0x08, 0x02, 0x00, 0x01, 0x0, 0x0 };
  5. System.Timers.Timer UpgradeTimer = new System.Timers.Timer(100);
  6. static UInt16 upgradeBarTime = 0;
  7. static bool upgradePanelFlag = true;
  8. static bool upgradeEndFlag = true;
  9. static byte[] endFrame = { 0x5A, 0x01, 0x02, 0x08, 0x04, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0 };
  10. byte[] updataFileData = new byte[1024 * 200];
  11. int readByte = 0;
  1. //打开文件
  2. private void btnOpenFile_Click(object sender, EventArgs e)
  3. {
  4. OpenFileDialog ofd = new OpenFileDialog(); //点击弹出对话框
  5. ofd.Title = "请选择要打开的文本文件"; //设置对话框标题
  6. ofd.Multiselect = true; //设置对话框多选
  7. ofd.InitialDirectory = @"C:\Users\Andy\Desktop"; //设置对话框初始目录
  8. ofd.Filter = "文本文件|*.bin"; //打开对话框文件类型
  9. ofd.ShowDialog(); //展示对话框
  10. string path = ofd.FileName; //获得在打开对话框中选中文件的路径
  11. if (path == "")
  12. {
  13. return;
  14. }
  15. using (FileStream fsRead = new FileStream(path, FileMode.OpenOrCreate,FileAccess.Read))
  16. {
  17. readByte = fsRead.Read(updataFileData, 0, updataFileData.Length);//实际读取到的字节数
  18. txbFileAddr.Text = path; //文件路径显示在文本框
  19. }
  20. upgradeBarTime = 0;
  21. this.pgbProcessBar.Value = 0;
  22. flagUpdata = true;
  23. upgradeEndFlag = true;
  24. upgradePanelFlag = true;
  25. UpgradeTimer.Start();
  26. btnStartUpgrade.Text = "开始升级";
  27. }

2)开始帧校验成功--->串口发送文件--->结束校验帧

发送文件时,显示进度条,当前进度条是计时的,待优化,实际应该根据文件的大小,显示进度条。

  1. //发送开始帧,初始化Timer事件
  2. private void btnStartUpgrade_Click(object sender, EventArgs e)
  3. {
  4. if (txbFileAddr.Text=="")
  5. {
  6. MessageBox.Show("请打开升级文件");
  7. return;
  8. }
  9. if (btnStartUpgrade.Text == "正在升级")
  10. {
  11. MessageBox.Show("正在升级,请勿操作!");
  12. return;
  13. }
  14. else
  15. {
  16. byte[] crc16BmsData = CrcCheck.CRC16(startFrame);
  17. startFrame[7] = crc16BmsData[0]; //高八位
  18. startFrame[8] = crc16BmsData[1]; //低八位
  19. MainForm.SerialPort1.Write(startFrame, 0, startFrame.Length);
  20. if (flagUpgradeTimer)
  21. {
  22. flagUpgradeTimer = false;
  23. UpgradeTimer.Elapsed += new
  24. System.Timers.ElapsedEventHandler(UpgradeAnalysis);
  25. UpgradeTimer.Enabled = true; //设置执行System.Timers.Elapsed事件
  26. UpgradeTimer.AutoReset = true; //设置一直执行
  27. UpgradeTimer.Start();
  28. }
  29. }
  30. }
  31. //开始帧应答判断、发送文件、发送结束校验帧及应答数据判断
  32. private void UpgradeAnalysis(object sender, System.Timers.ElapsedEventArgs e)
  33. {
  34. //开始帧应答数据判断
  35. if (MainForm.readData.Length == 9)
  36. {
  37. //判断帧头、通信目标
  38. if ((MainForm.readData[0] == 0x5A) && (MainForm.readData[1] == 2))
  39. {
  40. byte[] crc16StartFrameData = CrcCheck.CRC16(MainForm.readData);
  41. if ((crc16StartFrameData[0] == MainForm.readData[MainForm.readData.Length - 2]) && (crc16StartFrameData[1] == MainForm.readData[MainForm.readData.Length - 1]))
  42. {
  43. //升级进度条
  44. if (upgradeBarTime == 100)
  45. {
  46. return;
  47. }
  48. upgradeBarTime++;
  49. this.pgbProcessBar.Value = upgradeBarTime;
  50. if (flagUpdata)
  51. {
  52. flagUpdata = false;
  53. btnStartUpgrade.Text = "正在升级";
  54. }
  55. if (upgradePanelFlag)
  56. {
  57. upgradePanelFlag = false;
  58. MainForm.SerialPort1.Write(updataFileData, 0, readByte);//发送升级文件
  59. byte[] crc16upgradeFile = CrcCheck.CRC16(updataFileData);
  60. endFrame[7] = crc16upgradeFile[0];
  61. endFrame[8] = crc16upgradeFile[1];
  62. byte[] crc16EndFrame = CrcCheck.CRC16(endFrame);
  63. endFrame[9] = crc16EndFrame[0];
  64. endFrame[10] = crc16EndFrame[1];
  65. MainForm.SerialPort1.Write(endFrame, 0, endFrame.Length);
  66. }
  67. }
  68. else
  69. {
  70. MainForm.readData = null;
  71. this.pgbProcessBar.Value = 0;
  72. txbFileAddr.Text = "";
  73. btnStartUpgrade.Text = "开始升级";
  74. MessageBox.Show("开始帧校验错误,请重新升级");
  75. return;
  76. }
  77. }
  78. else
  79. {
  80. MainForm.readData = null;
  81. this.pgbProcessBar.Value = 0;
  82. txbFileAddr.Text = "";
  83. btnStartUpgrade.Text = "开始升级";
  84. MessageBox.Show("帧头或通信目标错误,请重新升级");
  85. return;
  86. }
  87. }
  88. //结束校验帧
  89. if (MainForm.readData.Length == 11)
  90. {
  91. //判断帧头、通信目标、数据长度、第一个数据02
  92. if ((MainForm.readData[0] == 0x5A) && (MainForm.readData[1] == 2) && (MainForm.readData[4] == 4) && (MainForm.readData[6] == 2))
  93. {
  94. //文件校验值和上报的校验值比对
  95. if ((endFrame[7] == MainForm.readData[7]) && (endFrame[8] == MainForm.readData[8]))
  96. {
  97. //结束帧CRC16校验比对
  98. byte[] crc16StartFrameData = CrcCheck.CRC16(MainForm.readData);
  99. if ((crc16StartFrameData[0] == MainForm.readData[MainForm.readData.Length - 2]) && (crc16StartFrameData[1] == MainForm.readData[MainForm.readData.Length - 1]))
  100. {
  101. if (upgradeEndFlag)
  102. {
  103. upgradeEndFlag = false;
  104. btnStartUpgrade.Text = "升级完成";
  105. MessageBox.Show("升级完成!");
  106. }
  107. UpgradeTimer.Close();
  108. MainForm.readData = null;
  109. }
  110. else
  111. {
  112. MainForm.readData = null;
  113. this.pgbProcessBar.Value = 0;
  114. txbFileAddr.Text = "";
  115. btnStartUpgrade.Text = "开始升级";
  116. MessageBox.Show("结束校验帧错误,请重新升级");
  117. return;
  118. }
  119. }
  120. else
  121. {
  122. MainForm.readData = null;
  123. this.pgbProcessBar.Value = 0;
  124. txbFileAddr.Text = "";
  125. btnStartUpgrade.Text = "开始升级";
  126. MessageBox.Show("升级文件验帧错误,请重新升级");
  127. return;
  128. }
  129. }
  130. else
  131. {
  132. MainForm.readData = null;
  133. upgradeBarTime = 0;
  134. txbFileAddr.Text = "";
  135. btnStartUpgrade.Text = "开始升级";
  136. MessageBox.Show("帧头、通信目标、数据长度或数据错误,请重新升级");
  137. return;
  138. }
  139. }
  140. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/264326
推荐阅读
相关标签
  

闽ICP备14008679号