一,環(huán)境
windows 11, JDK8
二,使用資料
鏈接:https://pan.baidu.com/s/13dy6DV5RcnFn-k_Oe5b8Cw
提取碼:30uh
我的JAVA_HOME 是 D:\env\jdk\
- 放置
RXTXcomm.jar于JDK的lib下面,還有JRE的lib\ext 下面(我的是這個,D:\env\jdk\lib,D:\env\jdk\jre\lib\ext) - 放置
rxtxSerial.dll和rxtxParallel.dll于JDK的 bin下面,還有JRE的bin下面(我的是這個,D:\env\jdk\bin,D:\env\jdk\jre\bin) -
smslib-3.5.1.jar你可以導入本地倉庫 或者項目引用也行 - 串口轉(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 就行
- yml配置
sms:
id: 1 # 編號不重復便可
comPort: COM1 # 串口號
baudRate: 115200 # 波特率
manufacturer: Quectel # 廠家 (可為空)
model: # 型號(可為空)
- 配置類
@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;
}
- 工具類
@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
- 輸出是拒絕訪問的,可能是其他程序占用了 (可能用了其他工具占用了端口)
- 輸出是設備不可用,那么就是驅(qū)動有問題,要換驅(qū)動(我被坑慘了)
NoSuchPortException
- 檢測下端口是否輸入錯誤 可能是COM2 COM3