Javaは、SMSテキストメッセージの猫コメントを送信SMSLib単一ポートで呼び出します

テックコーナー

[予約] JavaはSMSLibは、シングルポートの猫コメントを使用して、SMSテキストメッセージを送信呼び出します

発行日:2017年2月9日の ヒット数: 620

SMSLib Apacheはオープンソースプロジェクトである

いくつかの方法でテキストメッセージを送信するために:
1、ローカルキャリアゲートウェイに適用される大規模な通信会社の提供するAPI呼び出し他の手順を使用してテキストメッセージを送信するために追加の機器を必要としません。テキストメッセージの、特に大量のニーズに適した安定性、速度は、ネットワークプロジェクトに収まらない、事業者のネットワークに接続する必要があります。 
2、SMSの猫はSMS送信、コンピュータに接続されるデータ線を介してテキストメッセージを送信する、などの手段によって、GSMモデムデバイスを、この方法では、中小企業や個人のために適しています。あなたは、それは単にによって呼び出され、遅い、ネットワークを必要としないテキストメッセージの猫の機器の小規模なプロジェクトに適したパッケージを、見るためにカードの価格をテキストメッセージ、テキストメッセージ。 


SMSの猫送信テキストメッセージデバイス
メッセージテキストメッセージデータのインターネットサイトの生成に専門家によって作られた3、インターネットメッセージングプラットフォーム、サイト上の依存が高すぎる、スピード、価格選択肢のSMSプラットフォーム、いくつかの高速のためのプラットフォーム、安定しましたしかし、価格が高いです。 ネットワークプロジェクトに収まりません。 
4、ネットワークのメッセージング・プラットフォーム、SMSの猫と実際には違いはありません、つまり、製品を作るために、テキストメッセージング機能を分離し、カードの数十が同時にサポートであってもヘアケア製品、速度が遅いではない、SMSの負荷が非常にやっていますまあ、我々は、同社はまた、同様の製品を持っている所有し、比較的大規模な、面倒なインストールは、これらの製品は、通常より高価な価格です。

最終的に、我々はテキストメッセージを送信するSMSの猫を使用することを選択した、SMSは約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” ;
    }
    }
    }

    运行结果如下:
    最後に、上記のコードは、あなたが本当にプロジェクトにしたい場合は、すべてのコードをテストすることであることに注意する場所があり、それは、内部smslibものの5Sについての周りにメッセージを送信するため、メッセージを送信するために、独自のキューを維持するためにあまりにも多くの言葉が最適ですそこにキューがあるが、それはように、マルチコアデバイスならば、失われた、だけでなく、負荷バランスの問題を検討している場合、デバイスのエラーメッセージに一度。



      <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>

おすすめ

転載: blog.csdn.net/qq_37358860/article/details/93857873