您的位置:首页 > 编程语言 > Java开发

Java串口访问控制短信猫发送短信实例 .

2013-04-10 12:36 309 查看
//注意手机号码要奇偶移位 1369... -> 3196...

import gnu.io.*;

import java.util.*;

import java.io.*;

public class CommTest

{

static CommPortIdentifier portId;

static Enumeration portList;

static int bauds[] = { 9600,115200 ........};

public static void main(String[] args)

{

portList = CommPortIdentifier.getPortIdentifiers();

while (portList.hasMoreElements())

{

portId = (CommPortIdentifier) portList.nextElement();

if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)

{

System.out.println("Found port: " + portId.getName());

for (int i = 0; i < bauds.length; i++)

{

System.out.println("Trying at " + bauds[i] + "...");

SerialPort serialPort = null;

try

{

InputStream inStream;

OutputStream outStream;

int c;

String response;

serialPort = (SerialPort) portId.open("SMS", 2000);

serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN);

serialPort.setSerialPortParams(bauds[i], SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

inStream = serialPort.getInputStream();

outStream = serialPort.getOutputStream();

serialPort.enableReceiveTimeout(1000);

c = inStream.read();

while (c != -1) c = inStream.read();

outStream.write('A');

outStream.write('T');

outStream.write('/r');

try { Thread.sleep(1000); } catch (Exception e) {}

response = "";

c = inStream.read();

while (c != -1)

{

response += (char) c;

c = inStream.read();

}

if (response.indexOf("OK") >= 0)

{

try

{

System.out.println("Getting Info...");

outStream.write("AT+CMGF=0/r".getBytes());

getReply(inStream);

outStream.write("AT+CMGS=19/r".getBytes());

getReply(inStream);

outStream.write(("0011000D9168XXXXXXXXXXXF90008AA044F60597D"+(char)0x1a).getBytes());

getReply(inStream);

}

catch (Exception e)

{

e.printStackTrace();

}

}

else{

System.out.println("短信猫状态错误!"+response);

}

}

catch (Exception e)

{

System.out.println(" Nobody here!");

}

finally{

serialPort.close();

}

}

}

}

}

private static void getReply(InputStream inStream){

try {

String response="";

int c = inStream.read();

while (c != -1)

{

response += (char) c;

c = inStream.read();

}

System.out.println("Reply:" + response.replaceAll("/n", " "). replaceAll("/r", ""));

} catch (IOException e) {

e.printStackTrace();

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: