mvdm
Posts: 21
Joined: Fri Nov 25, 2016 1:34 pm

Perform Terminal Command, keep terminal open

Sun Jul 16, 2017 12:10 pm

Hi,

I'm trying to make a simple python script that will manage my FM radio on my pi that I'm playing through rtl_fm and play in the terminal.

if I put the following "rtl_fm -g 50 -f 101M -M fm -s 170k -A fast -l 0 -E deemp | play -r 170k -t raw -e s -b 16 -c 1 -V1 - lowpass 16k" in the terminal I can get the music to play. To stop it in the terminal I have to press CTRL+C.

Image


The following code will let the music play, but I can't stop it without having to reboot:

Code: Select all

import os, subprocess, time
cmdPlayRadioFM = "rtl_fm -g 50 -f 101M -M fm -s 170k -A fast -l 0 -E deemp |  play -r 170k -t raw -e s -b 16 -c 1 -V1 - lowpass 16k"

Monitor = subprocess.check_output(cmdPlayRadioFM, shell=True)
print Monitor
I have tried the following, but the python script finishes without error and the music keeps playing and I have to reboot to stop it:

Code: Select all

import os, subprocess, time
cmdPlayRadioFM = "rtl_fm -g 50 -f 101M -M fm -s 170k -A fast -l 0 -E deemp |  play -r 170k -t raw -e s -b 16 -c 1 -V1 - lowpass 16k"

from subprocess import Popen, PIPE, signal

process = Popen([cmdPlayRadioFM], stdin=PIPE, stdout=PIPE, shell=True)

process.stdin.write(cmdPlayRadioFM)
time.sleep(5)  #play radio for 5 seconds then stop

#for line in process.stdout:
#    print '0 ->',repr(line)


process.stdin.write(chr(03))
process.send_signal(signal.SIGINT)
process.stdin.close()
process.wait()
I would also like to be able to read the information that is put on the terminal by the above command string back into my script as well. But the code hangs at "for line in process.stdout:" no error messages, it just sits there and does nothing, which is why I commented it out.

Thanks in advance
Attachments
test.png
test.png (63.61 KiB) Viewed 421 times

User avatar
paddyg
Posts: 2420
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Perform Terminal Command, keep terminal open

Sun Jul 16, 2017 3:37 pm

if you start the radio on the command line how do you stop it? If you break then you could try killing the subprocess. Along the lines

Code: Select all

p = Popen(cmdPlayRadioFM.split(), stdout=PIPE)
t = time.time()
while time.time() < (t + 5.0):
  c = p.stdout.read()
  if len(c) > 0:
    print(c, end='')
p.kill()
But if you really do have to reboot to stop it playing then I don't suppose that will work. (Have you tried:
$ top # note the process id for rtl_fm
.. q # to exit top
$ sudo kill 6666 # whatever the process id was)
If you normally stop the radio by typing q (as for top) then you could try piping 'q' to the process. Not really sure why you are sending the whole cmdPlayetcetc to the process. I would expect just the commands you would normally type into the running radio. Sometimes the output doesn't seem to arrive via the stdout, probably where the output is being positioned in the terminal (say, using ctypes rather than plain print())
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Return to “Python”