我正在使用SMSLib使用三星gsm调制解调器发送短信。
我创建了一个单独的线程,该线程每20秒从服务器获取一次消息
如果收到消息,它将调用SendMessage ..这是SendMessage的代码
public class SendMessage {
public boolean doIt(String num, String umsg) {
try {
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());
SerialModemGateway gateway = new SerialModemGateway("modem.com10","COM10", 115200, "Samsung", "");
gateway.setInbound(true);
gateway.setOutbound(true);
gateway.setSimPin("0000");
gateway.setSmscNumber("+919826012311");
Service.getInstance().setOutboundMessageNotification(outboundNotification);
Service.getInstance().addGateway(gateway);
Service.getInstance().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()+ " dBm");
System.out.println(" Battery Level: " + gateway.getBatteryLevel()+ "%");
System.out.println();
OutboundMessage msg = new OutboundMessage(num, umsg);
Service.getInstance().sendMessage(msg);
System.out.println(msg);
Service.getInstance().stopService();
gateway.stopGateway();
return true;
} catch (GatewayException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TimeoutException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SMSLibException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
Service.getInstance().stopService();
} catch (TimeoutException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (GatewayException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SMSLibException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
public class OutboundNotification implements IOutboundMessageNotification {
public void process(AGateway gateway, OutboundMessage msg) {
System.out.println("Outbound handler called from Gateway: "
+ gateway.getGatewayId());
System.out.println(msg);
}
}
}
这段代码在我第一次调用doIt时效果很好。但是如果我的线程收到更多短信
从服务器,然后如果我调用doIt,它将引发异常
org.smslib.GatewayException:Comm库异常:java.lang.RuntimeException:javax.comm.PortInUseException:org.smslib当前拥有的端口
在org.smslib.modem.SerialModemDriver.connectPort(SerialModemDriver.java:102)
在org.smslib.modem.AModemDriver.connect(AModemDriver.java:114)
在org.smslib.modem.ModemGateway.startGateway(ModemGateway.java:189)
在org.smslib.Service $ 1Starter.run(Service.java:275)
问题出在哪里 ?
最佳答案
看起来您没有正确关闭SmsLib,因为异常似乎暗示与串行端口的连接保持打开状态。