首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用python解码DTMF

使用python解码DTMF
EN

Stack Overflow用户
提问于 2019-09-16 14:55:15
回答 1查看 736关注 0票数 0

我正在尝试使用python脚本解码接收到SIM-800模块的DTMF代码。

我的代码:

代码语言:javascript
复制
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"

但是我得到的输出仍然是:+DTMF: B。为了解码RING命令,我在raspberry-exchange中使用了给定的指令。在这里,我也在strip()之前尝试了decode('utf-8'),但仍然是相同的答案。

DTMF代码由另一个SIM-800和Raspberry-pi发送。

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-18 18:04:48

在@stovfl和我办公室的一些同事的帮助下,我已经解决了我的问题。

我刚刚更改了从DTMF中剥离结果代码的方式。

代码语言:javascript
复制
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)

在上面的代码中,我试图从我的字符串中删除+DTMF:,这里我只是从接收字符串中获取[9]的th元素。

这是100%的工作代码,经过多次测试。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57951737

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档