0

I sent USSD command using this code:

SerialPort port = new SerialPort();

port.BaudRate = 921600;
port.PortName = "COM16";
port.Parity = Parity.None;
port.DataBits = 8;
port.StopBits = StopBits.One;
port.ReadTimeout = 3000;
port.WriteTimeout = 3000;
port.DataReceived += port_DataReceived;

port.Open();

port.Write("AT+CUSD=1,\"*140*1#\"" + "\r\n");

void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
        SerialPort spL = (SerialPort)sender;
        byte[] buf = new byte[spL.BytesToRead];
        spL.Read(buf, 0, buf.Length);

        foreach (Byte b in buf)
        {
            message += b.ToString();
        }

        var result = Encoding.ASCII.GetString(buf);//just return OK
    }

Why just retrieve OK in result?

in this case i want to retrieve my balance and i must receive answer like this: "your balance is 100$..." but just retrieve: "AT+CUSD=1,\"*140*1#\"\r\n\OK\r\n" but when i send this command by modem's own application retrieve correct response from operator this means my sending command is OK but that application receive all answer but i receive half.

4

2 回答 2

1

实际上我更新了代码,它现在可以与 GSMCOMM 库一起使用。我可以检查我的余额并查看响应。当我仍在接收响应时,我只是将响应附加到字符串变量。

public string SendUssdRequest(string request)
    {
        log.DebugFormat("Sending USSD Request {0}", request);
        string result = "";
        try
        {
            IProtocol protocol = comm.GetProtocol();
            string gottenString = protocol.ExecAndReceiveMultiple("AT+CUSD=1," + request + ",15");
            result = gottenString;
            int i = 0;
            if (!gottenString.Contains("\r\n+CUSD: 2"))
            {
                bool receiving = false;
                do
                {
                    receiving = protocol.Receive(out gottenString);
                    result += gottenString;
                    ++i;
                } while (receiving);
            }

            result = result.Replace("\r\n", "");

            result = result.Replace("+CUSD: 2,", "");
            result = result.Replace(",15", "");
            log.DebugFormat("{1} - USSD Response is:  {0}", result,SenderNumber);
            return result;
        }
        catch(Exception ex) 
        {
            log.Error(ex);
        }
        finally
        {

            comm.ReleaseProtocol();
        }
        return "";
    }
于 2015-03-02T15:57:00.740 回答
-1

在代码的末尾

port.Write("AT+CUSD=1,\"*140*1#\"" + ",15\r");

于 2014-04-03T09:16:41.730 回答