| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- package mcu_ctrl_board
- import (
- "fmt"
- "os"
- "path/filepath"
- "time"
- "hnyfkj.com.cn/rtu/linux/baseapp"
- "hnyfkj.com.cn/rtu/linux/ymodem"
- )
- func StartMCUUpgrade() error {
- filePath := filepath.Join(baseapp.VAR_DIR, "mcu_firmware.rbl")
- defer os.Remove(filePath) //升级完成后删除升级文件
- info, err := os.Stat(filePath)
- if err != nil {
- return err
- }
- if !info.Mode().IsRegular() {
- return fmt.Errorf("固件文件无效: %s", filePath)
- }
- if info.Size() <= 0 {
- return fmt.Errorf("固件文件为空: %s", filePath)
- }
- fileSize := info.Size()
- startTime := time.Now()
- upgradeOK := false
- Board.uartMu.Lock()
- defer Board.uartMu.Unlock()
- baseapp.Logger.Warnf("====[ MCU UPGRADE PREPARE ]==== [%s] stop MCU communication", MODULE_NAME)
- mcuCtrlBoard_ComExit() /////////////// 1-关闭与控制板的串口通信
- time.Sleep(100 * time.Millisecond)
- baseapp.Logger.Warnf("##########################################################")
- baseapp.Logger.Warnf("############# MCU FIRMWARE UPGRADE START #############")
- baseapp.Logger.Warnf("##########################################################")
- baseapp.Logger.Warnf("====[ MCU UPGRADE FILE ]====, file=%s, size=%dBytes", filePath, fileSize)
- bytes := ymodem.Send_file(filePath) // 2-使用ymodem发送升级文件
- if bytes > 0 && bytes == int(fileSize) {
- upgradeOK = true
- }
- elapsed := time.Since(startTime)
- if !upgradeOK {
- baseapp.Logger.Errorf("!!!![ MCU UPGRADE FAILED ]!!!!, written=%dBytes, expect=%dBytes", bytes, fileSize)
- return fmt.Errorf("MCU firmware upgrade failed, written=%dBytes, expect=%dBytes", bytes, fileSize)
- }
- baseapp.Logger.Warnf("====[ MCU UPGRADE SUCCESS ]====, 用时: %v", elapsed) //成功
- for range 5 { ////////// 3-升级结束后,重新打开与控制板的串口通信
- if baseapp.IsExit1() {
- return nil
- }
- _, err = mcuCtrlBoard_ComInit()
- if err == nil {
- baseapp.Logger.Warnf("====[ MCU UPGRADE DONE ]==== [%s] restart MCU communication", MODULE_NAME)
- return nil
- }
- mcuCtrlBoard_ComExit()
- time.Sleep(time.Second)
- }
- return fmt.Errorf("failed to reopen MCU communication port: %w", err)
- }
|