技术园地

当前位置:短信猫网站主页 > 技术园地 >

【转载】Java调用SMSLib用单口短信猫发送短信详解

发布时间:2017/02/09 点击量:620

SMSLib是Apache的一个开源项目

发短信几种方式:
1、向当地的运营商申请网关,不需要额外的设备,利用对方提供的 API调用程序发送短信,适用于大型的通信公司。 稳定,速度快,适合短信量特别大的需求,需要连接到运营商的网络中,不适合内网项目。 
2、短信猫发送短信,借助像 GSM MODEM之类的设备,通过数据线连接电脑来发送短信,这种方法比较适用于小公司及个人。 不需要联网,速度慢,简单说就是通过程序调用短信猫设备发短信,价格看发短信卡的套餐了,适合中小项目。 

短信猫发送短信设备
3、互联网短信平台,由互联网上专业做短信收发的网站代发短信数据,对网站依赖性太高,速度,价格都看选择的短信平台了,有些平台速度快,也稳定,不过价格高。 不适合内网项目。 
4、内网短信平台产品,其实和短信猫没什么区别,就是单独把短信收发功能做成了一个产品,有支持甚至几十张卡同时发的产品,速度也不慢,短信负载都做的很好了,我们公司自己也有一个类似产品,不过比较庞大,安装麻烦,这类产品通常价格也比较贵。

最终我们选择了使用短信猫发送短信,预计一天1000左右的短信量,不算大,而且内网项目,不能连接外部网络和互联网。
因为怕后期短信量增大,一张卡每天发送短信太多会被运营商认为发送垃圾短信封掉,所以开始设备选择了4核的短信猫,能放4张卡,可惜设备驱动不支持,后来又换成了普通的短信猫,串口 GSM的,下面说说java程序如何和短信猫通信发送短信。
操作短信猫需要发送AT指令(就是一套控制短信设备的标准执行,详情可以百度一下),因为短信猫是串口设备,Java需要与串口进行通讯,发送 AT指令。
既然都是标准,通常就有现成的组件,发送指令控制短信猫等功能可以使用SMSLib这个开源组件完成,SMSLib是Apache的一个开源项目。程序与串口通讯也有很多组件,SMSLib官网上提供了两种: 
JavaComm只支持32位的windows,我们是windows server 2008 64位版本,只能选择RxTx来与串口通信。最终依赖jar包有4个:
windows和串口通讯还需要依赖操作系统的功能,所以 RxTx还需要将 rxtxParallel.dll 和 rxtxSerial.dll 放到jdk目录中,官方的安装说明非常详细了。
rxtxSerial.dll ---> <JAVA_HOME>\jre\bin
     rxtxParallel.dll ---> <JAVA_HOME>\jre\bin
RXTXcomm.jar只要放到classpath下就行,不用非得放到jre\lib\ext下面
接下来就可以写代码了,代码其实也不复杂:
package com.piaohan.sms;

import org.smslib.Message.MessageEncodings;
import org.smslib.OutboundMessage;
import org.smslib.Service;
import org.smslib.modem.SerialModemGateway;

public class SMSDemo {

<span class="hljs-function"><span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">public</span> <span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">static</span> <span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">void</span> <span class="hljs-title" style="color: rgb(136, 0, 0); font-weight: bold;">main</span><span class="hljs-params">(String args[])</span> <span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">throws</span> Exception </span>{<span class="hljs-comment" style="color: rgb(136, 136, 136);">// ---------------创建串口设备,如果有多个,就创建多个--------------</span><span class="hljs-comment" style="color: rgb(136, 136, 136);">// 1、你自己连接网关的id</span><span class="hljs-comment" style="color: rgb(136, 136, 136);">// 2、com口名称,如COM1或/dev/ttyS1(根据实际情况修改)</span><span class="hljs-comment" style="color: rgb(136, 136, 136);">// 3、串口波特率,如9600(根据实际情况修改)</span><span class="hljs-comment" style="color: rgb(136, 136, 136);">// 4、开发商,如Apple</span><span class="hljs-comment" style="color: rgb(136, 136, 136);">// 5、型号,如iphone4s</span>SerialModemGateway gateway = <span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">new</span> SerialModemGateway(<span class="hljs-string" style="color: rgb(136, 0, 0);">"SMS"</span> , <span class="hljs-string" style="color: rgb(136, 0, 0);">"COM3"</span>,<span class="hljs-number" style="color: rgb(136, 0, 0);">9600</span>, <span class="hljs-string" style="color: rgb(136, 0, 0);">""</span>, <span class="hljs-string" style="color: rgb(136, 0, 0);">""</span>);gateway.setInbound( <span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">true</span>); <span class="hljs-comment" style="color: rgb(136, 136, 136);">// 设置true,表示该网关可以接收短信</span>gateway.setOutbound( <span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">true</span>); <span class="hljs-comment" style="color: rgb(136, 136, 136);">// 设置true,表示该网关可以发送短信</span><span class="hljs-comment" style="color: rgb(136, 136, 136);">// -----------------创建发送短信的服务(它是单例的)----------------</span>Service service = Service. getInstance();<span class="hljs-comment" style="color: rgb(136, 136, 136);">// ---------------------- 将设备加到服务中----------------------</span>service.addGateway(gateway);<span class="hljs-comment" style="color: rgb(136, 136, 136);">// ------------------------- 启动服务 -------------------------</span>service.startService();<span class="hljs-comment" style="color: rgb(136, 136, 136);">// ------------------------- 发送短信 -------------------------</span>OutboundMessage msg = <span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">new</span> OutboundMessage(<span class="hljs-string" style="color: rgb(136, 0, 0);">"187xxxxxxxx"</span> , <span class="hljs-string" style="color: rgb(136, 0, 0);">"Hello World"</span>);msg.setEncoding(MessageEncodings. ENCUCS2);service.sendMessage(msg);<span class="hljs-comment" style="color: rgb(136, 136, 136);">// ------------------------- 关闭服务 -------------------------</span>service.stopService();
}

}


