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

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

分析:其主要原因是,短信猫接上电脑即建立了连接,执行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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在MyEclipse里 1. slf4j-api-1.5.2.jar slf4j-api-1.5.2-sources.jar slf4j-nop-1.5.2.jar comm.jar smsserver-3.4.1.jar smslib-3.4.1.jar 将上述6个jar包拷贝到lib下 2. 在Windows环境下使用SMSLib编程的时候,我们需要做一下comm的配置: 1. 将win32com.dll放置在%JAVA_HOME%/jre/bin下 2. 将comm.jar放置在%JAVA_HOME%/jre/lib/ext下 3. 将javax.comm.properties放置在%JAVA_HOME%/jar/lib下 再试试SMSLib自带的examples,看看效果。 3. pci接口安装drive 程序测试用例: package examples.modem; import org.smslib.IOutboundMessageNotification; import org.smslib.Library; import org.smslib.OutboundMessage; import org.smslib.Service; import org.smslib.modem.SerialModemGateway; public class SendMessage { public void doIt() throws Exception { Service srv; OutboundMessage msg; OutboundNotification outboundNotification = new OutboundNotification(); System.out.println("Example: Send message from a serial gsm modem."); System.out.println(Library.getLibraryDescription()); System.out.println("Version: " + Library.getLibraryVersion()); srv = new Service(); SerialModemGateway gateway = new SerialModemGateway("modem.com1", "COM1", 57600, "Nokia", "6310i"); gateway.setInbound(true); gateway.setOutbound(true); gateway.setSimPin("0000"); srv.setOutboundNotification(outboundNotification); srv.addGateway(gateway); srv.startService(); System.out.println(); System.out.println("Modem Information:"); System.out.println(" Manufacturer: " + gateway.getManufacturer()); System.out.println(" Model: " + gateway.getModel()); System.out.println(" Serial No: " + gateway.getSerialNo()); System.out.println(" SIM IMSI: " + gateway.getImsi()); System.out.println(" Signal Level: " + gateway.getSignalLevel() + "%"); System.out.println(" Battery Level: " + gateway.getBatteryLevel() + "%"); System.out.println(); // Send a message synchronously. msg = new OutboundMessage("+306948494037", "Hello from SMSLib!"); srv.sendMessage(msg); System.out.println(msg); // Or, send out a WAP SI message. //OutboundWapSIMessage wapMsg = new OutboundWapSIMessage("+306948494037", new URL("https://mail.google.com/"), "Visit GMail now!"); //srv.sendMessage(wapMsg); //System.out.println(wapMsg); // You can also queue some asynchronous messages to see how the callbacks // are called... //msg = new OutboundMessage("+309999999999", "Wrong number!"); //msg.setPriority(OutboundMessage.Priorities.LOW); //srv.queueMessage(msg, gateway.getGatewayId()); //msg = new OutboundMessage("+308888888888", "Wrong number!"); //msg.setPriority(OutboundMessage.Priorities.HIGH); //srv.queueMessage(msg, gateway.getGatewayId()); System.out.println("Now Sleeping - Hit to terminate."); System.in.read(); srv.stopService(); } public class OutboundNotification implements IOutboundMessageNotification { public void process(String gatewayId, OutboundMessage msg) { System.out.println("Outbound handler called from Gateway: " + gatewayId); System.out.println(msg); } } public static void main(String args[]) { SendMessage app = new SendMessage(); try { app.doIt(); } catch (Exception e) { e.printStackTrace(); } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值