java使用短信设备发送sms短信的示例(java发送短信)
import gnu.io.*;
import java.util.*;
import java.io.*;
public class commtest
{
static commportidentifier portid;
static enumeration portlist;
static int bauds[] = { 9600, 19200, 57600, 115200 }; //检测端口所支持的波特率
public static void main(string[] args)
{
portlist = commportidentifier.getportidentifiers();
system.out.println("短信设备端口连接测试...");
while (portlist.hasmoreelements())
{
portid = (commportidentifier) portlist.nextelement();
if (portid.getporttype() == commportidentifier.port_serial)
{
system.out.println("找到串口: " + portid.getname());
for (int i = 0; i < bauds.length; i++)
{
system.out.print(" trying at " + bauds[i] + "...");
try
{
serialport serialport;
inputstream instream;
outputstream outstream;
int c;
string response;
serialport = (serialport) portid.open("smslibcommtester", 1971);
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.print(" 获取设备信息...");
outstream.write('a');
outstream.write('t');
outstream.write('+');
outstream.write('c');
outstream.write('g');
outstream.write('m');
outstream.write('m');
outstream.write('\r');
response = "";
c = instream.read();
while (c != -1)
{
response += (char) c;
c = instream.read();
}
system.out.println(" 发现设备: " + response.replaceall("\\s+ok\\s+", "").replaceall("\n", "").replaceall("\r", ""));
}
catch (exception e)
{
system.out.println(" 没有发现设备!");
}
}
else system.out.println(" 没有发现设备!");
serialport.close();
}
catch (exception e)
{
system.out.println(" 没有发现设备!");
}
}
}
}
}
}
上一篇: 基于SQL中的数据查询语句汇总