代码不多,但是第一次弄这个时候搞了好久,碰到的问题不少,主要在于串口配置那,串口名称,波特率不知道,其实windows下可以通过“设备管理器”来查看。
也可以使用下面代码来检测端口设备:
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;

import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;

/**

  • CommTest 检测端口
    /
    public class ComTone {
    /
    * 常见端口波特率 */
    public static int bauds[] = { 9600, 19200, 57600, 115200 };

    @SuppressWarnings( “unchecked” )
    public static void main(String[] args) {

     Enumeration&lt;CommPortIdentifier&gt; portList = CommPortIdentifier. getPortIdentifiers();<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">long</span> st = System. currentTimeMillis();System. out.<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">println</span>( <span class="hljs-string" style="color: rgb(136, 0, 0);">"短信设备端口连接测试..."</span> );<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">while</span> (portList.hasMoreElements()) {CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">if</span> (portId.getPortType() == CommPortIdentifier.PORT_SERIAL ) {System. out.<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">println</span>( <span class="hljs-string" style="color: rgb(136, 0, 0);">"找到串口:"</span> + portId.getName() + <span class="hljs-string" style="color: rgb(136, 0, 0);">"\t类型:"</span>+ getPortTypeName(portId.getPortType()));<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">for</span> ( <span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">int</span> i = <span class="hljs-number" style="color: rgb(136, 0, 0);">0</span>; i &lt; bauds. length; i++) {System. out.<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">print</span>( <span class="hljs-string" style="color: rgb(136, 0, 0);">"  Trying at "</span> + bauds [i] + <span class="hljs-string" style="color: rgb(136, 0, 0);">"..."</span> );<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">try</span> {SerialPort serialPort;InputStream inStream;OutputStream outStream;<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">int</span> c;String response;serialPort = (SerialPort) portId.open(<span class="hljs-string" style="color: rgb(136, 0, 0);">"SMSLibCommTester"</span> , <span class="hljs-number" style="color: rgb(136, 0, 0);">1000</span>);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(<span class="hljs-number" style="color: rgb(136, 0, 0);">1000</span>);c = inStream.<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">read</span>();<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">while</span> (c != -<span class="hljs-number" style="color: rgb(136, 0, 0);">1</span>)c = inStream.<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">read</span>();outStream.<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">write</span>( <span class="hljs-string" style="color: rgb(136, 0, 0);">'A'</span>);outStream.<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">write</span>( <span class="hljs-string" style="color: rgb(136, 0, 0);">'T'</span>);outStream.<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">write</span>( <span class="hljs-string" style="color: rgb(136, 0, 0);">'\r'</span>);<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">try</span> {Thread. sleep(<span class="hljs-number" style="color: rgb(136, 0, 0);">1000</span>);} <span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">catch</span> (Exception e) {}response = <span class="hljs-string" style="color: rgb(136, 0, 0);">""</span>;c = inStream.<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">read</span>();<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">while</span> (c != -<span class="hljs-number" style="color: rgb(136, 0, 0);">1</span>) {response += ( <span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">char</span>) c;c = inStream.<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">read</span>();}<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">if</span> (response.indexOf( <span class="hljs-string" style="color: rgb(136, 0, 0);">"OK"</span> ) &gt;= <span class="hljs-number" style="color: rgb(136, 0, 0);">0</span>) {<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">try</span> {System. out.<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">print</span>( <span class="hljs-string" style="color: rgb(136, 0, 0);">"  获取设备信息..."</span> );outStream.<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">write</span>( <span class="hljs-string" style="color: rgb(136, 0, 0);">'A'</span>);outStream.<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">write</span>( <span class="hljs-string" style="color: rgb(136, 0, 0);">'T'</span>);outStream.<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">write</span>( <span class="hljs-string" style="color: rgb(136, 0, 0);">'+'</span>);outStream.<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">write</span>( <span class="hljs-string" style="color: rgb(136, 0, 0);">'C'</span>);outStream.<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">write</span>( <span class="hljs-string" style="color: rgb(136, 0, 0);">'G'</span>);outStream.<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">write</span>( <span class="hljs-string" style="color: rgb(136, 0, 0);">'M'</span>);outStream.<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">write</span>( <span class="hljs-string" style="color: rgb(136, 0, 0);">'M'</span>);outStream.<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">write</span>( <span class="hljs-string" style="color: rgb(136, 0, 0);">'\r'</span>);response = <span class="hljs-string" style="color: rgb(136, 0, 0);">""</span>;c = inStream.<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">read</span>();<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">while</span> (c != -<span class="hljs-number" style="color: rgb(136, 0, 0);">1</span>) {response += ( <span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">char</span>) c;c = inStream.<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">read</span>();}System. out.<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">println</span>( <span class="hljs-string" style="color: rgb(136, 0, 0);">"  发现设备: "</span>+ response.replaceAll(<span class="hljs-string" style="color: rgb(136, 0, 0);">"\\s+OK\\s+"</span> , <span class="hljs-string" style="color: rgb(136, 0, 0);">""</span>).replaceAll(<span class="hljs-string" style="color: rgb(136, 0, 0);">"\n"</span> , <span class="hljs-string" style="color: rgb(136, 0, 0);">""</span>).replaceAll(<span class="hljs-string" style="color: rgb(136, 0, 0);">"\r"</span> , <span class="hljs-string" style="color: rgb(136, 0, 0);">""</span>));} <span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">catch</span> (Exception e) {System. out.<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">println</span>( <span class="hljs-string" style="color: rgb(136, 0, 0);">"  没有发现设备!"</span> );}} <span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">else</span>System. out.<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">println</span>( <span class="hljs-string" style="color: rgb(136, 0, 0);">"  没有发现设备!"</span> );serialPort.close();} <span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">catch</span> (Exception e) {System. out.<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">println</span>( <span class="hljs-string" style="color: rgb(136, 0, 0);">"  没有发现设备!"</span> );}}System. out.<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">println</span>( <span class="hljs-string" style="color: rgb(136, 0, 0);">"找到串口: "</span> + portId.getName());}}<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">long</span> et = System. currentTimeMillis();<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">long</span> time = et - st;System. out.<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: 700;">println</span>( <span class="hljs-string" style="color: rgb(136, 0, 0);">"系统的试运行时间:"</span> + time + <span class="hljs-string" style="color: rgb(136, 0, 0);">"毫秒"</span> );
    

    }

    private static String getPortTypeName( int portType) {
    switch (portType) {
    case CommPortIdentifier. PORT_I2C :
    return “I2C” ;
    case CommPortIdentifier. PORT_PARALLEL :
    return “Parallel” ;
    case CommPortIdentifier. PORT_RAW :
    return “Raw” ;
    case CommPortIdentifier. PORT_RS485 :
    return “RS485” ;
    case CommPortIdentifier. PORT_SERIAL :
    return “Serial” ;
    default:
    return “unknown type” ;
    }
    }
    }

    运行结果如下:
    最后,有个要注意的地方,上面的代码全部是测试代码,如果真要放到项目中使用,最好自己维护一个短信发送队列,因为发1条短信大约5s左右,太多的话虽然smslib内部有个队列,但是如果一旦设备出错短信就丢了,如果多核设备,还要考虑负载均衡问题等等。

      <div class="page"><p>上一篇:<a href="/help/119.html">【转载】SMSLib开源短信猫二次开发包开发指南</a> </p><p>下一篇:<a href="/help/123.html">通过C#实现短信猫短信发送与接收</a> </p><a href="javascript:void(0);" title="返回" class="back" onclick="javascript:history.back(-1);">返回</a> </div></div></div>
