在构思运算符中使用AT命令检查平衡

时间:2015-06-24 14:26:13

标签: c# at-command

您是否知道使用*[number]#检查调制解调器的余额余额的服务类型是什么?

我正在尝试使用此代码检查余额。

  public AutoResetEvent receiveNow; 

    public string CheckBalance(SerialPort port)
    {
        string balance = string.Empty;
        try
        {
            #region Execute Command

            //string recievedData = SendATCommand(port, "AT", 300, "No phone connected at ");
            string recievedData = SendATCommand(port, "AT", 300, "No phone connected at ");
            recievedData = SendATCommand(port, "AT+CMGF=1", 300, "Failed to set message format.");
            String command = @"AT+CUSD=1,""*121#"",15";
            recievedData = SendATCommand(port, command, 5000, "Failed to count SMS message");
            balance = recievedData;

            #endregion
        }
        catch (Exception ex)
        {
            ErrorLog(ex.Message);
        }
        return balance;
    }

    public string SendATCommand(SerialPort port, string command, int responseTimeout, string errorMessage)
    {
        string input = string.Empty;
        try
        {
            port.DiscardOutBuffer();
            port.DiscardInBuffer();
            receiveNow.Reset();
            port.Write(command + "\r");

            input = ReadResponse(port, responseTimeout);
            if ((input.Length == 0) || ((!input.EndsWith("\r\n> ")) && (!input.EndsWith("\r\nOK\r\n"))))
                ErrorLog("No success message was received.");
        }
        catch (Exception ex)
        {
            ErrorLog(ex.Message);
        }
        return input;
    }

    public string ReadResponse(SerialPort port, int timeout)
    {
        string serialPortData = string.Empty;
        try
        {
            do
            {
                if (receiveNow.WaitOne(timeout, false))
                {
                    string data = port.ReadExisting();
                    serialPortData += data;
                }
                else
                {
                    if (serialPortData.Length > 0)
                        ErrorLog("Response received is incomplete.");
                    else
                        ErrorLog("No data received from phone.");
                }
            }
            while (!serialPortData.EndsWith("\r\nOK\r\n") && !serialPortData.EndsWith("\r\n> ") && !serialPortData.EndsWith("\r\nERROR\r\n"));
        }
        catch (Exception ex)
        {
            ErrorLog(ex.Message);
        }
        return serialPortData;
    }

前两个exec命令返回ok,但第三个exec命令返回错误。

USSD代码正在提供商移动合作伙伴应用程序

我需要发送哪些AT命令来正确处理ussd应用程序?

0 个答案:

没有答案