Пытаюсь сделать программу для отправки смс. Я написал программу, но не смог отправить сообщение. Моя программа отправляет команду At на порт COM на моем компьютере, но я не получаю ответа от своего модема GSM. Я использую COM-терминал (Temp pro ...) для отправки sms с помощью команды at, и я могу отправлять sms. Поэтому я не знаю, почему программа не может отправлять смс.
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
, отредактируйте свой вопрос и добавьте к нему трассировку стека. Не забудьте правильно отформатировать его (не используя обратные кавычки, а используя кнопку{}
). - person RealSkeptic   schedule 12.09.2016