</div>

Java调用SMSLib用单口短信猫发送短信详解相关推荐

  1. 超级终端调用短信猫发送短信说明

    转载文章:超级终端调用短信猫发送短信说明 短信发送测试 1.短信收发说明命令说明: 1).设置短消息中心  AT+CSCA="+8613800756500"回车(短信中心具体的号码 ...

  2. wavecom短信猫发送短信失败

    http://www.puppeter.cn/?p=106 wavecom短信猫发送短信失败 发表评论 分类:运维工具 | 标签:sms wavecom | 1,048 views 放置在机房用于监控 ...

  3. asp.Net下短信猫发送短信中的中文乱码解决

    现象:用c#编码调用短信猫发送包含中文字符的短信,但收到的总是乱码或问号(?),使用了Default, Unicode-16等多种编码方式仍然无效,而其他非中文字符都可以正常接收. 另外,在超级终端中 ...

  4. java 短信猫发送短信的方法

    http://zghbwjl.blog.163.com/blog/static/12033667220129175158806/ http://www.smscom.cn/sms_javasoft/ ...

  5. java调用js匿名函数参数,js匿名函数作为函数参数详解

    由衷的感叹,js真是烦. 学到现在,渐渐理解了什么是:语言都是通用的,没有好不好,只有擅长不擅长. 继承,多态,甚至指针,c能实现,c++,java有,javascript(和java是雷锋和雷峰塔的 ...

  6. 运筹优化学习21:Java调用Cplex实现求解Cuting Stock Porblem的列生成算法详解

    目录 1 CSP问题与模型 1.1 问题描述 1.2 模型构建 2 列生成方法理论 2.1 引子 2.2 单纯形法到列生成 2.3 subproblem 2.3.1 对偶理论 2.3.2 影子价格 2 ...

  7. c#实现wavecom短信猫发送长短信

    长短信是有规约的,协议头部分如果是0x40以下,则说明是普通短信,如果是0x40以上,则是长短信,然后在短信内容部分,有六个字节分别定义短信唯一标识以及该短信是第几条,所以长短信发送时每条实际为67个 ...

  8. Wavecom 短信猫 发送短信设置

    短信猫设置(命令为独占,当前命令没有返回写入其他命令提示ERROR) AT+CMGF 设置短信格式 AT+CMGF=0 设置短信为PDU格式 AT+CMGF=1 设置短信为text 格式(不能直接发送 ...

  9. Java调用SMSLib发送短信具体解释

          项目中须要用到发送短信功能.之前没做过这方面.找人咨询了一下.也网上查了查.发现并非非常复杂. 眼下项目已经完毕了.做个记录以备后用.程序中发送短信主要有4种方法:      1.向当地的 ...

最新文章

  1. Open3D 三维点云读取可视化、下采样、去除离群点、地面提取
  2. 学Java需要下载什么软件?都有什么作用?
  3. 非网络引用element-ui css导致图标无法正常显示的解决办法
  4. 如何用PPT编制方案 (2)PPT内容的逻辑表达
  5. py2neo 数据类型
  6. STM8学习笔记---如何在程序中确定寄存器名称
  7. Android IPC数据在内核空间中的发送过程分析
  8. shell脚本100例
  9. OpenCV中Mat,图像二维指针和CxImage类的转换
  10. python mobilenetssd android_tensorflow+ssd_mobilenet实现目标检测的训练
  11. 程序员面对分歧和难题应当具备的态度
  12. 【Unity3D插件】FancyScrollView插件分享《通用UI滑动列表》
  13. Java学习——Servlet是什么
  14. 谈谈核心网UPF和开放
  15. MacbookAir2011用U盘重装10.13.6High Sierra系统
  16. Linux下的摄相头驱动
  17. 读论文,第十五天:FingerPing: Recognizing Fine-grained Hand Poses using Active Acoustic On-body Sensing
  18. 2012 r2 万能网卡驱动_6款USB无线网卡评测
  19. Python调用高德地图API测距
  20. Python逆向进阶:Web逆向私单

热门文章

  1. THJ之内部类初探(1)
  2. 搭建LNMP,可以解析PHP文件
  3. Springboot毕设项目教务管理系统l7srg(java+VUE+Mybatis+Maven+Mysql)
  4. 大华等其他NVR接入海康IPC H.264方法
  5. 概率论与数理统计——分布律
  6. DenseNet发明者黄高:动态模型好在哪里?
  7. 滴普科技完成1亿美金B轮融资,引入多家金融业产业资本
  8. 使用java创建PDF并书写内容-怕原创删帖用做笔记
  9. ubuntu16.04 无法输入中文的问题终于解决了
  10. 任务管理器中性能突然不显示GPU了