C# 用于发送短消息的AT命令在Windows 8.1中不起作用
我现在已经研究了两天多,试图制作一个使用AT命令发送短信的应用程序,我在网上实现了一些教程和项目。不幸的是,他们都没有工作 []此代码使我执行了命令,但未发送消息 然后我尝试了另一个项目(我使用的是C#和Visual Studio 2013),其中包含以下文件,执行后状态为“更改为已发送消息”,但我没有收到消息。我使用的是华为移动连接-3G应用接口GSM调制解调器 Program.csC# 用于发送短消息的AT命令在Windows 8.1中不起作用,c#,.net,visual-studio-2013,at-command,C#,.net,Visual Studio 2013,At Command,我现在已经研究了两天多,试图制作一个使用AT命令发送短信的应用程序,我在网上实现了一些教程和项目。不幸的是,他们都没有工作 []此代码使我执行了命令,但未发送消息 然后我尝试了另一个项目(我使用的是C#和Visual Studio 2013),其中包含以下文件,执行后状态为“更改为已发送消息”,但我没有收到消息。我使用的是华为移动连接-3G应用接口GSM调制解调器 Program.cs using System; using System.Collections.Generic; using S
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CSharp_SMS
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form_SMS_Sender());
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CSharp_SMS
{
public partial class Form_SMS_Sender : Form
{
private SerialPort _serialPort;
public Form_SMS_Sender()
{
InitializeComponent();
}
private void buttonSend_Click(object sender, EventArgs e)
{
string number = textBoxNumber.Text;
string message = textBoxMessage.Text;
_serialPort = new SerialPort("COM17", 19200); //Replace "COM7" with corresponding port name
Thread.Sleep(1000);
_serialPort.Open();
Thread.Sleep(1000);
_serialPort.Write("AT+CMGF=1\r");
Thread.Sleep(1000);
_serialPort.Write("AT+CMGS=\"" + number + "\"\r\n");
Thread.Sleep(1000);
_serialPort.Write(message + "\x1A");
Thread.Sleep(1000);
labelStatus.Text = "Status: Message sent";
_serialPort.Close();
}
}
}
节目有问题吗?我错过什么了吗?或者,在Windows8.1中运行这个程序有问题,因为我还发现有一个叫做MS HyperTerminal的程序,我不清楚其中的哪一部分 我使用
和代码
private const string LT = "\r\n";
public void Auth(string pin)
{
lock (smsSendSync)
{
//Check if gateway is alive
lastSplit = SplitResponse(SendCommand("AT"));
if (!(lastSplit[lastSplit.Length - 1] == "OK"))
throw new OperationCanceledException("AT connection failed");
//Echo ON
lastSplit = SplitResponse(SendCommand("ATE1"));
if (!(lastSplit[lastSplit.Length - 1] == "OK"))
throw new OperationCanceledException("ATE command failed");
//Check echo
lastSplit = SplitResponse(SendCommand("AT"));
if (!(lastSplit.Length == 2 && lastSplit[1] == "OK"))
throw new OperationCanceledException("AT command failed");
//Verbose error reporting
lastSplit = SplitResponse(SendCommand("AT+CMEE=2"));
if (!(lastSplit.Length == 2 && lastSplit[1] == "OK"))
throw new OperationCanceledException("AT+CMEE command failed");
//Enter a PIN
lastSplit = SplitResponse(SendCommand("AT+CPIN?"));
if (!(lastSplit.Length == 3 && lastSplit[2] == "OK"))
throw new OperationCanceledException("AT+CPIN? command failed");
switch (lastSplit[1])
{
case "+CPIN: READY": //no need to enter PIN
break;
case "+CPIN: SIM PIN": //PIN requested
lastSplit = SplitResponse(SendCommand("AT+CPIN=" + pin));
string m_receiveData = String.Empty;
WaitForResponse(out m_receiveData);
if (m_receiveData == String.Empty)
throw new OperationCanceledException("PIN authentification timed out");
break;
default:
throw new OperationCanceledException("Unknown PIN request");
}
//Check if registered to a GSM network
lastSplit = SplitResponse(SendCommand("AT+CREG?"));
if (!(lastSplit.Length == 3 && lastSplit[2] == "OK"))
throw new OperationCanceledException("AT+CREG? command failed");
lastSplit = lastSplit[1].Split(new string[] {" ", ","}, StringSplitOptions.RemoveEmptyEntries);
if (!(lastSplit[2] == "1" || lastSplit[2] == "5"))
throw new OperationCanceledException("Not registered to a GSM network");
Debug.WriteLine("Authentification successfull");
}
}
private string[] SplitResponse(string response)
{
string[] split = response.Split(new string[] { LT }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < split.Length; i++)
split[i] = split[i].Trim();
return split;
}
public string SendCommand(string command)
{
string m_receiveData = string.Empty;
smsPort.ReadExisting(); //throw away any garbage
smsPort.WriteLine(command + LT);
WaitForResponse(out m_receiveData);
//Debug.WriteLine(m_receiveData);
return m_receiveData;
}
public string SendSms2(string phoneNumber, string message, bool flashMsg, SMS.SMSEncoding encoding)
{
if (phoneNumber.StartsWith("00"))
phoneNumber = "+" + phoneNumber.Substring(2);
if (phoneNumber.StartsWith("0"))
//replace with your national code
phoneNumber = "+386" + phoneNumber.Substring(1);
string StatusMessage = string.Empty;
SMS sms = new SMS(); //Compose PDU SMS
sms.Direction = SMSDirection.Submited; //Setting direction of sms
sms.Flash = flashMsg; //Sets the flash property of SMS
sms.PhoneNumber = phoneNumber.Replace(" ",""); //Set the recipient number
sms.MessageEncoding = encoding; //Sets the Message encoding for this SMS
sms.ValidityPeriod = new TimeSpan(4, 0, 0, 0); //Set validity period
sms.Message = message; //Set the SMS Message text
string sequence = sms.Compose() + CtrlZ; //Compile PDU unit
string sequenceLength = ((sequence.Length - 3) / 2).ToString();
lock (smsSendSync)
{
StatusMessage = SendCommand("AT+CMGS=" + sequenceLength) + " ";
Thread.Sleep(500);
StatusMessage += SendCommand(sequence);
}
Debug.WriteLine(StatusMessage);
if (StatusMessage.Contains("ERROR"))
throw new OperationCanceledException("Error sending SMS");
return StatusMessage;
}
private const string LT=“\r\n”;
公共无效身份验证(字符串pin)
{
锁(smsSendSync)
{
//检查网关是否处于活动状态
lastplit=SplitResponse(SendCommand(“AT”);
如果(!(lastSplit[lastSplit.Length-1]=“OK”))
抛出新操作CanceledException(“连接失败时”);
//呼应
lastSplit=SplitResponse(SendCommand(“ATE1”);
如果(!(lastSplit[lastSplit.Length-1]=“OK”))
抛出新操作CanceledException(“ATE命令失败”);
//检查回声
lastplit=SplitResponse(SendCommand(“AT”);
如果(!(lastSplit.Length==2&&lastSplit[1]==OK”))
抛出新操作CanceledException(“AT命令失败”);
//详细错误报告
lastSplit=SplitResponse(SendCommand(“AT+CMEE=2”);
如果(!(lastSplit.Length==2&&lastSplit[1]==OK”))
抛出新操作CanceledException(“AT+CMEE命令失败”);
//输入PIN码
lastplit=SplitResponse(SendCommand(“AT+CPIN?”);
如果(!(lastplit.Length==3&&lastplit[2]==OK”))
抛出新操作CanceledException(“AT+CPIN?命令失败”);
开关(lastSplit[1])
{
案例“+CPIN:READY”://无需输入PIN
打破
案例“+CPIN:SIM PIN”://请求PIN
lastSplit=SplitResponse(SendCommand(“AT+CPIN=“+pin”);
string m_receiveData=string.Empty;
WaitForResponse(out m_receiveData);
if(m_receiveData==String.Empty)
抛出新操作CanceledException(“PIN身份验证超时”);
打破
违约:
抛出新操作取消异常(“未知PIN请求”);
}
//检查是否已注册到GSM网络
lastSplit=SplitResponse(SendCommand(“AT+CREG?”);
如果(!(lastplit.Length==3&&lastplit[2]==OK”))
抛出新操作CanceledException(“AT+CREG?命令失败”);
lastSplit=lastSplit[1]。拆分(新字符串[]{“”,“},StringSplitOptions.RemoveEmptyEntries);
如果(!(lastSplit[2]=“1”| | lastSplit[2]=“5”))
抛出新操作CanceledException(“未注册到GSM网络”);
Debug.WriteLine(“身份验证成功”);
}
}
私有字符串[]拆分响应(字符串响应)
{
string[]split=response.split(新字符串[]{LT},StringSplitOptions.RemoveEmptyEntries);
for(int i=0;i
使用
Auth()
public bool sendMsg(string smsid, string PhoneNo, string Message, string from, string to)
{
string recievedData;
bool isSend = false;
string text = "Hello " + to + ",\n" + Message + "\n\n" + from;
if (!port.IsOpen)
port = OpenPort();
recievedData = ExecCommand(port, "AT+CMGF=1", 400, "Failed to set message format.");
try
{
//string recievedData; // = ExecCommand(port, "AT", 3000, "No phone connected");
String command = "AT+CMGS=\"" + PhoneNo + "\"";
recievedData = ExecCommand(port, command, 1000, "Failed to accept phoneNo");
command = text + char.ConvertFromUtf32(26) + "\r";
recievedData = ExecCommand(port, command, 1000, "Failed to send message");
if (recievedData.Contains("OK"))
{
isSend = true;
}
else if (recievedData.Contains("ERROR"))
{
isSend = false;
}
}
catch (Exception ex)
{
MyLog.Write(new LogPacket(ex, DateTime.Now));
}
return isSend;
}
应该删除该教程(相关帖子:和)。什么不起作用?有例外吗?根本没有结果?您必须更改一些SerialPort属性,不要等待使用Thred.Sleep(),检查调制解调器响应(顺便说一句,第一次睡眠是无用的)。我还建议莫夫