使用javax.comm向手机发送AT指令(短信)问题?

clh1981 2011-03-09 03:42:43
本人使用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.");
}
}

}
...全文
71 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
xzdfdxgxgx 2011-04-01
其实我不懂
回复
clh1981 2011-03-10
怎么没有人解答呀。
回复
相关推荐
发帖
J2ME
创建于2007-09-28

1.3w+

社区成员

Java J2ME
申请成为版主
帖子事件
创建了帖子
2011-03-09 03:42
社区公告
暂无公告