我一直在尝试使用java程序中的AT命令发送消息。当手动发送命令时,似乎工作正常,但是当通过java发送时,我得到错误代码38(网络无序)。但是,当我手动键入命令然后使用Java发送消息时,它仍然有效,所以我认为问题在于命令字符串。有什么建议吗?
CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier("COM3");
SerialPort port = (SerialPort) portId.open("Application", 5000);
char ctrlz = 26;
String creturn = "\r";
port.setSerialPortParams(115200, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
port.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
OutputStream outputStream = port.getOutputStream();
outputStream.flush();
String command = "\rAT+CMGS=\"[PhoneNumber]\"" + creturn;
String message = "This is a java message test" + creturn;
String end = Character.toString(ctrlz);
System.out.println(command);
System.out.println(message);
Thread.sleep(100);
outputStream.write(command.getBytes());
Thread.sleep(1000);
outputStream.flush();
outputStream.write(message.getBytes());
Thread.sleep(1000);
outputStream.write(end.getBytes());
Thread.sleep(1000);
outputStream.flush();
Thread.sleep(1000);
port.close();
outputStream.close();