1

I want to automate capturing logs from serial port with 2 functions:

1) trigger to start capture

2) trigger to stop

First looks like

def start_capture_output():
  file = '/home/test/Desktop/log.txt'
  os.system('touch %s' % file)
  os.system('chmod +rwx %s' % file)
  os.system('cat </dev/ttyUSB0>%s' % file)

and it works, but I wonder how to stop this process without manually pressing Ctrl+C

Andersson
  • 51,635
  • 17
  • 77
  • 129
  • don't use .system. use a pipe so the external app runs independently of python. e.g. .system() blocks and won't return until whatever external command you're executing exits. – Marc B Jan 22 '16 at 14:30
  • 1
    Check out [pyserial](https://github.com/pyserial/pyserial). – vesche Jan 22 '16 at 14:38

1 Answers1

1

If you spawn the process with

child = subprocess.Popen("command")

Then you can call

child.terminate()
child.kill()
timlyo
  • 2,086
  • 1
  • 23
  • 35