# SIM7600 Terminates Python Script At Setting SMS Mode

**URL:** <https://forum.core-electronics.com.au/t/sim7600-terminates-python-script-at-setting-sms-mode/20882>\
**Category:** Uncategorized\
**Created:** [18 September 2024 23:37 UTC](https://forum.core-electronics.com.au/t/sim7600-terminates-python-script-at-setting-sms-mode/20882 "2024-09-18T23:37:30Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![f0rml3ssf0rm](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/f0rml3ssf0rm/32/7643_2.png) [@f0rml3ssf0rm](https://forum.core-electronics.com.au/u/f0rml3ssf0rm)\
**Post date:** [18 September 2024 23:37 UTC](https://forum.core-electronics.com.au/t/sim7600-terminates-python-script-at-setting-sms-mode/20882/1 "2024-09-18T23:37:30Z")

</div>

This is the HAT I have:

> **[SIM7600G-H M.2 4G HAT for Raspberry Pi, LTE CAT4 High Speed, 4G/3G/2G, GNSS,...](https://core-electronics.com.au/sim7600g-h-m2-4g-hat-for-raspberry-pi-lte-cat4-high-speed-4g3g2g-gnss-global-band-1.html?gad_source=1)**
>
> SIM7600G-H M.2 4G HAT For Raspberry Pi, LTE CAT4 High Speed, Global Coverage Multi Band, 4G/3G/2G Compatible, GNSS...

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

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

```

---

<div class="post-metadata">

**Author:** ![Jack](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/jack/32/9287_2.png) [@Jack](https://forum.core-electronics.com.au/u/Jack)\
**Post date:** [19 September 2024 01:46 UTC](https://forum.core-electronics.com.au/t/sim7600-terminates-python-script-at-setting-sms-mode/20882/2 "2024-09-19T01:46:47Z")

</div>

Hi ff,

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

If there is an error could you please share that?

---

<div class="post-metadata">

**Author:** ![f0rml3ssf0rm](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/f0rml3ssf0rm/32/7643_2.png) [@f0rml3ssf0rm](https://forum.core-electronics.com.au/u/f0rml3ssf0rm)\
**Post date:** [19 September 2024 02:52 UTC](https://forum.core-electronics.com.au/t/sim7600-terminates-python-script-at-setting-sms-mode/20882/3 "2024-09-19T02:52:35Z")

</div>

Hey Jack,

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

```auto
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?

---

<div class="post-metadata">

**Author:** ![Jack](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/jack/32/9287_2.png) [@Jack](https://forum.core-electronics.com.au/u/Jack)\
**Post date:** [19 September 2024 05:19 UTC](https://forum.core-electronics.com.au/t/sim7600-terminates-python-script-at-setting-sms-mode/20882/4 "2024-09-19T05:19:37Z")

</div>

Is UART enabled on your Pi?

[Here’s an older forum post where someone was having some difficulty with ttyS0.](https://forums.raspberrypi.com/viewtopic.php?t=188233)

---

<div class="post-metadata">

**Author:** ![f0rml3ssf0rm](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/f0rml3ssf0rm/32/7643_2.png) [@f0rml3ssf0rm](https://forum.core-electronics.com.au/u/f0rml3ssf0rm)\
**Post date:** [19 September 2024 05:35 UTC](https://forum.core-electronics.com.au/t/sim7600-terminates-python-script-at-setting-sms-mode/20882/5 "2024-09-19T05:35:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jack](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/jack/32/9287_2.png) [@Jack](https://forum.core-electronics.com.au/u/Jack)\
**Post date:** [19 September 2024 05:51 UTC](https://forum.core-electronics.com.au/t/sim7600-terminates-python-script-at-setting-sms-mode/20882/6 "2024-09-19T05:51:50Z")

</div>

Were you just using [our test scripts](https://core-electronics.com.au/guides/raspberry-pi-4g-gps-hat/) or [Waveshare’s for this HAT](https://www.waveshare.com/wiki/SIM7600G-H-M2_4G_HAT#Demo_Example). 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.

---

<div class="post-metadata">

**Author:** ![f0rml3ssf0rm](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/f0rml3ssf0rm/32/7643_2.png) [@f0rml3ssf0rm](https://forum.core-electronics.com.au/u/f0rml3ssf0rm)\
**Post date:** [19 September 2024 06:09 UTC](https://forum.core-electronics.com.au/t/sim7600-terminates-python-script-at-setting-sms-mode/20882/7 "2024-09-19T06:09:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Zach](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/zach/32/10899_2.png) [@Zach](https://forum.core-electronics.com.au/u/Zach)\
**Post date:** [20 September 2024 01:06 UTC](https://forum.core-electronics.com.au/t/sim7600-terminates-python-script-at-setting-sms-mode/20882/8 "2024-09-20T01:06:54Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Alex304095](https://avatars.discourse-cdn.com/v4/letter/a/d78d45/32.png) [@Alex304095](https://forum.core-electronics.com.au/u/Alex304095)\
**Post date:** [24 September 2025 23:32 UTC](https://forum.core-electronics.com.au/t/sim7600-terminates-python-script-at-setting-sms-mode/20882/9 "2025-09-24T23:32:58Z")

</div>

I had the same issue and fixed it by adding a short delay after opening the serial port before sending any AT commands—seems like the modem wasn’t ready right away.

---

<div class="post-metadata">

**Author:** ![Jane](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/jane/32/12622_2.png) [@Jane](https://forum.core-electronics.com.au/u/Jane)\
**Post date:** [25 September 2025 01:44 UTC](https://forum.core-electronics.com.au/t/sim7600-terminates-python-script-at-setting-sms-mode/20882/10 "2025-09-25T01:44:08Z")

</div>

Hi @Alex304095 and welcome to the forum, glad to have you here 🙂

Thanks for the solution. Something short and sweet like a delay is often the best bet. Hopefully your solution helps someone else down the track.
