【问题标题】:Check balance using USSD Command in C#在 C# 中使用 USSD 命令检查余额
【发布时间】:2015-11-16 11:09:33
【问题描述】:

我更新了这个问题并知道它工作正常..

我尝试检查我的 mavecom 调制解调器中的余额,但我的文本框中没有任何响应。它一直是空的。

这是我的代码:

private SerialPort _port;

private void simpleButton1_Click(object sender, EventArgs e)
    {
        _port = new SerialPort();
        _port.PortName = cbPort.Text;
        _port.BaudRate = 115200;
        _port.Parity = Parity.None;
        _port.DataBits = 8;
        _port.StopBits = StopBits.One;
        _port.Handshake = Handshake.RequestToSend;

        port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
        port.Open();

        port.Write("AT+CUSD=1,\"" + txtUSSD.Text + "\",15" + "\r");
    }

private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        try
        {
            // read the response.
            var response = ((SerialPort)sender).ReadLine();

            // Need to update the txtProvider on the UI thread .
            //showing result in txtOutput based on txtProvider USSD Command
            this.Invoke(new Action(() => txtOutput.Text = response)); 
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

这个解决了,可以用来查余额....

【问题讨论】:

  • 写入后不要立即关闭端口。
  • 在打开串口之前注册DataReceived也是个好主意。
  • 我先试试看你的建议
  • 伙计们,..它仍然消失...在 txtOutput 找不到结果
  • 不要在标题中添加“[已解决]”字样,这不是它的工作原理。请选择正确的答案。

标签: c# at-command ussd


【解决方案1】:

良好的开端,您正在使用\r 正确终止 AT 命令行(不使用 WriteLine 或任何其他此类不正确的方法,不幸的是,这些方法是常见的初学者问题)。但是命令的格式在27.007 中定义为

AT+CUSD=[<n>[,<str>[,<dcs>]]]
...
Defined values
...
<str>: string type USSD-string ...

字符串参数必须用双引号括起来(V.250 第 5.4.2.2 章字符串常量:String constants shall be bounded at the beginning and end by the double-quote character)。

所以在不详细了解 textProvider 对象的情况下,我很有信心您的代码应该是

port.Write("AT+CUSD=1,\"" + txtProvider.Text + "\",15" + "\r");

但请注意,如果txtProvider.Text 包含任何" 字符,则它们必须被转义( 顺便说一下\",检查5.4.2.2)。


但是,即使上述问题已解决,您也需要认真地重新处理您的接收处理。您必须读取并解析来自调制解调器的每一行响应,直到获得最终结果代码(最常见的是 OKERROR,但还有其他几个)。任何其他方式都无法可靠地工作。请参阅this answer 了解如何正确执行的伪代码结构。

正如评论的那样,您关闭端口太早了。

【讨论】:

  • "\",15" + "\r" 可能只是 "\",15\r"
  • @hlovdal 当我在port.Write("AT+CUSD=1,\"" + txtProvider.Text + "\",15" + "\r"); 之类的命令中更改ussd 时没有任何反应,...在txtOutput 中没有结果
猜你喜欢
  • 2015-06-26
  • 2017-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-18
  • 1970-01-01
  • 2021-01-06
  • 2015-06-14
相关资源
最近更新 更多