package mcu_ctrl_board import ( "errors" "fmt" "os" "path/filepath" "time" "hnyfkj.com.cn/rtu/linux/baseapp" "hnyfkj.com.cn/rtu/linux/ymodem" ) const ( DefaultSendFileName = "rtthread.rbl" // 默认发送的升级文件名 ) func StartMCUUpgrade() error { fwPath := filepath.Join(baseapp.VAR_DIR, DefaultSendFileName) info, err := os.Stat(fwPath) if err != nil { return err } if !info.Mode().IsRegular() { return fmt.Errorf("固件文件无效: %s", fwPath) } if info.Size() <= 0 { return fmt.Errorf("固件文件为空: %s", fwPath) } fwSize := info.Size() startTime := time.Now() baseapp.Logger.Warnf("====[ MCU UPGRADE PREPARE ]==== [%s] stop MCU communication", MODULE_NAME) ModuleExit() // 先退出MCU控制板的通信 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", fwPath, fwSize) bytes := ymodem.Send_file(fwPath) // 使用ymodem发送升级文件 if bytes > 0 && bytes == int(fwSize) { baseapp.Logger.Warnf("====[ MCU UPGRADE SUCCESS ]====, %d bytes written", bytes) } else { baseapp.Logger.Errorf("!!!![ MCU UPGRADE FAILED ]!!!!, written=%dBytes, expect=%dBytes", bytes, fwSize) } elapsed := time.Since(startTime) baseapp.Logger.Warnf("====[ MCU UPGRADE SUCCESS ]====, 用时: %v", elapsed) // 成功 if ok := ModuleInit(Board.ExtInfo); !ok { return errors.New("mcu serial open failed") } return nil }