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.
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. IfExecAndReceiveMultipledoes 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+CUSDcommand is defined in the 27.005 specification in chapter 7.15 Unstructured supplementary service data +CUSD and the syntax is given as:The keyword here is string. The V.250 specification has the following to say about strings:
The
asPDUencodedvariable 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:Do you see the problem? The second parameter
<str>is a string, and it MUST be enclosed with double quotes2, e.g.So the original string concatenation needs to be corrected to the following:
"AT+CUSD=1,\"" + asPDUencoded + "\",15"1 Strictly speaking the
S3register, but never, never, never change it from its default 13 value.2 Also bear in mind that strings are subject to
AT+CSCSformatting.