|
本文转自网络,经测试可以解决部分SMSLIB二次开发包出现的一些问题,大家可以参考进行解决,最后的长链接例子可以解决USB口短信收发设备诶只能发送1次成功短信的问题
最近公司在网上购置了一款短信设备,要实现给客户发送短信的功能,厂家附带的开发包是smslib的java二次开发包,并附带了测试文件。
按照厂家提供的文档完成如下步骤。:
具体的操作步骤如下:
1、把smslib-3.5.0.jar、comm.jar与log4j-1.2.11.jar,放入到工程的lib中;
2、把javax.comm.properties放到%JAVA_HOME%/jre/lib下;
3、把win32com.dll放到%JAVA_HOME%/jre/bin下;
4、 把comm.jar放到%JAVA_HOME%/jre/lib/ext下
短信收发设备安装好后,然后按照厂家给的如下发送短信例子程序进行测试,
- import org.smslib.IOutboundMessageNotification;
- import org.smslib.Outbou、ndMessage;
- import org.smslib.Service;
- import org.smslib.Message.MessageEncodings;
- import org.smslib.modem.SerialModemGateway;
- public class SendMessage {
- public class OutboundNotification implements IOutboundMessageNotification {
- public void process(String gatewayId, OutboundMessage msg) {
- System.out.println("Outbound handler called from Gateway: "
- + gatewayId);
- System.out.println(msg);
- }
- }
- @SuppressWarnings("deprecation")
- public void sendSMS(String mobilePhones, String content) {
- Service srv;
- OutboundMessage msg;
- OutboundNotification outboundNotification = new OutboundNotification();
- srv = new Service();
- SerialModemGateway gateway = new SerialModemGateway("modem.com3",
- "COM3", 9600, "wavecom", ""); //设置端口与波特率
- gateway.setInbound(true);
- gateway.setOutbound(true);
- gateway.setSimPin("0000");
- gateway.setOutboundNotification(outboundNotification);
- srv.addGateway(gateway);
- System.out.println("初始化成功,准备开启服务");
- try {
- srv.startService();
- System.out.println("服务启动成功");
- String[] phones = mobilePhones.split(",");
- for (int i = 0; i < phones.length; i++) {
- msg = new OutboundMessage(phones[i], content);
- msg.setEncoding(MessageEncodings.ENCUCS2); // 中文
- srv.sendMessage(msg);
- }
- srv.stopService();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- SendMessage sendMessage = new SendMessage();
- sendMessage.sendSMS("您要发送的手机号", "您要发送的内容!");
- }
- }
复制代码
但出现了网上如下的异常,网上也能搜到一些解答。
- org.smslib.TimeoutException: No response from device.
- at org.smslib.modem.AModemDriver$CharQueue.get(AModemDriver.java:514)
- at org.smslib.modem.AModemDriver.getResponse(AModemDriver.java:306)
- at org.smslib.modem.athandler.ATHandler.getSimStatus(ATHandler.java:130)
- at org.smslib.modem.AModemDriver.connect(AModemDriver.java:131)
- at org.smslib.modem.ModemGateway.startGateway(ModemGateway.java:158)
- at org.smslib.Service$1Starter.run(Service.java:239)
复制代码
按网上的方法都解决不了,于是看了下厂商提供的另一个文档,进入超级终端软件(超级终端在windows xp下是自带的,win7以上好像没有,我是找厂商要的,网上也可以下载吧),按照设备管理器显示短信收发设备连接端口的COM口进去后,进行如下的初始化后,并测试发送短信。
图中指令说明如下:at+csq是测试信号的。AT&F,是将MODEM恢复到出厂默认状态;AT+CMGF=1,是将MODEM设置为TEXT格式,即发送英文格式。AT+CMGS=13713582925回车,设置接收号码(输入号码回车后自动换行后产生“>”,“>”的后面可以输入短信内容),how are you,为发送短信的内容。注意:输入内容后按Ctrl+z,确认发送,其中不含“+”号。返回OK,发送成功。
然后记住一定要断开超级终端的连接,不然然后用程序测会出现端口占用的问题。
- org.smslib.GatewayException: Comm library exception: java.lang.reflect.InvocationTargetException
- at org.smslib.modem.SerialModemDriver.connectPort(SerialModemDriver.java:93)
- at org.smslib.modem.AModemDriver.connect(AModemDriver.java:106)
- at org.smslib.modem.ModemGateway.startGateway(ModemGateway.java:111)
- at org.smslib.Service$1Starter.run(Service.java:227)
复制代码
再次用上述程序测试就不会出现问题.可以正常发送短信。
但是我们是需要连续发送短信的,结果出现了发送成功了一条,但发第二条就报错,出现了如下异常
- org.smslib.GatewayException: Comm library exception: java.lang.RuntimeException: javax.comm.PortInUseException: Port currently owned by org.smslib
- at org.smslib.modem.SerialModemDriver.connectPort(SerialModemDriver.java:102)
- at org.smslib.modem.AModemDriver.connect(AModemDriver.java:114)
- at org.smslib.modem.ModemGateway.startGateway(ModemGateway.java:189)
- at org.smslib.Service$1Starter.run(Service.java:276)
复制代码
参照网上说法,加上在srv.stopService();后面加一句srv.removeGateway(gateway);发现还是不行,出现了起初的异常,
- <p>org.smslib.GatewayException: Comm library exception: java.lang.reflect.InvocationTargetException</p><p> at org.smslib.modem.SerialModemDriver.connectPort(SerialModemDriver.java:93)</p><p> at org.smslib.modem.AModemDriver.connect(AModemDriver.java:106)</p><p> at org.smslib.modem.ModemGateway.startGateway(ModemGateway.java:111)</p><p></p><p> at org.smslib.Service$1Starter.run(Service.java:227)</p>
复制代码
于是按照网上一些说法,最终是用开启服务后不关端口,一直连续发送,并采用单例模式,最终代码如下,谨供参考。
- package com.hy.pmmm.common;
- import java.io.IOException;
- import java.util.Arrays;
- import org.apache.log4j.Logger;
- import org.smslib.AGateway;
- import org.smslib.GatewayException;
- import org.smslib.IOutboundMessageNotification;
- import org.smslib.Message.MessageEncodings;
- import org.smslib.OutboundMessage;
- import org.smslib.SMSLibException;
- import org.smslib.Service;
- import org.smslib.TimeoutException;
- import org.smslib.modem.SerialModemGateway;
- /**
- * @Project : pmmm
- * @ClassName: MessageUtil
- * @Description: TODO(发送短信帮助类)
- * @author cp
- * @date 2014年11月21日 下午2:28:39
- * @Copyright : Copyright (c) 2014 Wuhan Hongyi Infomation Co., Ltd.
- * @version V1.0
- *
- */
- public class MessageUtil {
- private static Logger logger = Logger
- .getLogger("com/hy/pmmm/common/MessageUtil");
- private static MessageUtil instance = new MessageUtil();
- /**
- * 启动的发送短信Service,设为静态变量,打开服务后就不关闭,实现连续发短信
- */
- private static Service srv;
- static {
- OutboundNotification outboundNotification = new OutboundNotification();
- srv = Service.getInstance();
- String port = "COM4";
- SerialModemGateway gateway = new SerialModemGateway("modem." + port,
- port, 9600, "Siemens", "MC35i"); // 设置端口与波特率
- gateway.setInbound(true);
- gateway.setOutbound(true);
- gateway.setSimPin("0000");
- Service.getInstance().setOutboundMessageNotification(
- outboundNotification);
- try {
- srv.addGateway(gateway);
- logger.info("初始化成功,准备开启服务");
- srv.startService();
- logger.info("服务启动成功");
- } catch (GatewayException e) {
- e.printStackTrace();
- } catch (TimeoutException e) {
- e.printStackTrace();
- } catch (SMSLibException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- public static class OutboundNotification implements
- IOutboundMessageNotification {
- public void process(AGateway gateway, OutboundMessage msg) {
- System.out.println("Outbound handler called from Gateway: "
- + gateway.getGatewayId());
- System.out.println(msg);
- }
- }
- private MessageUtil() {
- }
- /**
- * 给短信号码发送相应内容
- *
- * @param phones
- * 发送短信号码数组
- * @param content
- * 发送内容
- * @return
- */
- public static void sendMessage(String[] phones, String message)
- throws Exception {
- instance.sendSmsInfo(phones, message);
- logger.info("给" + Arrays.toString(phones) + "发送短信,内容为[" + message
- + "]");
- }
- /**
- * 给特定短信号码数组发送短信
- *
- * @param phones
- * 短信号码数组
- * @param content
- * 发送短信内容
- */
- private void sendSmsInfo(String[] phones, String content) {
- try {
- OutboundMessage msg;
- boolean isSendSuc;
- for (int i = 0; i < phones.length; i++) {
- msg = new OutboundMessage(phones[i], content);
- msg.setEncoding(MessageEncodings.ENCUCS2); // 中文
- isSendSuc = srv.sendMessage(msg);
- if (isSendSuc) {
- logger.info("send " + msg + "success!");
- } else {
- logger.info("send " + msg + "fail!");
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
复制代码
注意其他地方是采用 MessageUtil.sendMessage(phones, content);两个参数phones是发送的手机号数组,content是发送短信内容,
srv.sendMessage(msg)是返回boolean型的,如果发送不成功是不会抛出异常的,只会返回false,这里必须是通过判定这个方法返回true才是发送成功。
本文转自:短信收发设备smsLib for java二次开发系列问题解决探讨
|
|