短信貓測試

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();
		}
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章