SIM7600 Terminates Python Script At Setting SMS Mode

This is the HAT I have:

Hey there, I have been able to set this up to connect to the network and send and receive sms messages using screen and minicom commands such as AT+CMGS=“************”

However, when running python scripts that attempt to do the same thing, such as the waveshare demo scripts, the script terminates while trying to set the 7600 into sms mode.

Any ideas?

Thanks,

ff

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()

Hi ff,

Does it output any errors or does the script just terminate?

If there is an error could you please share that?

Hey Jack,

It doesnt spit out an error, but ive narrowed it down to these lines of code:

if ser.inWaiting():
		time.sleep(0.01 )
		rec_buff = ser.read(ser.inWaiting())

For some reason it seams there are no bytes in the buffer waiting to be read, and its terminating.

Any ideas?

Is UART enabled on your Pi?

Here’s an older forum post where someone was having some difficulty with ttyS0.

Yes, UART is enabled I have followed the Core Electronics tutorial for this. Minicom works perfectly with AT commands, I have sent and received sms msgs, but when python attempts to talk to the 7600 it seems not to be able to communicate. I have given 777 permissions as per the CE tutorial.

Were you just using our test scripts or Waveshare’s for this HAT. There’s will be better tuned for this HAT as ours aims to target the range. I can see that their test scripts do differ a little.

I have tried multiple different scripts, all get stuck and terminate when the script tries to write and/or read from the buffer. Even gotten really basic with ChatGPT scripts just to send an AT command and get an OK response, and the response comes back blank.

Hey ff,

I find it strange that multiple scripts are all having this same issue. A couple of areas come to mind regarding the origin of the problem.

  1. Some sort of data type conflict, though this feels unlikely given that many of the functions being used simply deal in bytes. Python is also typically quite forgiving in this sense I’ve found.

  2. If you are using another device to receive/send messages, there could be something wrong with the setup on that end, though I would find it strange that a separate device is causing the Pi/Hat to terminate.

Both of these issues additionally have the added contradiction that you successfully used minicom commands, and the problem arose when it came to python scripts!

It may be worth fully isolating the issue. Using scrap bits from the test code to do one or two actions at a time. (e.g. successfully writing to the buffer.)

I’ll give this some more thought and update you if I have any solution ideas!