使用javax.comm向手机发送AT指令(短信)问题?
本人使用javax.comm向手机发送AT指令(短信),手机没有反应。使用微软的超级终端可以发短信。哪位有成功的源代码没有呀?给我学习一下,多谢!!!!
发到我的邮箱谢谢!!!cailinhui@163.com
这是我的代码:
package com;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;
import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.UnsupportedCommOperationException;
public class Test {
/**
* @param args
*/
static Enumeration portList;
static CommPortIdentifier portId;
static String messageString = "Hello, world!";
static SerialPort serialPort;
static OutputStream outputStream;
static boolean outputBufferEmptyFlag = false;
public static void main(String[] args) {
boolean portFound = false;
String defaultPort = "COM5";
if (args.length > 0) {
defaultPort = args[0];
System.out.println("test args");
}
portList = CommPortIdentifier.getPortIdentifiers();
System.out.println(portList.hasMoreElements());
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
System.out.println("portid:" + portId.getName());
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(defaultPort)) {
System.out.println("Found port " + defaultPort);
portFound = true;
try {
serialPort = (SerialPort) portId.open("Test", 2000);
} catch (PortInUseException e) {
System.out.println("Port in use.");
continue;
}
try {
outputStream = serialPort.getOutputStream();
} catch (IOException e) {
}
try {
serialPort.setSerialPortParams(115200,
SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
}
try {
serialPort.notifyOnOutputEmpty(true);
} catch (Exception e) {
System.out.println("Error setting event notification");
System.out.println(e.toString());
System.exit(-1);
}
System.out.println("Writing \"" + messageString + "\" to "
+ serialPort.getName());
try {
System.out.println("start.......");
outputStream.write("AT+CGMI".getBytes());
//outputStream.write("AT+CMGF=0\r".getBytes());
//outputStream.write("AT+CMGS=28\r".getBytes());
//outputStream.write("0011000B813188151616F10008FF0E6B228FCE6765523054C85DE559270X000X1A".getBytes());
} catch (IOException e) {
System.err.println("发生了某些异常");
}
try {
Thread.sleep(2000); // Be sure data is xferred before
// closing
} catch (Exception e) {
}
serialPort.close();
System.exit(1);
}
}
}
if (!portFound) {
System.out.println("port " + defaultPort + " not found.");
}
}
}