当前位置: 首页 > 工具软件 > SMSLib > 使用案例 >

短信猫发短信,不能关闭服务,报端口占用,javax.comm.PortInUseException: Port currently owned by org.smslib等问题

简烨烁
2023-12-01

短信猫发短信调用串口发短信时,第一条可以正常发送,第二条的时候就报错了!大致意思是端口被占用,没有可用或找不到串口。

分析:其主要原因是,短信猫接上电脑即建立了连接,执行service.startService()相当于保持一个长连接,不能用程序断开!最后把srv设为静态属性,每次点击按钮只调用 service.sendMessage(msg)方法,就实现连续发送短信了。

没有改造之前的代码:

package com.amc.sggk.toSendSMS;



import javax.jws.WebService;
import javax.xml.ws.Endpoint;

import org.smslib.AGateway;
import org.smslib.IOutboundMessageNotification;
import org.smslib.Library;
import org.smslib.Message.MessageEncodings;
import org.smslib.OutboundMessage;
import org.smslib.Service;
import org.smslib.modem.SerialModemGateway;


@WebService
public class SendMessage
{
	public String doIt( String phone , String center) 
	{	
		
		try {
			OutboundNotification outboundNotification = new OutboundNotification();
			System.out.println("开启服务");
			System.out.println(Library.getLibraryDescription());
			System.out.println("Version: " + Library.getLibraryVersion());
			/*
				modem.com1:网关ID(即短信猫端口编号)
				COM4:串口名称(在window中以COMXX表示端口名称,在linux,unix平台下以ttyS0-N或ttyUSB0-N表示端口名称),通过端口检测程序得到可用的端口
				115200:串口每秒发送数据的bit位数,必须设置正确才可以正常发送短信,可通过程序进行检测。常用的有115200、9600
				Huawei:短信猫生产厂商,不同的短信猫生产厂商smslib所封装的AT指令接口会不一致,必须设置正确.常见的有Huawei、wavecom等厂商
				最后一个参数表示设备的型号,可选
			 */
			SerialModemGateway gateway = new SerialModemGateway("modem.com3", "COM3", 115200, "Quectel", "EC20");
			gateway.setInbound(true);	//设置true,表示该网关可以接收短信,根据需求修改
			gateway.setOutbound(true);//设置true,表示该网关可以发送短信,根据需求修改
			gateway.setSimPin("0000");//sim卡锁,一般默认为0000或1234
			gateway.setSmscNumber("+8613010180500");//短信服务中心号码
			Service.getInstance().setOutboundMessageNotification(outboundNotification);	//发送短信成功后的回调函方法
			Service.getInstance().addGateway(gateway);	//将网关添加到短信猫服务中
			Service.getInstance().S.SERIAL_POLLING = true; 
			System.out.println("启动服务,进入短信发送就绪状态");
			Service.getInstance().startService();	//启动服务,进入短信发送就绪状态
			
			OutboundMessage msg = new OutboundMessage(phone, center);	//参数1:手机号码 参数2:短信内容
			msg.setEncoding(MessageEncodings.ENCUCS2);//这句话是发中文短信必须的
			Service.getInstance().sendMessage(msg);	//执行发送短信
			
			/*System.in.read();*/
			Service.getInstance().stopService();
			System.out.println("关闭服务");
			
			return "发送成功!";
			
		} catch (Exception e) {
			e.printStackTrace();
			return "短信模块调用失败!";
		}
		
	}

	/*
	 短信发送成功后,调用该接口。并将发送短信的网关和短信内容对象传给process接口
	*/
	public class OutboundNotification implements IOutboundMessageNotification
	{
		public void process(AGateway gateway, OutboundMessage msg)
		{
			System.out.println("短信发送成功!");
			System.out.println(msg);
		}
	}

	/*public static void main(String args[])
	{
		SendMessage app = new SendMessage();
		try
		{
			app.doIt("15652194735","ceshi");
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}*/
	
	
	public static void main(String[] args) {
		/**
		 * 参数1:服务的发布地址(http://192.168.43.173/SendMsnService)
		 * 参数2:服务的实现者
		 */
		Endpoint.publish("http://192.168.4.52:8080/toSendMsn", new SendMessage());

	}
	
	
	
	
}

改造好的代码:

package com.amc.sggk.toSendSMS;



import java.io.IOException;

import javax.jws.WebService;
import javax.xml.ws.Endpoint;

import org.smslib.AGateway;
import org.smslib.GatewayException;
import org.smslib.IOutboundMessageNotification;
import org.smslib.Library;
import org.smslib.Message.MessageEncodings;
import org.smslib.OutboundMessage;
import org.smslib.Service;
import org.smslib.TimeoutException;
import org.smslib.modem.SerialModemGateway;


@WebService
public class SendMessage
{
	public static Service service;
	static {
		try {
			init();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void init() throws Exception {
		//配置信息只加载一遍
		OutboundNotification outboundNotification = new OutboundNotification();
		SerialModemGateway gateway = new SerialModemGateway("modem.com3", "COM3", 115200, "Quectel", "EC20");
		gateway.setInbound(true);	//设置true,表示该网关可以接收短信,根据需求修改
		gateway.setOutbound(true);//设置true,表示该网关可以发送短信,根据需求修改
		gateway.setSimPin("0000");//sim卡锁,一般默认为0000或1234
		gateway.setSmscNumber("+8613010180500");//短信服务中心号码
		service.getInstance().setOutboundMessageNotification(outboundNotification);	//发送短信成功后的回调函方法
		service.getInstance().addGateway(gateway);	//将网关添加到短信猫服务中
		service.getInstance().S.SERIAL_POLLING = true; 
		System.out.println("启动服务,进入短信发送就绪状态");
		service.getInstance().startService();	//启动服务,进入短信发送就绪状态
		
	}
	
	//调用发短信
	public void doIt( String phone , String center) throws Exception {
		OutboundMessage msg = new OutboundMessage(phone, center);	//参数1:手机号码 参数2:短信内容
		msg.setEncoding(MessageEncodings.ENCUCS2);//这句话是发中文短信必须的
		service.getInstance().sendMessage(msg);	//执行发送短信
		System.out.println("发送成功");
		//service.getInstance().stopService();
	}
	
	/*public static void main(String[] args) {
		try {
			doIt("15652194735","123");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}*/
	

	/*
	 短信发送成功后,调用该接口。并将发送短信的网关和短信内容对象传给process接口
	*/
	public static class OutboundNotification implements IOutboundMessageNotification
	{
		public void process(AGateway gateway, OutboundMessage msg)
		{
			System.out.println("短信发送成功!");
			System.out.println(msg);
		}
	}

	
	
	
	public static void main(String[] args) {
		/**
		 * 参数1:服务的发布地址(http://192.168.43.173/SendMsnService)
		 * 参数2:服务的实现者
		 */
		Endpoint.publish("http://192.168.4.52:8080/toSendMsn", new SendMessage());

	}
	
	
	
	
}

参考:https://blog.csdn.net/kakarottoz/article/details/38011001

 类似资料: