使用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.");
}
}

}
...全文
124 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
xzdfdxgxgx 2011-04-01
  • 打赏
  • 举报
回复
其实我不懂
clh1981 2011-03-10
  • 打赏
  • 举报
回复
怎么没有人解答呀。

13,097

社区成员

发帖
与我相关
我的任务
社区描述
Java J2ME
社区管理员
  • J2ME社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