SMSLib中的javax.comm.PortInUseException

时间:2012-02-09 07:37:12

标签: smslib

我正在尝试使用SMSLib从我的电脑向手机发送短信。我正在使用我的诺基亚5130 GSM手机发送消息,但它无法正常工作。继承人我正在使用的代码。

package sms;
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;

public class SendMessage
{
public void doIt() throws Exception {
 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.com12", "COM12", 115200, "Nokia", "Nokia 5130 XPressMusic USB Modem");


gateway.setInbound(true);
gateway.setOutbound(true);
gateway.setSimPin("0000");

gateway.setSmscNumber("+919886005444");
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();
// Send a message synchronously.
OutboundMessage msg = new OutboundMessage("+917829903913", "Sample msg");
msg.setEncoding(MessageEncodings.ENC7BIT);
msg.setSrcPort(0);
msg.setDstPort(66500);
Service.getInstance().sendMessage(msg);

System.out.println(msg);

System.out.println("Now Sleeping - Hit <enter> to terminate.");
System.in.read();
Service.getInstance().stopService();
}

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);
 }
}

public static void main(String args[]) {
SendMessage app = new SendMessage();
try {
app.doIt();
} catch (Exception e) {
e.printStackTrace();
}
}
}

这是我收到的错误消息。请帮助,因为我对这些知之甚少。

run:
Example: Send message from a serial gsm modem.
SMSLib: A Java API library for sending and receiving SMS via a GSM modem or other supported   gateways.
This software is distributed under the terms of the Apache v2.0 License.
Web Site: http://smslib.org
Version: 3.5.1
log4j:WARN No appenders could be found for logger (smslib).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
org.smslib.GatewayException: Comm library exception: java.lang.RuntimeException: javax.comm.PortInUseException: Port currently owned by Unknown Windows Application
    at org.smslib.modem.SerialModemDriver.connectPort(SerialModemDriver.java:102)
    at org.smslib.modem.AModemDriver.connect(AModemDriver.java:114)
    at org.smslib.modem.ModemGateway.startGateway(ModemGateway.java:189)
    at org.smslib.Service$1Starter.run(Service.java:276)
 BUILD SUCCESSFUL (total time: 6 seconds)

3 个答案:

答案 0 :(得分:1)

确保没有其他应用正在监听COM12。发生了javax.comm.PortInUseException:因为您的代码尝试注册COM12,但该代码不可用。

将所有必要的jar和amd dll文件复制到正确的位置后,这应该可行。你还需要一个32位的JDK,所以你可能不得不下载一个32位的JDK,如果你没有其他的你将获得java.lang.UnsatisfiedLinkError:

我运行此代码并且可以正常运行。还记得将setSmscNumber设置为您的SIM提供商,看起来您仍然拥有默认设置(示例一)

干杯 PB

答案 1 :(得分:1)

有一个与您的手机相关的应用程序(如诺基亚套件)可在PC上运行,并在将手机连接到PC时与手机进行连接,请确保转关闭所有此类应用程序并使您的连接只是物理连接。

答案 2 :(得分:0)

安装32位java版本后,它对我有用。还要确保使用正确的端口。 com11在我的情况下。

enter image description here