无法使用System.IO.Ports使用GSM调制解调器通过C#code发送短信 [英] Unable to send SMS through C# code using System.IO.Ports using gsm modem

查看:560
本文介绍了无法使用System.IO.Ports使用GSM调制解调器通过C#code发送短信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个按钮,点击后,发送短信到NumTxt文本框中输入的号码,并将在SMSTxt文本框中输入的文本。在texbox COMPORT输入端口名称这里的按钮点击事件的事件处理程序。

A button, when clicked, sends an sms to the number entered in NumTxt textbox, and sends the text entered in SMSTxt textbox. Port name entered in texbox ComPort Here's the event handler of the button click event.

  using System.IO.Ports;

  private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            int mSpeed = 1;
            serialport.PortName = ComPort.Text;
            serialport.BaudRate = 96000;
            serialport.Parity = Parity.None;
            serialport.DataBits = 8;
            serialport.StopBits = StopBits.One;
            serialport.Handshake = Handshake.XOnXOff;
            serialport.DtrEnable = true;
            serialport.RtsEnable = true;
            serialport.NewLine = Environment.NewLine;
            Console.WriteLine("1a");
            try
            {
                serialport.Open();
            }
            catch (Exception)
            {
                MessageBox.Show("Try another Port." + 
    Environment.NewLine + "Phone not detected or The requested resource is in      
    use.", "CONNECTION ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            Console.WriteLine("2a");

            serialport.WriteLine("AT+CMGF=1" + Environment.NewLine);
            System.Threading.Thread.Sleep(200);
            serialport.WriteLine("AT+CSCS=GSM" + Environment.NewLine);
            System.Threading.Thread.Sleep(200);
            serialport.WriteLine("AT+CMGS=" + (char)34 + NumTxt.Text
            + (char)34 + Environment.NewLine);
            System.Threading.Thread.Sleep(200);
            serialport.WriteLine(SMSTxt.Text + (char)26);
            System.Threading.Thread.Sleep(mSpeed);
            serialport.Close();

        }
        catch (Exception)
        {
            if (serialport.IsOpen)
                serialport.Close();
            MessageBox.Show("Couldn't send the SMS.", "CONNECTION ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

我能够给我使用这种非常code昨日短信,但我不知道为什么它不工作了..没有抛出异常。
当我使用与GSM调制解调器软件,我能发送短信。但不是通过C#code。如果任何人都可以针点在上面code中的错误,我会很感激。

I was able to send the sms using this very code yesterday, but i do not know why it doesn't work any more.. no exceptions thrown. When I use the software that comes with gsm modem, I am able to send sms. But not through C# code. If anyone can pin point the mistake in the above code, I will be very grateful.

推荐答案

您应该永远,永远,永远使用睡眠作为等待来自调制解调器的最终结果code的替代品。正如你不会写一个HTTP客户端完全忽略从HTTP服务器的所有响应,你不应该发送AT到调制解调器的命令和完全忽略它发回的响应。您必须的读取和解析一切调制解调器发送回给你。闲来无事将可靠地工作。

You should never, never, ever use sleep as a substitute for waiting for the Final result code from the modem. Just as you would not write a http client that completely ignores all responses from the http server, you should not send AT commands to a modem and completely ignore the responses it sends back. You must read and parse everything the modem sends back to you. Nothing else will work reliably.

我的建议是,你通过抓取的的 V.250标准并读取第5章的全部至少这个标准是AT命令处理圣经,教你关于AT命令处理大量的。例如像,使用的WriteLine 和/或 Environment.NewLine 是错误的; AT命令行应该与终止\\ r 单独而不是其他。

My suggestion is that you start by fetching a copy of the V.250 standard and read at least all of chapter 5. This standard is the bible for AT command handling and will teach you an enormous amount regarding AT command handling. Like for instance that using WriteLine and/or Environment.NewLine is wrong; AT command lines should be terminated with \r alone and nothing else.

只是为了强调该文件是多么重要:即使在爱立信手机超过十年我和我的同事仍的商议标准定期的。

Just to emphasis how important that document is: Even after working with implementing AT command in mobile phones in Ericsson for over a decade I and my colleagues still consulted that standard regularly.

其实现在停止阅读这里这个答案,下载该文件,返回读其余部分之前阅读所有第5章。

In fact stop reading this answer here now, download that document, read all of chapter 5 before returning to read the rest.

有关发送,你不特别在意的响应命令 1 ,唯一可靠的办法是做类似的东西。

For sending commands where you do not particularly care about the response1, the only reliably approach is to do something similar to

serialport.Open();
...
// start sending AT+CMGF=1
serialport.Write("AT+CMGF=1\r");
do {
    line = readLine(serialport);
} while (! is_final_result_code(line))
// Sending of AT+CMGF=1 command finished (successfully or not)
...
serialport.Close();

在那里,直到它已经接受了终止\\ r \\ n 的readLine 函数读取来自串行端口一个又一个字节code>,然后返回该行。

where the readLine function reads one and one byte from the serial port until it has received a complete line terminated with \r\n and then returns that line.

您可以看一下code代表的 atinout 为在 is_final_result_ code 函数的一个例子(你也可以比较 isFinalResponseError isFinalResponseSuccess 2 中的 ST-Ericsson的U300 RIL )。

You can look at the code for atinout for an example for the is_final_result_code function (you can also compare to isFinalResponseError and isFinalResponseSuccess2 in ST-Ericsson's U300 RIL).

的AT + CMGS命令必须被不同的处理。你必须等待\\ r \\ n>中发送有效载荷之前从调制解调器的反应,看的这个答案了解详情。

The AT+CMGS command must be handled differently. You must wait for the "\r\n> " response from the modem before sending the payload, see the first part of this answer for details.

1 虽然你也许应该关心是否成功或不执行命令。

1 Although you should probably care about if the command was executed successfully or not.

2 注意连接是不是一个最终的结果code,它是一个中间结果code,故得名isFinalResponseSuccess严格来说不是100%正确。

2 Note that CONNECT is not a final result code, it is an intermediate result code, so the name isFinalResponseSuccess is strictly speaking not 100% correct.

这篇关于无法使用System.IO.Ports使用GSM调制解调器通过C#code发送短信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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