C#应用程序通过PC向其他手机发送短信 [英] C# application to send sms to other mobiles through pc

查看:111
本文介绍了C#应用程序通过PC向其他手机发送短信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过以下c#代码使用AT命令发送短信.有人可以检查这个简单的代码并建议我为什么它不起作用.
我可以使用
从超级终端发送短信 AT + CSCA ="msg center no";
AT + CMGS ="mobile no"

这段代码:
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.IO.Ports;

命名空间condemo
{
课程计划
{
静态void Main(string [] args)
{
SerialPort e =新的SerialPort();
e.PortName =&"COM4&";
e.BaudRate = 460800;
e.Parity = Parity.None;
e.DataBits = 8;
e.StopBits = StopBits.One;

如果(!(e.IsOpen))
{
试试
{
e.Open();
}
catch(异常m)
{
Console.WriteLine(m.Message);
}
}

e.Write("AT" +(char)13);

e.Write("AT + CPIN?" +(char)13);

e.Write("AT + CMGF = 1" +(char)13);

e.WriteLine("AT + CSCA =" +"9886005444" +(char)13);
e.WriteLine("AT + CMGS" +"9742899065" +(char)13);
e.WriteLine("message"; +(char)13);
e.Write(" +(char)26);
Console.Read();

}
}
}
上面的代码不起作用.我没有任何错误.有人可以检查出来吗.

I am sending sms using AT commands through the following c# code. Can someone check this simple code and suggest me why it is not working.
I can send my sms from hyperterminal using
AT+CSCA="msg centre no";
AT+CMGS="mobile no"

This the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;

namespace condemo
{
class Program
{
static void Main(string[] args)
{
SerialPort e = new SerialPort();
e.PortName = "COM4";
e.BaudRate = 460800;
e.Parity = Parity.None;
e.DataBits = 8;
e.StopBits = StopBits.One;

if (!(e.IsOpen))
{
try
{
e.Open();
}
catch (Exception m)
{
Console.WriteLine(m.Message);
}
}

e.Write("AT" + (char)13);

e.Write("AT+CPIN?" + (char)13);

e.Write("AT+CMGF=1"+ (char)13);

e.WriteLine("AT+CSCA=" + "9886005444" + (char)13);
e.WriteLine("AT+CMGS" + "9742899065" + (char)13);
e.WriteLine("message"; + (char)13);
e.Write(""+(char)26);
Console.Read();

}
}
}
The above code is not working. I am not getting any errors. Can someone check it out.

推荐答案

检查您的速度:460800似乎太高了.在此处 [
Check your speed: 460800 seems too high. There is a tutorial here[^] that suggests 230400 or less. What did you use in Hyperterminal?


这就是我的方法.
This is how I did it.
e = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);
e.Handshake = Handshake.RequestToSend;
e.WriteLine("AT+CMGF=1"+Environment.NewLine);
e.WriteLine("AT+CMGS" + "phone number here" + Environment.NewLine);
w.WriteLine("Message here"+(char)26+Environment.NewLine);



上面的代码可能包含一些小错误,因为我只是手工编写的.
如果我有更多要发送的号码,则每次发送短信时都必须关闭端口并打开.
我还没有研究为什么会发生这种情况.我的应用程序仍在开发中.

我相信您的问题可能是换行命令,我尝试使用"\ n","\ t \ n"等其他命令.
在我使用Environment.NewLine之前,什么都没做.

希望这会有所帮助.



The code above might contain some minor errors because i just wrote it by hand.
If i have more numbers to send to I have to close the port and open for each time I send a text message.
I havent looked into why it happens yet.My application is still being developed.

I believe your problem could be the newline command, I tried different commands using "\n","\t\n" and so on.
Nothing worked until i used Environment.NewLine.

Hope this helps.


这篇关于C#应用程序通过PC向其他手机发送短信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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