我已经能够将其设置为连接到网络并使用屏幕和 minicom 命令(例如 AT+CMGS=“************”)发送和接收短信
但是,当运行尝试执行相同操作的 python 脚本(例如 waveshare 演示脚本)时,脚本会在尝试将 7600 设置为短信模式时终止。
有什么想法吗?
import RPi.GPIO as GPIO
import serial
import time
ser = serial.Serial("/dev/ttyS0",115200)
ser.flushInput()
phone_number = '**********' #********** change it to the phone number you want to call
text_message = 'www.waveshare.com'
power_key = 6
rec_buff = ''
def send_at(command,back,timeout):
rec_buff = ''
ser.write((command+'\r\n').encode())
time.sleep(timeout)
if ser.inWaiting():
time.sleep(0.01 )
rec_buff = ser.read(ser.inWaiting())
if back not in rec_buff.decode():
print(command + ' ERROR')
print(command + ' back:\t' + rec_buff.decode())
return 0
else:
print(rec_buff.decode())
return 1
def SendShortMessage(phone_number,text_message):
print("Setting SMS mode...")
send_at("AT+CMGF=1","OK",1)
print("Sending Short Message")
answer = send_at("AT+CMGS=\""+phone_number+"\"",">",2)
if 1 == answer:
ser.write(text_message.encode())
ser.write(b'\x1A')
answer = send_at('','OK',20)
if 1 == answer:
print('send successfully')
else:
print('error')
else:
print('error%d'%answer)
def ReceiveShortMessage():
rec_buff = ''
print('Setting SMS mode...')
send_at('AT+CMGF=1','OK',1)
send_at('AT+CPMS=\"SM\",\"SM\",\"SM\"', 'OK', 1)
answer = send_at('AT+CMGR=1','+CMGR:',2)
if 1 == answer:
answer = 0
if 'OK' in rec_buff:
answer = 1
print(rec_buff)
else:
print('error%d'%answer)
return False
return True
def power_on(power_key):
print('SIM7600X is starting:')
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(power_key,GPIO.OUT)
time.sleep(0.1)
GPIO.output(power_key,GPIO.HIGH)
time.sleep(2)
GPIO.output(power_key,GPIO.LOW)
time.sleep(20)
ser.flushInput()
print('SIM7600X is ready')
def power_down(power_key):
print('SIM7600X is loging off:')
GPIO.output(power_key,GPIO.HIGH)
time.sleep(3)
GPIO.output(power_key,GPIO.LOW)
time.sleep(18)
print('Good bye')
try:
power_on(power_key)
print('Sending Short Message Test:')
SendShortMessage(phone_number,text_message)
print('Receive Short Message Test:\n')
print('Please send message to phone ' + phone_number)
ReceiveShortMessage()
power_down(power_key)
except :
if ser != None:
ser.close()
GPIO.cleanup()
我已经成功连接到4G网络,通过minicom中的直接命令发送和接收短信,例如AT+CMGS="********"。当我尝试使用 python 脚本执行此操作时,脚本在尝试将 7600 设置为 SMS 模式时停止并终止。我已对该文件应用了 777 权限,使用 sudo python3 启动脚本,还打开和关闭了硬件和软件流控制。
没有错误,它只是终止,但我将其范围缩小到以下代码行:
if ser.inWaiting():
time.sleep(0.01 )
rec_buff = ser.read(ser.inWaiting())
由于某种原因,它接缝缓冲区中没有等待读取的字节,并且它终止。
有什么想法吗?