题
我用GSM手机通过AT命令发送短信。我想发送成千上万的消息。我读过GSM手机,我们每分钟可以发送6-8个短信。但是当我发送消息时,有人会去,而有人不会。我从excel文件获取信息意味着目的地号码和消息文本。你能告诉我为什么有些短信正在进行而有些则没有。我的代码是
SmsFields smsObj = null;
List<SmsFields> smsColl = null;
SerialPort serialport = null;
StringBuilder strbuild = new StringBuilder();
try
{
//Validate the form
if (!Validation()) return;
serialport = new SerialPort();
////Sets the properties of serial port object
serialport.PortName = cboPort.SelectedItem.ToString();
serialport.BaudRate = 9600;
serialport.Parity = Parity.None;
serialport.DataBits = 8;
serialport.StopBits = StopBits.One;
serialport.Handshake = Handshake.RequestToSend;
serialport.DtrEnable = true;
serialport.RtsEnable = true;
//Open the port to send sms
serialport.Open();
//Check if port is opened or not
if (!serialport.IsOpen)
{
MessageBox.Show("Serial port is not opened. Please try with other port");
return;
}
//Create smsFields class's object and fill the data in the generic collection
smsObj = SmsFields.Instance;
smsColl = smsObj.FillData(txtFilePath.Text);
if (smsColl == null)
{
MessageBox.Show("No data found in the excel table");
return;
}
//Gets the single record from SmsFields class and sends the message
foreach (SmsFields sms in smsColl)
{
//checks phone status
serialport.WriteLine("AT" + Environment.NewLine);
//Configures message as SMS (0 for PDU format) and (1 for text format)
serialport.WriteLine("AT+CMGF=1" + Environment.NewLine);
//Sets message center number
serialport.WriteLine("AT+CSCA=\"" + txtServiceNo.Text + "\"" + Environment.NewLine);
//Sets destination number
serialport.WriteLine("AT+CMGS=\"" + sms.DestinationNo + "\"" + Environment.NewLine);
//Specifies message and sends Ctrl+z
serialport.WriteLine(sms.Message + (char)26);
//Displays buffer containing output messages
System.Threading.Thread.Sleep(4000);
}
解决方案
我认为您的问题是您在发送下一个命令之前没有等待最终结果代码(即OK,ERROR和其他一些代码)。问题在于,如果未完成,新命令将触发正在进行的命令的中止。引用 V.250 :
5.6.1中止命令
...
中止 命令由。完成 从DTE到DCE的传输 任何角色。
所以始终发送AT命令时,必须等待最终结果代码才能发送下一个命令。
我建议将 serialport.WriteLine(&quot; ATxxx&quot; + Environment.NewLine)
重构为 sendCommand(serialport,&quot; ATxxx&quot;)
函数?然后你可以在该函数的末尾添加等待最终结果代码。
其他提示
尝试查看是否存在未发送的消息模式。因为这样可能会出现数字格式或消息中无效字符的问题。
另外,还有一些注意事项:
-
您没有进行任何错误检查。我会确保在调用每个命令后得到了预期的答复。
-
您正在使用Environment.NewLine完成每一行。我假设这是一个随底层操作系统而变化的属性。但是,AT标准非常清楚用于终止命令行的确切字符。
-
手机是真正的混蛋。仅仅因为您遵循规范或文档并不意味着他们这样做。假设每个手机型号的行为与其他手机型号不同。见第1点。
醇>
不隶属于 StackOverflow