DeviceUpgrade.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.IO.Ports;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace MV485.helper
  10. {
  11. //设备升级(xModemSender升级包的部分)
  12. public class DeviceUpgrade
  13. {
  14. private const byte SOH = 0x01;
  15. private const byte STX = 0x02;
  16. private const byte EOT = 0x04;
  17. private const byte ACK = 0x06;
  18. private const byte NAK = 0x15;
  19. private const byte CAN = 0x18;
  20. private const byte C = 0x43;
  21. private SerialPort _serialPort;
  22. private readonly int _packetSize = 128; //1024;
  23. private readonly int _maxRetry = 10;
  24. private readonly int _ackTimeout = 500; //2000;
  25. private IProgress<string> _progress;
  26. private CancellationToken _cancellationToken;
  27. public bool IsOpen => _serialPort?.IsOpen ?? false;
  28. public event Action<int, int> OnProgress;
  29. private string _message;
  30. private string _portName;
  31. private int _baudrate;
  32. public DeviceUpgrade()
  33. {
  34. }
  35. private bool OpenPort(string portName, int baudRate = 115200, int dataBits = 8, Parity parity = Parity.None, StopBits stopBits = StopBits.One)
  36. {
  37. ClosePort();
  38. try
  39. {
  40. _serialPort = new SerialPort(portName, baudRate, parity, dataBits, stopBits)
  41. {
  42. ReadTimeout = 1000,
  43. WriteTimeout = 1000
  44. };
  45. _serialPort.Open();
  46. _progress?.Report($"串口{portName}连接成功");
  47. Logger.Info($"串口{portName}连接成功");
  48. return true;
  49. }
  50. catch (Exception)
  51. {
  52. _progress?.Report($"串口{portName}连接失败");
  53. Logger.Info($"串口{portName}连接失败");
  54. return false;
  55. }
  56. }
  57. private void ClosePort()
  58. {
  59. try
  60. {
  61. if (_serialPort != null && _serialPort.IsOpen)
  62. {
  63. _serialPort.Close();
  64. _serialPort.Dispose();
  65. _serialPort = null;
  66. _message = $"串口{_portName}已关闭";
  67. _progress.Report(_message);
  68. Logger.Info(_message);
  69. }
  70. }
  71. catch(Exception ex)
  72. {
  73. _message = $"关闭串口失败: {ex.Message}";
  74. _progress?.Report(_message);
  75. Logger.Info(_message);
  76. }
  77. }
  78. public async Task StopSendFileAsync()
  79. {
  80. _cancellationToken?.c
  81. }
  82. //开始异步发送升级包文件
  83. public async Task<bool> StartSendFileAsync(IProgress<string> progress, string portName,int baudRate,byte[] fileData)
  84. {
  85. _portName = portName;
  86. _baudrate = baudRate;
  87. try
  88. {
  89. _progress = progress;
  90. _cancellationToken = new CancellationToken();
  91. bool blSend = false;
  92. if (OpenPort(portName, baudRate))
  93. {
  94. blSend = await SendFileAsync(fileData, progress, _cancellationToken);
  95. }
  96. return blSend;
  97. }
  98. catch (Exception ex)
  99. {
  100. progress.Report(ex.Message);
  101. }
  102. finally
  103. {
  104. ClosePort();
  105. //_cancellationToken = null;
  106. }
  107. return false;
  108. }
  109. private async Task<bool> SendFileAsync(byte[] fileData, IProgress<string> progress, CancellationToken cancellationToken)
  110. {
  111. //byte[] fileData = File.ReadAllBytes(filePath);
  112. //常用的 trick,用于模拟 向上取整除法
  113. int totalPackets = (fileData.Length + _packetSize - 1) / _packetSize;
  114. //这里一定是C,嵌入式设备已经定义好了,不需要处理其它情况
  115. progress.Report("等待设备发出 'NAK' 信号...");
  116. await Task.Delay(2000);
  117. byte handshakeChar = await WaitForByteAsync(cancellationToken, NAK);
  118. //progress.Report($"{handshakeChar:02x}");
  119. if(handshakeChar != NAK)
  120. {
  121. progress.Report("未收到 'NAK',设备未准备好。");
  122. return false;
  123. }
  124. progress.Report($"{(handshakeChar == NAK ?"已开始传输":"未收到NAK")}");
  125. for (int packetNum = 1; packetNum <= totalPackets; packetNum++)
  126. {
  127. int offset = (packetNum - 1) * _packetSize;
  128. int length = Math.Min(_packetSize, fileData.Length - offset);
  129. byte[] dataBlock = new byte[_packetSize];
  130. Array.Copy(fileData, offset, dataBlock, 0, length);
  131. if(length < _packetSize)
  132. {
  133. for(int i = length; i < _packetSize; i++)
  134. {
  135. dataBlock[i] = 0x1A;
  136. }
  137. }
  138. for (int attempt = 0; attempt < _maxRetry; attempt++)
  139. {
  140. //progress.Report($"发送第 {packetNum}/{totalPackets} 包,尝试 {attempt + 1}");
  141. byte[] packet = BuildPacket(packetNum, dataBlock);
  142. try
  143. {
  144. //progress?.Report($"发送{packet.Length}字节");
  145. //progress?.Report(BitConverter.ToString(packet).Replace("-", ""));
  146. _serialPort.Write(packet, 0, packet.Length);
  147. }
  148. catch (Exception ex)
  149. {
  150. progress.Report($"发送失败:{ex.Message}");
  151. continue;
  152. }
  153. var result = await WaitForByteAsync(cancellationToken,ACK,NAK,CAN);
  154. if (result == ACK)
  155. {
  156. //progress?.Report("接收到ACK");
  157. //发送成功
  158. OnProgress?.Invoke(packetNum, totalPackets);
  159. break;
  160. }
  161. if(result == CAN)
  162. {
  163. progress.Report("接收到CAN取消信号,终止发送。");
  164. _serialPort.Write(new[] { CAN, CAN }, 0, 2);
  165. return false;
  166. }
  167. if(result == NAK)
  168. {
  169. progress?.Report("接收到NAK");
  170. }
  171. if (attempt == _maxRetry - 1)
  172. {
  173. //progress.Report("发送失败,超出重试次数。");
  174. progress.Report("发送失败,超出重试次数。发送 CAN 通知设备...");
  175. _serialPort.Write(new[] { CAN, CAN }, 0, 2);
  176. return false;
  177. }
  178. }
  179. }
  180. //_serialPort.Write(new[] { EOT }, 0, 1);
  181. //progress.Report("发送 EOT,等待 ACK...");
  182. if (!await SendEotWithRetryAsync(cancellationToken))
  183. {
  184. progress.Report("未收到 EOT 的 ACK,升级失败。发送 CAN 通知设备...");
  185. _serialPort.Write(new[] { CAN, CAN }, 0, 2);
  186. return false;
  187. }
  188. progress.Report("升级完成。");
  189. return true;
  190. }
  191. private async Task<bool> SendEotWithRetryAsync(CancellationToken token)
  192. {
  193. //等待10秒
  194. for (int attempt = 0; attempt < _maxRetry; attempt++)
  195. {
  196. _serialPort.Write(new[] { EOT }, 0, 1);
  197. //await Task.Delay(100, token);
  198. _progress.Report("发送 EOT,等待 ACK...");
  199. var result = await WaitForByteAsync(token, ACK);
  200. if (result == ACK)
  201. {
  202. _progress.Report("收到EOT的ACK");
  203. return true;
  204. }
  205. }
  206. return false;
  207. }
  208. private byte[] BuildPacket(int packetNum, byte[] data)
  209. {
  210. byte[] packet = new byte[3 + _packetSize + 1];
  211. packet[0] = SOH; //STX; //帧头 //改为128,1024的包有问题
  212. packet[1] = (byte)(packetNum % 256); //包序号
  213. packet[2] = (byte)(255 - packet[1]); //包序号反码
  214. Array.Copy(data, 0, packet, 3, _packetSize);
  215. //计算CRC
  216. //ushort crc = CRC16_XModem(data, 0, _packetSize);
  217. //packet[3 + _packetSize] = (byte)(crc >> 8);
  218. //packet[4 + _packetSize] = (byte)(crc & 0xFF);
  219. int sum = 0;
  220. foreach(byte bt in data)
  221. {
  222. sum = (sum + bt) % 256;
  223. }
  224. packet[3 + _packetSize] = (byte)sum;
  225. return packet;
  226. }
  227. private async Task<byte> WaitForByteAsync(CancellationToken token, params byte[] expected)
  228. {
  229. int timeout = _ackTimeout;
  230. while (timeout > 0 && !token.IsCancellationRequested)
  231. {
  232. if (_serialPort.BytesToRead > 0)
  233. {
  234. int value = _serialPort.ReadByte();
  235. if (Array.Exists(expected, b => b == (byte)value))
  236. return (byte)value;
  237. }
  238. await Task.Delay(10, token);
  239. timeout -= 10;
  240. }
  241. return 0;
  242. }
  243. public static ushort CRC16_XModem(byte[] data, int offset, int count)
  244. {
  245. ushort crc = 0;
  246. for (int i = 0; i < count; i++)
  247. {
  248. crc ^= (ushort)(data[offset + i] << 8);
  249. for (int j = 0; j < 8; j++)
  250. {
  251. if ((crc & 0x8000) != 0)
  252. crc = (ushort)((crc << 1) ^ 0x1021);
  253. else
  254. crc <<= 1;
  255. }
  256. }
  257. return crc;
  258. }
  259. //-----------------------------------------------------
  260. }
  261. }