Using GSMComm Library Get USSD Result

837 Views Asked by At

I'm trying to run ussd code via gsm modem to get sim balance. I'm using GsmComm library, ASP.NET Web Form, C#. Below is my code :

    public string SendUssdRequest2(string request)
    {
        comm = ConnectAndGetComm();
        string data = TextDataConverter.StringTo7Bit(request);

        string msg = "";
        var asPDUencoded = Calc.IntToHex(TextDataConverter.SeptetsToOctetsInt(data));
        try
        {
            IProtocol protocol = comm.GetProtocol();
            string gottenString = protocol.ExecAndReceiveMultiple("AT+CUSD=1," + asPDUencoded + ",15");
            var re = new Regex("\".*?\"");
            int i = 0;
            if (!re.IsMatch(gottenString))
            {
                do
                {
                    protocol.Receive(out gottenString);
                    ++i;
                } while (!(i >= 5
                           || re.IsMatch(gottenString)
                           || gottenString.Contains("\r\nOK")
                           || gottenString.Contains("\r\nERROR")
                           || gottenString.Contains("\r\nDONE"))); 
            }

            string m = re.Match(gottenString).Value.Trim('"');
            return PduParts.Decode7BitText(Calc.HexToInt(m));
        }
        catch(Exception e)
        {
            msg = e.Message;
        }
        finally
        {
            comm.ReleaseProtocol();
        }
        return msg;
    }

I debug and found Modem is connected. On "ExecAndReceiveMultiple" I'm Getting the error - No data received from phone after waiting for 29999 / 30030 ms. Is the code alright ? What could be the problem ? Any other suggestion like other library or code to get sim balance would be great help. Thank You.

1

There are 1 best solutions below

0
hlovdal On

string gottenString = protocol.ExecAndReceiveMultiple("AT+CUSD=1," + asPDUencoded + ",15");

There are one or maybe two problems with this line. To take the maybe part first, an AT command line should be terminated with the carriage return character, e.g. \r, aka ASCII value 131. If ExecAndReceiveMultiple does this implicitly then there is no problem, but if not then that that's a problem because the command line is not terminated properly.


But the problem that definitely exist is "AT+CUSD=1," + asPDUencoded + ",15".

The AT+CUSD command is defined in the 27.005 specification in chapter 7.15 Unstructured supplementary service data +CUSD and the syntax is given as:

+CUSD=[<n>[,<str>[,<dcs>]]]
...
<str>: string type USSD-string 

The keyword here is string. The V.250 specification has the following to say about strings:

5.4.2.2 String constants

String constants shall consist of a sequence of displayable characters ... except for the characters " and \. String constants shall be bounded at the beginning and end by the double-quote character (").

The asPDUencoded variable is given a sting value with a hex representation of the input, let's assume "1A2B3C4D5F" for the following. The code line will then result in the following function call:

protocol.ExecAndReceiveMultiple("AT+CUSD=1,1A2B3C4D5F,15");

Do you see the problem? The second parameter <str> is a string, and it MUST be enclosed with double quotes2, e.g.

protocol.ExecAndReceiveMultiple("AT+CUSD=1,\"1A2B3C4D5F\",15");

So the original string concatenation needs to be corrected to the following:

"AT+CUSD=1,\"" + asPDUencoded + "\",15"


1 Strictly speaking the S3 register, but never, never, never change it from its default 13 value.

2 Also bear in mind that strings are subject to AT+CSCS formatting.