- 浏览: 29124 次
- 性别:
- 来自: 厦门
-
文章分类
最新评论
-
gentlesong88:
赞一个 基础的操作都有了
条形码/二维码之开源利器ZXing图文介绍(转) -
苇间风语:
...
条形码/二维码之开源利器ZXing图文介绍(转) -
songfantasy:
学习了,今天刚好遇到这样的困惑。3Q
将变量置入循环
摘选:http://sjsky.iteye.com/blog/1045502
用java实现短信收发的功能,目前一般项目中短信群发功能的实现方法大致有下面三种:
鉴于项目的情况和多方考虑,同时又找到了一个开源的SMSLib项目的支持,比较倾向于第二种方法,SMSLib的出现就不需要我们自己去写底层的AT指令,这样就可以直接通过调用SMSLib的API来实现通过GSM modem来收发送短信了。
SMSLib官方网站:http://smslib.org/
,使用SMSLib的一些基本要点:
- SUN JDK 1.6 or newer. (Java环境)
- Java Communications Library. (Java串口通信)
- Apache ANT for building the sources. (编译源码时需要的)
- Apache log4j. (日志工具)
- Apache Jakarta Commons - NET. (网络操作相关的)
- JSMPP Library (SMPP协议时需要的)
有关Java串口通信需要补充说明:
- window系统可以用SUN Java Comm v2. (该版本好像也支持solaris) 其下载地址:http://smslib.googlecode.com/files/javacomm20-win32.zip
- 其他操作系统(比如:Linux, Unix, BSD,等等),你可以选择 Java Comm v3 或者是RxTx。 Java Comm v3下载地址:http://java.sun.com/products/javacomm/ (需要注册);
RxTx官网:http://users.frii.com/jarvi/rxtx/index.html or http://rxtx.qbang.org/wiki/index.php/Main_Page
附件提供相关下载:
- java串口通信v2:javacomm20-win32.zip
- smslib-3.5.1.jar
- 短信 modem驱动:PL2303_Prolific_DriverInstaller_v130.zip
本次测试的环境是window,GSM modem是wavecom,所以这次主要描述window环境下简单的实现过程:
【一】、配置相应的环境
首先解压下载的Java Comm v2文件javacomm20-win32.zip,具体配置步骤如下:
- 把文件:comm.jar copy 到目录:<JDKDIR>/jre/lib/ext/,当然这一步也可以不要这样做,你只需把comm.jar copy到所要运行的项目对应的lib/下既可
- 把文件:javax.comm.properties copy 到目录:<JDKDIR>/jre/lib/
- 把DLL文件:win32com.dll(windows) copy 到目录:<JDKDIR>/jre/bin/
- 如果存在JRE目录, 最好按照上面步骤把文件copy到<JREDIR>相应的目录下
【二】、测试串口端口程序:
TestGetPortList.java
- package michael.comm.serial;
- import java.util.Enumeration;
- import javax.comm.CommDriver;
- import javax.comm.CommPortIdentifier;
- import javax.comm.SerialPort;
- /**
- * @author michael
- *
- */
- public class TestGetPortList {
- /**
- * @param args
- * @throws Exception
- */
- public static void main(String[] args) throws Exception {
- // 人工加载驱动
- // MainTest.driverInit();
- TestGetPortList.getCommPortList();
- // 人工加载驱动获取端口列表
- // TestGetPortList.getPortByDriver();
- }
- /**
- * 手工加载驱动<br>
- * 正常情况下程序会自动加载驱动,故通常不需要人工加载<br>
- * 每重复加载一次,会把端口重复注册,CommPortIdentifier.getPortIdentifiers()获取的端口就会重复
- */
- public static void driverManualInit() {
- String driverName = "com.sun.comm.Win32Driver" ;
- String libname = "win32com" ;
- CommDriver commDriver = null ;
- try {
- System.loadLibrary("win32com" );
- System.out.println(libname + " Library Loaded" );
- commDriver = (javax.comm.CommDriver) Class.forName(driverName)
- .newInstance();
- commDriver.initialize();
- System.out.println("comm Driver Initialized" );
- } catch (Exception e) {
- System.err.println(e);
- }
- }
- /**
- * 获取端口列表
- */
- public static void getCommPortList() {
- CommPortIdentifier portId;
- Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
- while (portEnum.hasMoreElements()) {
- portId = (CommPortIdentifier) portEnum.nextElement();
- if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
- System.out.println("串口: name-" + portId.getName()
- + " 是否占用-" + portId.isCurrentlyOwned());
- } else {
- System.out.println("并口: name-" + portId.getName()
- + " 是否占用-" + portId.isCurrentlyOwned());
- }
- }
- System.out.println("-------------------------------------" );
- }
- /**
- *
- */
- public static void getPortByDriver() {
- String driverName = "com.sun.comm.Win32Driver" ;
- String libname = "win32com" ;
- CommDriver commDriver = null ;
- try {
- System.loadLibrary("win32com" );
- System.out.println(libname + " Library Loaded" );
- commDriver = (CommDriver) Class.forName(driverName).newInstance();
- commDriver.initialize();
- System.out.println("comm Driver Initialized" );
- } catch (Exception e) {
- System.err.println(e);
- }
- SerialPort sPort = null ;
- try {
- sPort = (SerialPort) commDriver.getCommPort("COM24" ,
- CommPortIdentifier.PORT_SERIAL);
- System.out.println("find CommPort:" + sPort.toString());
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- }
- }
本机运行结果:
串口: name-COM10 是否占用-false
串口: name-COM21 是否占用-false
串口: name-COM23 是否占用-false
串口: name-COM20 是否占用-false
串口: name-COM22 是否占用-false
串口: name-COM24 是否占用-false
串口: name-COM9 是否占用-false
串口: name-COM19 是否占用-false
串口: name-COM3 是否占用-false
串口: name-COM8 是否占用-false
串口: name-COM98 是否占用-false
串口: name-COM99 是否占用-false
串口: name-COM4 是否占用-false
串口: name-COM5 是否占用-false
串口: name-COM6 是否占用-false
并口: name-LPT1 是否占用-false
并口: name-LPT2 是否占用-false
-------------------------------------
【三】、检查串口设备信息:
TestCommPort.java
- package michael.comm.serial;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.Enumeration;
- import javax.comm.CommPortIdentifier;
- import javax.comm.SerialPort;
- /**
- * @author michael
- *
- */
- public class TestCommPort {
- static CommPortIdentifier portId;
- static Enumeration portList;
- static int bauds[] = { 9600 , 19200 , 57600 , 115200 };
- /**
- * @param args
- */
- public static void main(String[] args) {
- portList = CommPortIdentifier.getPortIdentifiers();
- System.out.println("GSM Modem 串行端口连接测试开始..." );
- String portName = "COM24" ;
- while (portList.hasMoreElements()) {
- portId = (CommPortIdentifier) portList.nextElement();
- if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL
- && portName.equals(portId.getName())) {
- 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;
- StringBuffer response = new StringBuffer();
- serialPort = (SerialPort) portId.open(
- "SMSLibCommTester" , 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) {
- }
- c = inStream.read();
- while (c != - 1 ) {
- response.append((char ) c);
- c = inStream.read();
- }
- if (response.indexOf( "OK" ) >= 0 ) {
- System.out.print(" 正在检测设备:" );
- try {
- outStream.write('A' );
- outStream.write('T' );
- outStream.write('+' );
- outStream.write('C' );
- outStream.write('G' );
- outStream.write('M' );
- outStream.write('M' );
- outStream.write('\r' );
- response = new StringBuffer();
- c = inStream.read();
- while (c != - 1 ) {
- response.append((char ) c);
- c = inStream.read();
- }
- System.out.println(" 发现设备: "
- + response.toString().replaceAll(
- "(\\s+OK\\s+)|[\n\r]" , "" ));
- } catch (Exception e) {
- System.out.println(" 检测设备失败,获取设备信息异常:"
- + e.getMessage());
- }
- } else {
- System.out.println(" 检测设备失败,沒有接收到响应结果!" );
- }
- serialPort.close();
- } catch (Exception e) {
- System.out.println(" 检测设备失败,发生异常:" + e.getMessage());
- }
- }
- }
- }
- }
- }
运行结果如下:
GSM Modem 串行端口连接测试开始...
找到串口: COM24
Trying at 9600... 正在检测设备: 发现设备: AT+CGMM MULTIBAND 900E 1800
Trying at 19200... 发现设备失败,沒有接收到响应结果!
Trying at 57600... 发现设备失败,沒有接收到响应结果!
Trying at 115200... 发现设备失败,沒有接收到响应结果!
【四】、测试收发短信:
- package michael.sms;
- import java.util.ArrayList;
- import java.util.LinkedList;
- import java.util.List;
- import org.apache.log4j.Level;
- import org.apache.log4j.Logger;
- import org.smslib.AGateway;
- import org.smslib.GatewayException;
- import org.smslib.InboundMessage;
- import org.smslib.OutboundMessage;
- import org.smslib.Service;
- import org.smslib.AGateway.Protocols;
- import org.smslib.Message.MessageEncodings;
- import org.smslib.modem.SerialModemGateway;
- /**
- * @author michael
- *
- */
- public class SmsHandler {
- private static final Logger logger = Logger.getLogger(SmsHandler. class );
- private Service smsService;
- /**
- *
- */
- public SmsHandler() {
- smsService = Service.getInstance();
- List<AGateway> agatewayList = new ArrayList<AGateway>();
- String portName = "COM24" ; //"/dev/ttyUSB0";// COM24
- SerialModemGateway gateway = new SerialModemGateway(
- "modem." + portName, portName, 9600 , "wavecom" , "PL2303" );
- gateway.setInbound(true );
- gateway.setOutbound(true );
- gateway.setProtocol(Protocols.PDU);
- gateway.setSimPin("0000" );
- agatewayList.add(gateway);
- try {
- for (AGateway gatewayTmp : agatewayList) {
- smsService.addGateway(gatewayTmp);
- }
- } catch (GatewayException ex) {
- logger.error(ex.getMessage());
- }
- }
- /**
- *
- */
- public void start() {
- logger.info("SMS service start....." );
- try {
- smsService.startService();
- } catch (Exception ex) {
- logger.error("SMS service start error:" , ex);
- }
- }
- /**
- *
- */
- public void destroy() {
- try {
- smsService.stopService();
- } catch (Exception ex) {
- logger.error("SMS service stop error:" , ex);
- }
- logger.info("SMS service stop" );
- }
- /**
- * send SMS
- * @param msg
- * @return Boolean
- */
- public Boolean sendSMS(OutboundMessage msg) {
- try {
- msg.setEncoding(MessageEncodings.ENCUCS2);
- return smsService.sendMessage(msg);
- } catch (Exception e) {
- logger.error("send error:" , e);
- }
- return false ;
- }
- private boolean isStarted() {
- if (smsService.getServiceStatus() == Service.ServiceStatus.STARTED) {
- for (AGateway gateway : smsService.getGateways()) {
- if (gateway.getStatus() == AGateway.GatewayStatuses.STARTED) {
- return true ;
- }
- }
- }
- return false ;
- }
- /**
- * read SMS
- * @return List
- */
- public List<InboundMessage> readSMS() {
- List<InboundMessage> msgList = new LinkedList<InboundMessage>();
- if (!isStarted()) {
- return msgList;
- }
- try {
- this .smsService.readMessages(msgList,
- InboundMessage.MessageClasses.ALL);
- logger.info("read SMS size: " + msgList.size());
- } catch (Exception e) {
- logger.error("read error:" , e);
- }
- return msgList;
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- Logger.getRootLogger().setLevel(Level.INFO);
- OutboundMessage outMsg = new OutboundMessage( "189xxxx****" , "信息测试" );
- SmsHandler smsHandler = new SmsHandler();
- smsHandler.start();
- //发送短信
- smsHandler.sendSMS(outMsg);
- //读取短信
- List<InboundMessage> readList = smsHandler.readSMS();
- for (InboundMessage in : readList) {
- System.out.println("发信人:" + in.getOriginator() + " 短信内容:"
- + in.getText());
- }
- smsHandler.destroy();
- System.out.println("-----------" );
- }
- }
发送短信亲测,手机能正常接收显示。读取设备的短信程序运行结果结果如下:
INFO - Service.listSystemInformation(113) | SMSLib: A Java API library for sending and receiving SMS via a GSM modem or other supported gateways.
This software is distributed under the terms of the Apache v2.0 License.
Web Site: http://smslib.org
INFO - Service.listSystemInformation(114) | Version: 3.5.1
INFO - Service.listSystemInformation(115) | JRE Version: 1.6.0_18
INFO - Service.listSystemInformation(116) | JRE Impl Version: 16.0-b13
INFO - Service.listSystemInformation(117) | O/S: Windows Vista / x86 / 6.0
INFO - SmsHandler.start(55) | SMS service start.....
INFO - DefaultQueueManager.init(92) | Queue directory not defined. Queued messages will not be saved to filesystem.
INFO - ModemGateway.startGateway(188) | GTW: modem.COM24: Starting gateway, using Generic AT Handler.
INFO - SerialModemDriver.connectPort(68) | GTW: modem.COM24: Opening: COM24 @9600
INFO - AModemDriver.waitForNetworkRegistration(459) | GTW: modem.COM24: GSM: Registered to foreign network (roaming).
INFO - AModemDriver.connect(175) | GTW: modem.COM24: MEM: Storage Locations Found: SMBM
INFO - CNMIDetector.getBestMatch(142) | CNMI: No best match, returning: 1
INFO - ModemGateway.startGateway(191) | GTW: modem.COM24: Gateway started.
INFO - SmsHandler.readSMS(113) | read SMS size: 1
发信人:8618918001030 短信内容:hello 回复测试
INFO - ModemGateway.stopGateway(197) | GTW: modem.COM24: Stopping gateway...
INFO - SerialModemDriver.disconnectPort(120) | GTW: modem.COM24: Closing: COM24 @9600
INFO - ModemGateway.stopGateway(201) | GTW: modem.COM24: Gateway stopped.
INFO - SmsHandler.destroy(72) | SMS service stop
-----------
- PL2303_Prolific_DriverInstaller_v130.zip (2.3 MB)
- 下载次数: 66
- javacomm20-win32.zip (266.3 KB)
- 下载次数: 36
- smslib-3.5.1.jar (345 KB)
- 下载次数: 37
发表评论
-
条形码/二维码之开源利器ZXing图文介绍(转)
2012-05-03 11:18 7798摘选:http://sjsky.iteye.com/blog/ ... -
log4j使用详解
2012-04-25 13:30 552log4j使用详解 ... -
读取条形码
2012-04-23 10:54 646(摘选: http://www.ibm.com/deve ... -
用 Java 实现断点续传 (HTTP)
2012-04-23 10:23 478断点续传的原理 其实断点续传的原理很简单,就是在 H ... -
用 Servlet 进行上载的原理和实现
2012-04-20 14:45 587用 Servlet 进行上载的原理和实现 (引用:h ... -
尽可能使用堆栈变量
2012-04-20 13:58 418如果您频繁存取变量,就需要考虑从何处存取这些变量。变量是 ... -
finally 块
2012-04-20 11:29 490finally 块必须与 try 或 ... -
mx参数
2012-04-20 11:25 516在大多数情况下,如果 ...
相关推荐
【SMSLib实现Java短信收发的功能】 SMSLib是一个开源的Java库,专门用于实现通过GSM调制解调器或SMS网关进行短信的发送和接收。它为开发者提供了便捷的API,使得无需深入理解串口通信、AT指令或者SMPP协议等底层...
SMSLIB_SMSlib CServi_java 短信收发_短信收发_短信猫"中,我们可以看到这个压缩包包含了SMSLib的特定版本——v2.1.0,以及可能的CServi服务端实现,这表明该库不仅支持单向的短信发送,还可能具备接收短信的功能。...
本篇将详细介绍如何使用SMSLIB进行Java短信收发,并结合实际例子进行说明。 首先,理解SMSLIB的核心概念是关键。SMSLIB是一个基于Java的库,它支持GSM、3G和4G调制解调器,以及连接到多个SMS网关的协议,如HTTP、...
【描述】中的关键词"java短信"表明我们要关注的是Java语言实现的短信功能。"短信猫"是硬件设备,通常是指能够模拟GSM/3G等移动通信模块的USB或串口设备,它可以让计算机通过SIM卡收发短信。"java发送短信"则强调了...
SMSLib不仅支持短信收发,还支持与一些批量短信服务提供商集成,仅限于发送消息。 要开始使用SMSLib,你需要准备以下组件: 1. SMSLib的库文件,可以从官方网站http://code.google.com/p/smslib/下载。 2. SLF4J...
java资源短信收发包 SMSLibjava资源短信收发包 SMSLib提取方式是百度网盘分享地址
使用java SMslib实现了短信猫收发短信。串口开发,串口配置工具,配合了完整的测试用例,可以使用到项目中。
【标题】"SMSLIB短信猫发送短信"是一个基于Java编程语言的Eclipse项目,它利用了SMSLIB组件来实现通过串口与短信猫设备进行通信,从而实现短信的发送功能。这个项目对于需要进行批量短信发送或者自动化短信通知的...
JAVA源码短信收发包SMSLibJAVA源码短信收发包SMSLib
SMSLib是一个开源的Java库,专门设计用于实现短信的发送和接收功能。这个库广泛应用于需要通过GSM调制解调器或者网络提供商接口进行短信通信的系统中,例如企业级的提醒服务、物联网设备的远程控制以及移动应用的...
【标题】"java源码:短信收发包 SMSLib.zip" 涉及的主要知识点是Java编程语言,以及使用Java进行短信收发的库——SMSLib。SMSLib是一个开源项目,专为Java开发者设计,提供了方便的API接口,用于实现GSM短信的发送和...
Java短信收发文档主要涉及Java编程与短信服务的集成,使用了smslib库来实现短信的发送和接收功能。smslib是一个开源的Java库,它提供了与短信猫(硬件设备,用于通过SIM卡进行短信通信)交互的能力。下面将详细阐述...
SMSLib是一个功能强大的Java库,专门用于实现短信的发送、接收和管理。它在标题中被称为"SMSLib-Java-v1.1.0.zip_SMSLIB_smslib ja_服务平台",表明这是一个针对Java开发者的资源包,版本号为v1.1.0,且可能包含了...
2. **smsserver**:smslib内建了一个smsserver组件,它可以作为一个独立的服务运行,用于监听和管理短信收发,为开发者提供了一个统一的接口来处理短信通信。 3. **多种接口支持**:smslib支持多种短信网关接口,...
smslib使用rxtx发短信需要的jar,commons-net-3.0.1.jar ,jsmpp-2.1.0.jar,log4j-1.2.16.jar,RXTXcomm.jar, slf4j-api-1.6.3.jar,slf4j-log4j12-1.6.3.jar, smslib-3.5.2.jar
在java下利用smslib的二次开发包使用短信猫进行短信收发,该资源包括smslib-3.3.0b2.jar,win32com.dll,javax.comm.properties,comm.jar,短信收发的两个示例文件以及简略的使用说明,具体的使用可以参考...
基于java的短信收发包 SMSLib.zip
【描述】这个压缩包包含了一个基于Java实现的短信收发库SMSLib的源代码。SMSLib是一个功能丰富的库,专门设计用于通过各种接口(如GSM Modems、3G调制解调器、固定电话线路以及通过HTTP或HTTPS的在线服务)发送和...
【标题】"基于Java的源码-短信收发包 SMSLib.zip" 提供的是一个用于短信收发功能的Java库,名为SMSLib。这个库旨在帮助开发者构建能够发送和接收短信的应用程序。SMSLib通常被用在需要进行批量短信发送、短信通知...