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 中删除我的手机后才会发送消息。如果我不删除我的手机并运行相同的程序,它会显示忙碌和 CMI 错误:503。
第二条消息永远不会发送(当程序再次编译时)。此外,正如您在程序中看到的那样,端口永远不会关闭。
这段代码可以做什么?请不要向我提供像 SMSLIB 这样的其他程序,而是改进/编辑此代码。
我尝试了大约 3 天,结果仍然是负面的。 请帮助我。我想在不一次又一次断开手机的情况下发送批量短信。
最佳答案
你绝对不能像那样使用 sleep ;您必须阅读并解析调制解调器给出的响应。像这样 sleep 只比根本不等待好一点(我在这个 answer 中提到)。看这个answer了解如何读取和解析您返回的响应。
顺便说一句,AT 命令应该仅以 \r
终止,而不是 \r\n
(除非您更改了 S3,并且您不应该这样做),请参阅V.250有关该指令和一般 AT 命令的更多详细信息(例如,如果您尚未阅读该规范,强烈建议您阅读该规范)。
关于java - 当短信再次发送时,命令显示忙(在java中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16946600/