批处理文件以自动执行STM32闪烁

问题描述 投票:0回答:1

为了使用ST-Link Utility进行闪烁,我编写了以下有效的命令:

"C:\Program Files\STMicroelectronics\STM32 ST-Link Utility\ST-Link_CLI.exe" -c SN=XXXXXXXXXXXXX SWD UR FREQ=400 -P "C:\flash STM32\test.hex" -V -HardRst

我想通过使用ST-Link_CLI.exe命令和批处理文件所在文件夹中的.hex文件获取序列号来自动执行此操作,因此我可以单击此.bat文件以使用任何调试器进行刷新(SN)即已连接。

我可以使用以下方法获得序列号:

@echo off
setlocal EnableDelayedExpansion
set "output_cnt=0"
for /F "delims=" %%f in ('"C:\Program Files\STMicroelectronics\STM32 ST-LINK Utility\ST-LINK Utility\ST-LINK_CLI.exe" -List') do (
    set /a output_cnt+=1
    set "output[!output_cnt!]=%%f"
)
for /L %%a in (1 1 !output_cnt!) do call echo !output[%%a]!
pause
set "str=SN"
for /L %%n in (1 1 !output_cnt!) do (
    echo !output[%%n]!|find "SN" >nul
    if errorlevel 1 (echo notfound) else (echo found at!output[%%n]!)
)
pause

!output[%%a]!中的行包含字符串" SN:XXXXXXXXXXX"时,[output_cntSN

关于编写有效的批处理脚本,我是一个完全的新手,我想知道是否有人可以帮助我自动执行此任务。

batch-file stm32 firmware
1个回答
0
投票

您可以使用FOR /F循环拆分字符串。

...
set "serial=NONE"
for /L %%n in (1 1 !output_cnt!) do (
    echo !output[%%n]!
    FOR /F "tokens=1,* delims=: " %%1 in ("!output[%%n]!") do (
        if "%%1" == "SN" (
            set "serial=%%2"
        )
    )
)
echo Found serial: !serial!
© www.soinside.com 2019 - 2024. All rights reserved.