使用Java将At命令发送到gsm调制解调器
我正在尝试做一个程序来发送短信。我编写了程序,但没有成功发送消息。我的程序向计算机中的COM端口发送At命令,但我没有收到gsm调制解调器的响应。我使用COM终端(Temp pro…)发送带有at命令的短信,并且我能够发送短信。因此我不知道为什么程序不能发送短信使用Java将At命令发送到gsm调制解调器,java,gsm,at-command,gsmcomm,Java,Gsm,At Command,Gsmcomm,我正在尝试做一个程序来发送短信。我编写了程序,但没有成功发送消息。我的程序向计算机中的COM端口发送At命令,但我没有收到gsm调制解调器的响应。我使用COM终端(Temp pro…)发送带有at命令的短信,并且我能够发送短信。因此我不知道为什么程序不能发送短信 import java.io.*; import java.util.*; import gnu.io.*; public class prawiefinal { static Enumeration portList;
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());}
}
}
}
}
}
好的,我制作了这个程序,我可以发短信。我使用CTRL+Z并输入,这是一个问题:D
import java.io.*;
import java.util.*;
import gnu.io.*;
public class SMSsender {
static Enumeration portList;
static CommPortIdentifier portId;
static String messageString1 = "AT";
static String messageString2 = "AT+CPIN=\"7078\"";
static String messageString3 = "AT+CMGF=1";
static String messageString4 = "AT+CMGS=\"+4866467xxxx\"";
static String messageString5 = "TESTY2";
static SerialPort serialPort;
static OutputStream outputStream;
static InputStream inputStream;
static char enter = 13;
static char CTRLZ = 26;
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();
inputStream = serialPort.getInputStream();
} 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((messageString1 + enter).getBytes());
Thread.sleep(100);
outputStream.flush();
outputStream.write((messageString2 + enter).getBytes());
Thread.sleep(100);
outputStream.flush();
outputStream.write((messageString3 + enter).getBytes());
Thread.sleep(100);
outputStream.flush();
outputStream.write((messageString4 + enter).getBytes());
Thread.sleep(100);
outputStream.flush();
outputStream.write((messageString5 + CTRLZ).getBytes());
outputStream.flush();
Thread.sleep(100);
System.out.println("Wyslano wiadomosc");
Thread.sleep(3000);
outputStream.close();
serialPort.close();
System.out.println("Port COM zamkniety");
} catch (IOException e) {e.printStackTrace();}
}
}
}
}
}不要使用
println(“err3”)
而是在catch
子句中添加e.printStackTrace()
。它可以更好地解释什么是例外。在所有的catch
子句中都这样做,然后添加堆栈跟踪。请记住正确格式化它(不是使用反勾号,而是使用{}
按钮)