-1

I am trying to run the code rtl_fm -f 162.475M -M fm -s 22050 -E dc -p 0 - | multimon-ng -t raw -a EAS in Python, but I can't get the data from RTL_FM to pass to Multimon-ng. Am I doing something wrong?

Here is the code snippet:

def main():
    args = parse_arguments()
    logging.basicConfig(level=args.loglevel, format='%(levelname)s: %(message)s')
    if args.msg:
        same_decode(args.msg, args.lang, same_watch=args.same, event_watch=args.event, text=args.text, call=args.call,
                    command=args.command, jsonfile=args.json)
    elif args.source:
        if args.source == 'rtl':
            sys.stdout.write('RTL\n')
            sys.stdout.write("rtl_fm -f " + str(args.frequency[0]) + "M -M fm -s 22050 -E dc -p " + str(args.ppm[0]) + " - | multimon-ng -t raw -a EAS\n")
            try:
                # source_process = subprocess.call("rtl_fm -f " + str(args.frequency[0]) + "M -M fm -s 22050 -E dc -p " + str(args.ppm[0]) + " -| multimon-ng -t raw -a EAS", stdout=subprocess.PIPE, shell=True, creationflags=subprocess.CREATE_NEW_CONSOLE)
                rtl_fm_cmd = ['rtl_fm', '-f', '162.475M', '-M', 'fm', '-s', '22050', '-E', 'dc', '-p', '0', '-']
                multimon_ng_cmd = ['multimon-ng', '-t', 'raw', '-a', 'EAS']
                time.sleep(1)
                rtl_fm_process = subprocess.Popen(rtl_fm_cmd)
                time.sleep(1)
                multimon_ng_process = subprocess.Popen(multimon_ng_cmd, stdin=rtl_fm_process.stdout,
                                                       stdout=subprocess.PIPE)
                source_process = multimon_ng_process
            except Exception as detail:
                logging.error(detail)
                return
        elif args.source == 'soundcard':
            sys.stdout.write('Soundcard')
            try:
                source_process = subprocess.Popen('multimon-ng -a EAS', stdout=subprocess.PIPE)
            except Exception as detail:
                logging.error(detail)
                return
        else:
            sys.stdout.write('ERROR')
            time.sleep(10)
            exit()
        while True:
            line = source_process.stdout.readline()
            # sys.stdout.write('rtl_fm -f ' + args.frequency + 'M -M fm -s 22050 -E dc -p ' + args.ppm + ' -| multimon-ng -t raw -a EAS')
            if line:
                line1 = line.decode('ascii')
                logging.debug(line1)
                same_decode(line1, args.lang, same_watch=args.same, event_watch=args.event, text=args.text,
                            call=args.call, command=args.command, jsonfile=args.json)
    else:
        while True:
            for line in sys.stdin:
                logging.debug(line)
                same_decode(line, args.lang, same_watch=args.same, event_watch=args.event, text=args.text,
                            call=args.call, command=args.command, jsonfile=args.json)

Soundcard works fine, so it's not an issue with Multimon-ng, and I ran the command in CMD and it works fine as well. It seems my Python code is the main culprit here.

Compo
  • 36,585
  • 5
  • 27
  • 39
Jamieden
  • 11
  • 3
  • Does this answer your question? [How to use \`subprocess\` command with pipes](https://stackoverflow.com/questions/13332268/how-to-use-subprocess-command-with-pipes) – mkrieger1 May 21 '23 at 18:31
  • Unfortunately, no. The piping method in that answer results in the same outcome. – Jamieden May 21 '23 at 19:03
  • Did you use `subprocess.PIPE` as `stdout` argument of `rtl_fm_process` and use `rtl_fm_process.stdout` as `stdin` argument of `multimon_ng_process`? – mkrieger1 May 21 '23 at 19:10
  • I just found the solution, and it is a stupid one – Jamieden May 21 '23 at 19:15

1 Answers1

0

I found the solution. It's because I didn't add the '-' character at the end of the multimon-ng command. It works perfectly now.

So instead of multimon_ng_cmd = ['multimon-ng', '-t', 'raw', '-a', 'EAS'], I need to use multimon_ng_cmd = ['multimon-ng', '-t', 'raw', '-a', 'EAS', '-']

Jamieden
  • 11
  • 3