简体   繁体   中英

DTMF decode using python

I am trying to decode DTMF codes received to SIM-800 module using a python script.

My code :

import serial,time

serialport = serial.Serial("/dev/ttyS0", 115200,timeout=1)

while True:
    command = serialport.read(10)
    ring = "RING"
    ring_command = command.decode('utf-8')
    ring_command = ring_command.strip()

    if ring_command == ring:

        serialport.write("ATA"+"\r")
        print serialport.read(20)

        serialport.write("AT+DDET=1"+"\r\n")  # enable DTMF 
        time.sleep(2)

        while True:
            dtmf = serialport.read(20)

            if dtmf != "":
                dtmf_new = dtmf.strip('+DTMF:')
                print dtmf_new
                time.sleep(1)
            else:
                print "There were notthing"

But still I am getting output as : +DTMF: B . To decode RING command I have used given instructions in raspberry-exchange . Here also I have tried decode('utf-8') before strip() , but still same answer.

DTMF code is sent by another SIM-800 and Raspberry-pi.

Thanks in advance.

I have solved my issue with help from @stovfl and some colleagues in my office.

I have just changed how I strip the result code receive from DTMF.

import serial,time

serialport = serial.Serial("/dev/ttyS0", 115200,timeout=1)

while True:
    command = serialport.read(10)
    print(command)
    ring = "RING"
    ring_command = command.decode('utf-8')       # Incoming call
    ring_command = ring_command.strip()          # Decoding incoming RING message
    print("Ring decoded : "+ ring_command)

    if ring_command == ring:
        serialport.write("ATA"+"\r")
        print serialport.read(10)

        serialport.write("AT+DDET=1"+"\r\n")     # enable DTMF
        print serialport.read(10)

        while True:
            dtmf = serialport.read(20)

            if len(dtmf) > 8:
                print dtmf[9]             # Just print the code we sent, Original -> `+DTMF: C`
                time.sleep(1)
            else:
                print "DTMF is less than 8 char"
                time.sleep(1)

I above code, I tried to remove +DTMF: from my string, here I just get [9] th element from the receiving string.

This is 100% working code now, tested many times.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM