Pyserial + AT命令=收到第一个短信后不再接收短信

问题描述

我正在Python 3.8中使用一个非常简单的脚本来实时收集来自USB记忆棒调制解调器的传入消息。问题在于,一旦收到第一个SMS,就再也不会收到其他SMS。

这是我的代码(在两个不同的调制解调器中测试,结果相同):

type typeByString<T> = T extends 'number' ? number : T extends 'string' ? string : T extends 'boolean' ? boolean : T extends 'null' ? null : T extends 'number[]' ? number[] : T extends 'string[]' ? string[] : T extends 'boolean[]' ? boolean[] : any;

const myTypes = ["string","null"]  as const;
const selected = myTypes[0];
const c : typeByString<typeof selected> = "cool";
const d : typeByString<typeof myTypes[1]> = null;

运行脚本时,它似乎工作正常。所有AT命令均得到成功回复

import serial
import time

ser = serial.Serial(port = "COM6",baudrate=9600,timeout=5)

ser.write("AT+CMGF=1\r\n".encode())
time.sleep(1)

ser.write('AT+CPMS="SM","SM","SM"\r\n'.encode())
time.sleep(1)

ser.write("AT+CNMI=1,2,0\r\n".encode())
time.sleep(1)


while True:
    msg = ser.readline().strip()
    msg = msg.decode()
    msg = msg.replace("\r","")
    msg = msg.replace("\n","")
    
    if msg!="":
        print(msg)


ser.close()

当我从手机向USB调制解调器发送一条SMS时,没有任何提示接收到它:

======== RESTART: C:\Users\user\Desktop\sms.py =======


OK                         # Text mode OK

+CPMS: 1,20,1,20       

OK                         # SIM as preferred message storage OK

OK                         # Set the new message indicator OK

当我在第一个短信之后发送更多短信时,痛苦就来了。该脚本不再显示消息,只是继续阅读,就像没有收到消息一样(也没有错误报告)。 它仅适用于第一个SMS,在此之后,如果我想接收另一个脚本,则需要重新启动脚本。

我尝试了几种CNMI(新消息指示符)设置,但是不走运。

我的代码小伙子怎么了?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)