【问题标题】:How to retrieve response result in sending USSD commands in c#?如何在 C# 中检索发送 USSD 命令的响应结果?
【发布时间】:2015-03-02 23:57:00
【问题描述】:

我使用此代码发送了 USSD 命令:

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
    }

为什么只在结果中检索 OK?

在这种情况下,我想找回我的余额,我必须收到这样的答案:“你的余额是 100$...”,但只需检索:“AT+CUSD=1,\”*140*​​1#\” \r\n\OK\r\n" 但是当我通过调制解调器自己的应用程序发送此命令时,从操作员那里检索正确的响应,这意味着我的发送命令正常,但该应用程序收到所有答案但我收到一半。

【问题讨论】:

  • 你找到解决方案了吗?我面临同样的问题。
  • 这是因为代码首先收到调制解调器响应(这没关系),然后在一小段时间后收到网络响应(这是实际余额消息)

标签: c# modem ussd


【解决方案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 "";
    }

【讨论】:

    【解决方案2】:

    在代码末尾

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-08
      • 1970-01-01
      • 2016-01-16
      • 2015-07-06
      • 2014-08-30
      • 2020-04-10
      相关资源
      最近更新 更多