upgrade_mcu.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package mcu_ctrl_board
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "time"
  8. "hnyfkj.com.cn/rtu/linux/baseapp"
  9. "hnyfkj.com.cn/rtu/linux/ymodem"
  10. )
  11. const (
  12. DefaultSendFileName = "rtthread.rbl" // 默认发送的升级文件名
  13. )
  14. func StartMCUUpgrade() error {
  15. fwPath := filepath.Join(baseapp.VAR_DIR, DefaultSendFileName)
  16. info, err := os.Stat(fwPath)
  17. if err != nil {
  18. return err
  19. }
  20. if !info.Mode().IsRegular() {
  21. return fmt.Errorf("固件文件无效: %s", fwPath)
  22. }
  23. if info.Size() <= 0 {
  24. return fmt.Errorf("固件文件为空: %s", fwPath)
  25. }
  26. fwSize := info.Size()
  27. startTime := time.Now()
  28. baseapp.Logger.Warnf("====[ MCU UPGRADE PREPARE ]==== [%s] stop MCU communication", MODULE_NAME)
  29. ModuleExit() // 先退出MCU控制板的通信
  30. time.Sleep(100 * time.Millisecond) /// 延时等待串口安全关闭
  31. baseapp.Logger.Warnf("##########################################################")
  32. baseapp.Logger.Warnf("############# MCU FIRMWARE UPGRADE START #############")
  33. baseapp.Logger.Warnf("##########################################################")
  34. baseapp.Logger.Warnf("====[ MCU UPGRADE FILE ]====, file=%s, size=%dBytes", fwPath, fwSize)
  35. bytes := ymodem.Send_file(fwPath) // 使用ymodem发送升级文件
  36. if bytes > 0 && bytes == int(fwSize) {
  37. baseapp.Logger.Warnf("====[ MCU UPGRADE SUCCESS ]====, %d bytes written", bytes)
  38. } else {
  39. baseapp.Logger.Errorf("!!!![ MCU UPGRADE FAILED ]!!!!, written=%dBytes, expect=%dBytes", bytes, fwSize)
  40. }
  41. elapsed := time.Since(startTime)
  42. baseapp.Logger.Warnf("====[ MCU UPGRADE SUCCESS ]====, 用时: %v", elapsed) // 成功
  43. if ok := ModuleInit(Board.ExtInfo); !ok {
  44. return errors.New("mcu serial open failed")
  45. }
  46. return nil
  47. }