4

我正在使用 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: 通讯库异常: java.lang.RuntimeException: javax.comm.PortInUseException: 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)

问题出在哪里 ?

4

2 回答 2

0

这个对我有用。利用

Service.getInstance().sendMessages(myList, gateway.getGatewayId()); 

函数而不是使用 sendmessage mylist.add(msg);

mylist是包含出站消息的列表

于 2013-07-03T09:17:34.897 回答
0

看起来您没有正确关闭 SmsLib,因为异常似乎暗示与串行端口的连接保持打开状态。

于 2011-09-06T19:07:20.933 回答