upgrade_mcu.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package mcu_ctrl_board
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "time"
  7. "hnyfkj.com.cn/rtu/linux/baseapp"
  8. "hnyfkj.com.cn/rtu/linux/rtu/cbd/utils"
  9. "hnyfkj.com.cn/rtu/linux/ymodem"
  10. )
  11. func StartMCUUpgrade(filePath string) error {
  12. if !Board.upgradeMu.TryLock() {
  13. return fmt.Errorf("upgrade busy")
  14. }
  15. defer func() {
  16. os.Remove(filePath) /////// 升级完成后删除
  17. Board.upgradeMu.Unlock()
  18. }()
  19. info, err := os.Stat(filePath)
  20. if err != nil {
  21. return err
  22. }
  23. if !info.Mode().IsRegular() {
  24. return fmt.Errorf("升级文件无效: %s", filePath)
  25. }
  26. if info.Size() <= 0 {
  27. return fmt.Errorf("升级文件为空: %s", filePath)
  28. }
  29. fileSize := info.Size()
  30. startTime := time.Now()
  31. Board.uartUseMu.Lock()
  32. defer Board.uartUseMu.Unlock()
  33. mcuCtrlBoard_ComExit() /////////////// 1-关闭与控制板的串口通信
  34. time.Sleep(100 * time.Millisecond)
  35. utils.PlayAudioAsync(filepath.Join(baseapp.EXEC_DIR, "voices",
  36. "startupdate.wav")) ////// 播报开始升级语音
  37. baseapp.Logger.Warnf("##########################################################")
  38. baseapp.Logger.Warnf("############# MCU FIRMWARE UPGRADE START #############")
  39. baseapp.Logger.Warnf("##########################################################")
  40. baseapp.Logger.Warnf("====[ MCU UPGRADE FILE ]====, file=%s, size=%dBytes", filePath, fileSize)
  41. size := ymodem.Send_file(filePath) /// 2-使用ymodem发送升级文件
  42. if size <= 0 || size != int(fileSize) {
  43. utils.PlayAudioAsync(filepath.Join(baseapp.EXEC_DIR, "voices",
  44. "updatefailed.wav")) /// 播报升级失败语音
  45. baseapp.Logger.Errorf("!!!![ MCU UPGRADE FAILED ]!!!!, written=%dBytes, expect=%dBytes", size, fileSize)
  46. } else {
  47. utils.PlayAudioAsync(filepath.Join(baseapp.EXEC_DIR, "voices",
  48. "updatesuccess.wav")) // 播报升级成功语音
  49. baseapp.Logger.Warnf("====[ MCU UPGRADE SUCCESS ]====, 用时: %v", time.Since(startTime)) ////////// 成功
  50. }
  51. for range 3 { ////////// 3-升级结束后,重新打开与控制板的串口通信
  52. _, err = mcuCtrlBoard_ComInit()
  53. if err == nil {
  54. return nil
  55. }
  56. mcuCtrlBoard_ComExit()
  57. time.Sleep(time.Second)
  58. }
  59. return fmt.Errorf("failed to reopen MCU serial port: %w", err)
  60. }