JAVA 對接 短信貓

一,環(huán)境

windows 11, JDK8

二,使用資料

鏈接:https://pan.baidu.com/s/13dy6DV5RcnFn-k_Oe5b8Cw
提取碼:30uh

我的JAVA_HOME 是 D:\env\jdk\

  1. 放置RXTXcomm.jar 于JDK的lib下面,還有JRE的lib\ext 下面(我的是這個,D:\env\jdk\lib,D:\env\jdk\jre\lib\ext
  2. 放置 rxtxSerial.dllrxtxParallel.dll 于JDK的 bin下面,還有JRE的bin下面(我的是這個,D:\env\jdk\bin ,D:\env\jdk\jre\bin
  3. smslib-3.5.1.jar 你可以導入本地倉庫 或者項目引用也行
  4. 串口轉(zhuǎn)usb 驅(qū)動(這個特別注意,如果你安裝最新的,會一直報端口占用問題,我被坑的懷疑人生,搞個舊版本的就行),把 CH341S64.SYS 放置 C:\Windows\System32 下面就行了,如果之前有安裝過的替換掉。

三,maven 依賴

        <dependency>
            <groupId>org.smslib</groupId>
            <artifactId>smslib</artifactId>
            <version>3.5.1</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

第一個依賴在資料內(nèi)部,自己導入本地倉庫

四,使用直接調(diào)用 sendSMS 就行

  1. yml配置
sms:
  id: 1 # 編號不重復便可
  comPort: COM1 # 串口號
  baudRate: 115200 # 波特率
  manufacturer: Quectel # 廠家 (可為空)
  model: # 型號(可為空)
  1. 配置類
@Configuration
@ConfigurationProperties("sms")
@Data
public class SmsConfig {
    //編號
    private String id;

    //串口
    private String comPort;

    //波特率
    private String baudRate;

    //廠家
    private String manufacturer;

    //型號
    private String model;

    //sim卡的pin碼
    private String simPin;
}
  1. 工具類
@Slf4j
@Component
public class SMSUtil {


    static SerialModemGateway gateway;

    @Autowired
    public SMSUtil(SmsConfig smsConfig) {
        gateway = new SerialModemGateway(smsConfig.getId(),
                smsConfig.getComPort(),
                Integer.valueOf(smsConfig.getBaudRate()),
                smsConfig.getManufacturer(),
                smsConfig.getModel());
        gateway.setInbound(true);
        gateway.setOutbound(true);
        gateway.setSimPin(smsConfig.getSimPin());
    }
    public static void sendSMS(String mobile, String content) throws IOException, InterruptedException, SMSLibException {
        log.info("發(fā)送短信:" + mobile + ",內(nèi)容:" + content);
        Service.getInstance().addGateway(gateway);
        Service.getInstance().startService();
        OutboundMessage outboundMessage =
                new OutboundMessage(mobile, content);
        outboundMessage.setEncoding(Message.MessageEncodings.ENCUCS2);
//        outboundMessage.setEncoding(Message.MessageEncodings.ENCCUSTOM);
//        outboundMessage.setEncoding(Message.MessageEncodings.ENC8BIT);
        Service.getInstance()
                .sendMessage(outboundMessage);
        Service.getInstance().stopService();
        Service.getInstance().removeGateway(gateway);
    }
}

五,可能出現(xiàn)的異常問題

PortInUseException

  1. 輸出是拒絕訪問的,可能是其他程序占用了 (可能用了其他工具占用了端口)
  2. 輸出是設備不可用,那么就是驅(qū)動有問題,要換驅(qū)動(我被坑慘了)

NoSuchPortException

  1. 檢測下端口是否輸入錯誤 可能是COM2 COM3
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容