我正在使用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)
问题出在哪里?
答案 0 :(得分:0)
看起来你没有正确关闭SmsLib,因为异常似乎暗示与串口的连接是打开的。
答案 1 :(得分:0)
它对我有用。使用
Service.getInstance().sendMessages(myList, gateway.getGatewayId());
而不是使用sendmessage mylist.add(msg);
mylist
是包含outboung消息的列表