1
我想做一个程序发送短信。我编写了程序,但没有成功发送消息。我的程序向我的计算机的端口COM发送一个At命令,但是我没有收到我的GSM调制解调器的响应。我正在使用COM终端(Temp pro ...)使用at命令发送短信,并且我可以发送短信。所以我不知道为什么这个程序不能发送短信。发送命令到与gsm调制解调器与Java
import java.io.*;
import java.util.*;
import gnu.io.*;
public class prawiefinal {
static Enumeration portList;
static CommPortIdentifier portId;
static String messageString = "AT+CMGF=1 \r";
static String messageString2 = "AT+CMGS=\"+4866467xxxx\"\r";
static String messageString3 = "TESting \u001A\r\n";
static SerialPort serialPort;
static OutputStream outputStream;
public static void main(String[] args) throws InterruptedException {
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM3")) {
try {
serialPort = (SerialPort)
portId.open("COM3", 2000);
} catch (PortInUseException e) {System.out.println("err");}
try {
outputStream = serialPort.getOutputStream();
} catch (IOException e) {e.printStackTrace();}
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {e.printStackTrace();}
try {
outputStream.write(messageString.getBytes());
Thread.sleep(3000);
outputStream.flush();
outputStream.write(messageString2.getBytes());
Thread.sleep(3000);
outputStream.flush();
outputStream.write(messageString3.getBytes());
Thread.sleep(3000);
outputStream.write(26);
outputStream.flush();
System.out.println(messageString);
Thread.sleep(3000);
outputStream.close();
serialPort.close();
} catch (IOException e) {e.printStackTrace());}
}
}
}
}
}
而不是使用'调用println( “ERR3”)的'放一个'e.printStackTrace()'在'catch'条款。这可能更好地解释什么是例外。在所有'catch'子句中这样做,[编辑]你的问题并将堆栈跟踪添加到它。记住要正确格式化它(不使用反引号,而是使用'{}'按钮)。 – RealSkeptic
什么是你使用的jar文件** SerialPort ** –