1
import javax.comm.*;
import java.io.*;
import java.util.*;


public class Sms {
public synchronized static String main1(String arr) {
 char cntrlZ=(char)26;
InputStream input = null;
OutputStream output = null;
SerialPort serialPort = null;
try {
CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier("COM3");


serialPort = (SerialPort) portId.open("SimpleReadApp1", 2000);
//System.out.println("sdiosdfdsf");
String f=null;int n;
input = serialPort.getInputStream();
output = serialPort.getOutputStream();


Thread readThread;
serialPort.notifyOnDataAvailable(true);

try {
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, 
SerialPort.STOPBITS_1, 
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}

output.write(("ATZ\r\natH\r\n+CMGW: 0\r\n+CMGW: 1\r\n").getBytes());
output.flush();Thread.sleep(200);
output.write(("ath0\r\n").getBytes());
output.flush();Thread.sleep(200);
output.write(("AT+CMGF=1\r\n").getBytes());
output.flush();Thread.sleep(200);
output.write(("AT+CMGS=\"09629993650\"\r\n+CMGW: 20\r\n").getBytes());

output.write(("hellooopssss445 545inoo you there?").getBytes());
output.write(("\032").getBytes());
output.flush();


Thread.sleep(2000);
byte[] readBuffer = new byte[120];

try {
while (input.available() > 0) {
int numBytes = input.read(readBuffer);
}

 input.close();
 output.close();
 serialPort.removeEventListener();
serialPort.sendBreak(1000);
serialPort.getInputStream().close();
serialPort.getOutputStream().close();
if (serialPort!=null)
System.out.print("Port is not null!!!");
//serialPort.closeport();
if (serialPort!=null)
System.out.print("Port is not null!!!");
System.out.print(new String(readBuffer));


return(new String(readBuffer));
} catch (IOException e) {}
 output.flush();

} catch (NoSuchPortException e) {
System.out.println("Exception in Adding Listener" + e);
} catch (PortInUseException e) {
System.out.println("Exception in Adding Listener" + e);
} catch (IOException e) {
System.out.println("Exception in Adding Listener" + e);
}
catch (InterruptedException e) {
System.out.println("Exception in Adding Listener" + e);
}
return ("fault");
}



public static void main(String[] arg) {


char ii[]=main1("").toCharArray();
for(int j=0;j<ii.length;j++)
{
if((ii[j]=='O')&&(ii[j+1]=='K'))
System.out.println("GOT");
}
}

}

当我编译并执行这个程序时,直到我从 USB 中移除我的手机才会发送消息。如果我不移除我的手机并运行相同的程序,它会显示 Busy 和 CMI ERROR : 503。

并且永远不会发送第二条消息(再次编译程序时)。此外,您可以在程序中看到端口永远不会关闭。

这段代码可以做什么?请不要向我提供一些其他程序,例如 SMSLIB,而是改进/编辑此代码。

我正在尝试这个大约 3 天,仍然是负面的结果。请帮帮我。我想在不断开手机的情况下一次又一次地发送批量短信。

4

1 回答 1

0

你绝不能这样使用睡眠;您必须阅读并解析调制解调器给出的响应。像这样睡觉只比完全不等待好一点(我在这个答案中提到了这一点)。请参阅此答案以了解如何阅读和解析您返回的响应。

顺便说一句,AT 命令应该只终止\r而不应该终止\r\n(除非您更改了 S3,并且您不应该这样做),请参阅V.250以了解有关该命令和一般 AT 命令的更多详细信息(例如,如果您尚未阅读规格,强烈推荐)。

于 2013-06-07T23:37:11.593 回答