Search code examples
pythonautomationsubprocesscommand-line-interfacestm32

subprocess input commands when running executable


STM32 cube programmer CLI. In command terminal I can type: C:\location\bin>STM32_Programmer_CLI.exe -c port=COM1 -w C:\location\test.bin 0x08000000 This programs the device when using the command line.

When I use my python script. It fails.

import os
import subprocess

#inputting commands when cli tool is running
connection = "C:\location\\bin>STM32_Programmer_CLI.exe"
subprocess.check_output([connection, '-c', '-c port=COM14' '-w C:\source\test.bin', '0x08000000'])

ERROR MESSAGE

# Traceback (most recent call last):
# File "C:\source\q.py", line 14, in <module>
# subprocess.check_output([connection, '-c', '-c port=COM14' '-w C:\source\test.bin', '0x08000000'])
# File "C:\source\python_version\py3110\Lib\subprocess.py", line 466, in check_output
# return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
# #            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# File "C:\source\python_version\py3110\Lib\subprocess.py", line 548, in run
# with Popen(*popenargs, **kwargs) as process:
# #          ^^^^^^^^^^^^^^^^^^^^^^^^^^^
# File "C:\source\python_version\py3110\Lib\subprocess.py", line 1024, in __init__
# self._execute_child(args, executable, preexec_fn, close_fds,
# File "C:\source\python_version\py3110\Lib\subprocess.py", line 1509, in _execute_child
# hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
# #                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expecting the device to program. When manually running the bin using the CLI tool the device program. Why is the above code not working ?


Solution

  • You are failing to:

    • differentiate between your prompt C:\location\bin> and your command STM32_Programmer_CLI.exe, and also
    • separate your parameters because -c port=COM1 is two separate parameters

    I don't have (or want) Windows so this is untested, but you will need something more like:

    cmd = [
       r'C:\location\bin\STM32_Programmer_CLI.exe',
       '-c', 'port=COM1',
       '-w', r'C:\location\test.bin',
       '0x08000000'
    ]
    
    subprocess.checkoutput(cmd)