import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.InputStream;
import java.io.OutputStream;
/**
* 短信猫测试,需插上USB和电源
* 这个短信猫跟短信塔无线网络通讯
* 读取到OK则测试成功
* win32模式:javax.comm.properties,win32com.dll放到工程下
* 并导入comm.jar,适用于window32系统
* rx模式:rxtxSerial.dll放到工程下并导入RXTXcomm.jar
*/
public class Test {
public static void main(String[] args) {
// 加载库文件,用win32要执行该代码
// System.loadLibrary("win32com");
//如果是rx则不用该代码
// System.loadLibrary("rxtxSerial");
// 得到系统的所有口名称,有COM4这个串口
// java.util.Enumeration en = CommPortIdentifier.getPortIdentifiers();
//
// while (en.hasMoreElements()) {
//
// CommPortIdentifier comm = (CommPortIdentifier) en.nextElement();
// System.out.println(comm.getName());
// System.out.println(comm.getPortType());
// System.out.println("----------------------");
// }
try {
// 指定得到com4窗口对象
CommPortIdentifier com4 = CommPortIdentifier
.getPortIdentifier("COM3");
// 打开串口,2个参数随便设置
SerialPort comm = (SerialPort) com4.open("mycom4", 600);
// 下面参数不设置也可以
// comm.setSerialPortParams(9600, SerialPort.DATABITS_8,
// SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
// comm.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
InputStream in = comm.getInputStream();
OutputStream out = comm.getOutputStream();
// 1.发送指定英文格式的短信,AT是测试命令,\r是换行
out.write("AT\r".getBytes());
// 这里休息,是因为发短信需要一定时间才能发出
// 不休息马上接可能会接收不到
Thread.sleep(500);
byte[] b = new byte[1000];
in.read(b);
System.out.println(new String(b).trim());
} catch (Exception e) {
e.printStackTrace();
}
}
}
短信猫测试
最新推荐文章于 2016-03-09 15:14:25 发布