C# AT Command CUSD : Wait for network unsolicited response

I'm developing a C# utility to execute USSD codes (checking balance, activate offers ...). For this, I use AT Command +CUSD. My first exemple is to get the balance. I execute the command :

AT+CUSD=1,"*222#",15

As a result, I get :

AT+CUSD=1,"*222#",15
OK

Now, the problem is, as you can see, the result does not contain any information on the balance, and when I execute the same command *222# using the modem application, it get the desired result : "Your balance is 50 $". It's like the OK result I'm getting is from the modem, and I'm not waiting for the network response, which take less than 1 second.

Here is my code :

// Initialize Port
var port = new SerialPort("COM14");
port.BaudRate = 9600;
port.DataBits = 8;
port.StopBits = StopBits.One;
port.Parity = Parity.None;
port.ReadTimeout = 3000;
port.WriteTimeout = 3000;
port.Encoding = Encoding.GetEncoding("iso-8859-1");
port.Open();

port.DtrEnable = true;
port.RtsEnable = true;

port.DiscardOutBuffer();
port.DiscardInBuffer();

// Execute command
var cmd = "AT+CUSD=1,\"*222#\",15" + "\r";
port.Write(cmd);

// Wait 3 sec
Thread.Sleep(3000);

// Read and display the response
var resp = port.ReadExisting();
MessageBox.Show(resp);

I've used two types of modem, Huawei and D-Link. And I have the same behaviour.

Any idea how to get the network response "Your balance is ..." ? Thanks in advance.

EDIT :

I've handled the DataReceived event of the SerialPort as follow, but no unsolicited result code was received.

BlockingCollection<string> reponsesCollection = new BlockingCollection<string>();

private SerialPort initializePort()
{
    var port = new SerialPort("COM14") { ... };

    port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
    port.Open();

    ...

    return port;
}

private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    reponsesCollection.Add(port.ReadExisting());
}

EDIT 2:

I've tried the same commands using Putty utility, like shown in this article, same result, no URC received. Starting to think that the modem itself is blocking the responses, in a way that only its application can receive them.

728x90

0 Answers C# AT Command CUSD : Wait for network unsolicited response