有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

再次发送sms时gsm At命令显示忙碌(java)

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这样的其他程序,而是改进/编辑此代码

我试了大约三天,结果仍然是负面的。 请帮帮我。我想发送大量短信而不必一次又一次地断开手机


共 (1) 个答案

  1. # 1 楼答案

    你必须永远不要像那样使用睡眠;必须读取并解析调制解调器给出的响应。像那样睡觉只比完全不等待稍微好一点(我在本answer中提到了这一点)。有关如何读取和解析返回的响应,请参见此answer

    顺便说一句,AT命令应该只以\r而不是\r\n终止(除非您已经更改了S3,并且不应该这样做),有关该命令和AT命令的更多详细信息,请参阅V.250(例如,如果您还没有阅读该规范,强烈建议您这样做)