Different output with pySerial vs bash screen

Hey guys,

This issue is puzzling to me. I'm trying to read responses from my GSM Shield 2, but I have an issue where I get incomplete responses using ser.read() as opposed to screen. For example, pySerial doesn't return any OK responses:

In Python I'm using ser.read() at the end, for debugging purposes, so I should be getting byte for byte response, but the OK reponse is missing:

import serial

ser = serial.Serial(
    port='/dev/tty.usbmodemFD111',
    baudrate=9600,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS, 
)

str_modem_ready="Enter your AT commands (with CR & NL)..."
com_set_sms_mode="AT+CMGF=1".encode('utf-8')
modem_state=0

while(modem_state==0):
	x = ser.read_until()
	if str_modem_ready in x.decode('utf-8'):
		modem_state=1
		print("Modem ready")
		ser.write(com_set_sms_mode)
		while True:
			print(ser.read())

Returns:

Modem ready
b'A'
b'T'
b'+'
b'C'
b'M'
b'G'
b'F'
b'='
b'1'

As opposed to screen, where I get the OK response:

Start Reset
End Reset
Enter your AT commands (with CR & NL)...
AT+CMGF=1%13%%13%%10%
OK%13%%10%

Strangely enough, when I send an SMS to the SIM, the responses from pySerial and screen are identical:

screen:

%13%%10%
+CMTI: "SM",12%13%%10%

Python:

b'%'
b'1'
b'3'
b'%'
b'%'
b'1'
b'0'
b'%'
b'\r'
b'\n'
b'+'
b'C'
b'M'
b'T'
b'I'
b':'
b' '
b'"'
b'S'
b'M'
b'"'
b','
b'1'
b'3'
b'%'
b'1'
b'3'
b'%'
b'%'
b'1'
b'0'
b'%'
b'\r'
b'\n'

I wonder if the problem is that your Python code is not allowing time for the Arduino to reset when it opens the serial port?

Have a look at this Simple Python - Arduino demo

...R

Robin2:
I wonder if the problem is that your Python code is not allowing time for the Arduino to reset when it opens the serial port?

Have a look at this Simple Python - Arduino demo

...R

Hmm... The Arduino shouldn't somehow be resetting when I run the code, right? My code is listening for the GSM module to send its ready message.

When a PC program opens the serial port an Arduino Uno, Mega or nano will reset - that is a deliberate feature to facilitate the uploading of a new program.

...R

I dont know if you interested in this now but the .read() in pyserial takes and argument which defines the length of the data to be expected. for example if you are expecting a data to be 10 bytes long, you should use the following code:

s = Ser.read(10)

use a more complicated code which receives available data byte by byte in a loop until it receives a special character like a carriage return '\r' or a line feed '\n'.