Using GSMComm Library Get USSD Result
Asked Answered
B

1

7

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.

Boyette answered 19/8, 2019 at 14:56 Comment(3)
You are connecting to a modem and the connection is good. The modem is broadcasting the SMS message over the phone network and not getting any text back from the phone. So the phone may just be turned off or the person is not sending and text back in 30 seconds.Lumumba
That's the same code snippet everybody seems to be using. It works fine, maybe there's an issue in your phone network carrier? Try several times, I had to. You can also try with SmsLibGeorginageorgine
By the way the overall code structure here is not bad, but DONE is not a valid Final result code and should not be checked for. There exists more final result codes than just OK and ERROR, so you must check for all of them. Also you should change the serial input handling to do proper framing.Dyaus
D
0

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.

Dyaus answered 18/1, 2023 at 22:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.