NetworkInterface causes program to hang after using the same uart port

Hi, I am using NetworkInterface to do a cellular network connection with Quectel BG96.
Network connection is fine, until I try to create a UARTSerial object (to communicate to the same BG96 chip) for obtaining GPS coordinates. I am able to read the response from the BG96, however the program hangs after this.

Mutex serial_mutex;
NetworkInterface *net = NetworkInterface::get_default_instance();
net->connect();
UARTSerial* serial = new UARTSerial(PD_5, PA_3, 115200);
ATCmdParser* parser = new ATCmdParser(serial);
int nreg, stat;
/* example AT commands */
serial_mutex.lock();
parser->flush();
bool ok = parser->send("AT+CREG?");
ok = parser->recv("+CREG: %d,%d",&nreg, &stat);
serial_mutex.unlock();
printf("at command reply %s: %d, %d\r\n", ok?"-":"ok", nreg, stat);
int ret = net->disconnect();    // hangs here
printf("status %d\r\n", ret);

Is it not possible to use both network interface and uartserial of the same serial port at the same time?

Hi @ziqiyap
Thank you for your question!

I am assuming your NetworkInterface is QUECTEL_BG96_CellularContext , right? Are you changing your default interface? This might cause some confusion on what is current default Interface, if not handled carefully.

Note that in your sample code you have a memory leak, since you are not deleting parser and serial. Perhaps these are keeping a handle to the serial port, causing the disconnect to hang.
Please try deleting parser and serial before you try to disconnect from net.
Regards,
Mbed Support
Ron

Hi Ron,

Thank you for your reply – happy new year to you by the way!

To your first question: yes it is. I declared

“target.network-default-interface-type” : “CELLULAR”,
“QUECTEL_BG96.provide-default” : true

in the mbed_app.json file.

As for your suggestion, I have edited the code accordingly and it still hangs at the disconnect. Could it be due to the error of the BG96 chip itself? There is no mbed hard fault error shown so I am unsure what the main issue is.

Mutex serial_mutex;
NetworkInterface net = NetworkInterface::get_default_instance();
net->connect();
UARTSerial
serial = new UARTSerial(PD_5, PA_3, 115200);
ATCmdParser* parser = new ATCmdParser(serial);
int nreg, stat;
/* example AT commands */
serial_mutex.lock();
parser->flush();
bool ok = parser->send(“AT+CREG?”);
ok = parser->recv(“+CREG: %d,%d”,&nreg, &stat);
serial_mutex.unlock();
printf(“at command reply %s: %d, %d\r\n”, ok?“ok”:“no reply”, nreg, stat);
delete parser;
delete serial;
int ret = net->get_connection_status();
printf(“connection status %d\r\n”, ret);
ret = net->disconnect();
printf(“disconnect rc %d\r\n”, ret);

Here’s the printout:

[INFO][CELL]: Attaching network (timeout 60000 ms)
[INFO][CELL]: Found PDP context 2
[INFO][CELL]: Activate PDP context 2
[INFO][CELL]: Cellular local IP: 0.0.0.0
at command reply ok: 2, 1
connection status 1
[INFO][CELL]: CellularContext disconnect()

Hi,
Another question: what data access mode is used in CellularInterface for Quectel BG96?

I realised in opening a TCP socket service, there are a few data access modes that we can choose to use. I suspected that the issue might be that the service is in transparent access mode, but does not appear to be that case as the chip is replying to AT commands even after opening a socket service. The hanging issue occurs after I send an AT command using another serial interface open (which uses the same port) and trying to send something over the open socket. (See code block below)

[INFO][CELL]: Socket 0 recv 5 bytes
[INFO][CELL]: Socket 0 recv 29 bytes
at command reply ok: 2, 1
[DBG ][TLSW]: send 201    // stuck here
[BOOT] Mbed Bootloader

I would like either know how I can find the information about the data access mode used and how the cellular interface handles the TCP sockets. Thanks!

Hi @ziqiyap
If I had to guess, I would sat that the disconnect is waiting for a mutex to be freed
However there could be other reasons for the hang
Regards