Reliably setting up APN for EG915 in ECM mode with AT commands

We are using EG915 module under Linux in ECM mode.

When the system powers on and the Linux system boots we run a script that checks whether the APN needs to be set. The scripts does this via the serial interface and AT commands.

The script performs the following AT commands when APN needs to be changed (or setup for the first time). The script waits for the module to reply after each command is sent before sending the next command and checks for OK or any errors or unexpected replies.

check the currently set APN

AT+CGDCONT?

disconnect the network adapter

AT+QNETDEVCTL=0

deactivate current PDP

AT+CGACT=0,1

dettach PS

AT+CGATT=0

delete current PDP

AT+CGDCONT=1

create PDP with the new APN

AT+CGDCONT=1,“IPV4V6”,“”

set network adapter connect type to auto and PDP 1

AT+QNETDEVCTL=3,1

This procedure works, but is unreliable. Often it will fail with +CME ERROR: 50 at some command (CG+CGATT=0, AT+CGDCONT=1 or AT+CGDCONT=1,…). Then the module will not be set up correctly on boot and the network connection will not work. Repeating the same procedure later will sometimes work again. The problem can be reproduced by manually typing AT commands into the terminal, so it’s not an issue with how the script handles the AT interface.

I suspect that the problem is some kind of a race condition in regard to module registering on the mobile network, or that we are not using the correct sequence of commands for deleting and creating the PDP. The “AT commands manual” doesn’t go into such detail and neither does GSM 07.07.

Is there a document that describes the correct procedure for setting up the APN in our use case?

Module version:

Quectel
EG915U
Revision: EG915UEUABR03A01M08

Just to add some more clarification to my question above:

The problem is that we have the script described above that seems to work during development, but then we deploy this to test devices and they will fail to setup the APNs. We modify the script, change the order of AT commands, add retries in new points, etc. and again we get failures during testing. It’s not just a matter of simply retrying since sometimes the same sequence will repeatably fail. It’s possible different SIM cards, mobile networks, other conditions etc. affect this.

I suspect we have a wrong mental model of what these commands are doing to the internal state of the module and/or what are the requirements on the internal state of the module for adding/deleting/activating PDP contexts. For example, is detaching PS/AT+CGATT=0 necessary before deleting a PDP/AT+CGDCONT=1? From our testing it seems sometimes it is and sometimes it is not. Is it necessary to activate a PDP with AT+CGACT in addition to connecting the network adapter in “auto” connect type/AT+QNETDEVCTL=3,1 or not?

So what I am asking is: Is there a published procedure (from Quectel) on how to correctly setup/modify APN for ECM mode on EG915 that we can replicate? I can’t find it in available EG915 documents.

If not, what is the document that describes in more detail how the commands interact with the internal state? What is CME ERROR 50 (it is not included in the “EC200U&EG91xU&EG915G Series AT Commands Manual”)?

Thanks.

Have you ever tried using a serial port tool (QCOM or XCOM) to send AT commands to configure APN without using scripts?