在C#中使用USSD命令检查余额 [英] Check balance using USSD Command in C#

查看:136
本文介绍了在C#中使用USSD命令检查余额的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此问题进行了更新,并且知道它可以正常工作.

我尝试在mavecom调制解调器中检查余额,但在文本框中没有任何响应.它保持为空.

I try to check balance in my mavecom modem but I got no response in my textbox. It stays empty.

这是我的代码:

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);
        }
    }

此问题已解决,可以用来检查余额....

This is solved and can be use to check the balance....

推荐答案

良好的开始,您可以使用\r正确终止AT命令行(不使用WriteLine或任何其他不幸的常见初学者问题).但是,该命令的格式在 27.007 中定义为

Good start, you are properly terminating AT command lines with \r (not using WriteLine or any other such incorrect approaches that unfortunately are common beginner problems). However the format of the command is defined in 27.007 to be

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

和字符串参数应始终用双引号引起来(

and string parameters shall always be enclosed with double quotes (V.250 chapter 5.4.2.2 String constants: String constants shall be bounded at the beginning and end by the double-quote character).

因此,在不详细了解textProvider对象的情况下,我很确定您的代码应该是

So without knowing the textProvider object in detail I am quite confident that your code should be

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

,但请注意,如果txtProvider.Text包含任何"字符,则必须对其进行转义(顺便说一下,不是\"一样,请参见5.4.2.2).

but note that if txtProvider.Text contains any " characters they must be escaped (not as \" by the way, check 5.4.2.2).

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

However even with the above fixed you need to seriously rework your reception handling. You MUST read and parse every single line of response from the modem until you get a Final result code back (most commonly OK or ERROR, but there are several others). Any other way cannot work reliably. See this answer for pseudo code structure of how to do it properly.

据评论,您关闭端口为时过早.

And as commented, you are closing the port too early.

这篇关于在C#中使用USSD命令检查余额的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