0

Here is my code:

    class TestRegisters():
        def application(self):
            return(Popen(["i2c_control.exe"], shell=True, bufsize=0, stdout=PIPE, stdin=PIPE, stderr=PIPE, universal_newlines=True))

        def run_read_command(self, address, size):
            cmd = "read %s %s" % (address, size)
            out = self.application().communicate(cmd)[0]
            print('output is: {}\n'. format(out))
            # print('error is: \n', err)

            return(out)

    test = TestRegisters()
    for key in register:
        address = key
        size = int(register[key][1])*4
        access = register[key][0]  
        data_type = register[key][2]

        if access == 'Read' or access == 'Read/Write':
            test.application()
            read = test.run_read_command(address, size)

It prints the output correctly in terminal if I don't PIPE stdout, but when I PIPE stdout, communicate[0] prints empty output as following:

output is:

output is:

output is:

output is:

Not sure what am I doing wrong. Any help is appreciated, thanks.

tripleee
  • 175,061
  • 34
  • 275
  • 318
Manvir
  • 3
  • 3
  • 1
    You are hugely overcomplicating this, if you just want to run the command and capture its output, that's simply `output = subprocess.check_output(["i2c_control.exe"], text=True, input="read %s %s" % (address, size))`. Perhaps see also [Actual meaning of `shell=True` in `subprocess`](https://stackoverflow.com/questions/3172470/actual-meaning-of-shell-true-in-subprocess) as for why you want to avoid that, especially where it's completely unnecessary. – tripleee Jun 14 '22 at 18:37
  • 1
    Some commands print different results when they are not connected to a terminal; there's a separate duplicate about that. – tripleee Jun 14 '22 at 18:38
  • If you're using `shell=True`, the command should be a `str`, not a `list` of `str` (the behavior when using `shell=True` + `list` of `str` on POSIX is... weird). I don't *think* it causes any problems here (where the `list` only has one item), but you should generally stick to using `list` of `str` when `shell=False` (the default) and single `str` when `shell=True`. – ShadowRanger Jun 14 '22 at 19:00
  • @trplee: I tried the command you suggested, but it gives me following errors: File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 411, in check_output **kwargs).stdout File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 512, in run output=stdout, stderr=stderr) subprocess.CalledProcessError: Command '['i2c_control.exe']' returned non-zero exit status 3221225477. – Manvir Jun 14 '22 at 19:32
  • If it is expected to fail, try `subprocess.run` without `check=True`. The equivalent code is slightly more complex, but not by much. `s = subprocess.run([command], text=True, capture_output=True, input=whatever); print(s.stdout)` – tripleee Jun 15 '22 at 18:55
  • (Pinging a user from a comment only works if you don't botch the user name after the @.) – tripleee Jun 15 '22 at 18:59

0 Answers0