短信猫 centos 6.3 使用 smslib 3.5.2 发短信 java.lang.InstantiationException的解决办法,附带 可以运行的程序。

1  http://www.smslib.org/ 这是 smslib主页,提供smslib的下载
2  如果你在windows上开发请参考 http://blog.csdn.net/xyang81/article/details/7584970 这篇文章,写的不错,而且还有linux的,但是,我用他提供的方法,在centos 6.3 上实验不成功。报 Comm library exception: java.lang.reflect.InvocationTargetException,如下图(注RxTx v2.1.7 R2 的下载 在附件中,需要注意的是要选择正确的 so库否则,CommTest.java检测程序会报驱动版本不匹配的错误)





3 但是运行 驱动检测程序 CommTest.java(见附件) 这个程序可以成功的检测到串口,并获得设备信息。如下图。


4  但是,使用  smslib.3.5.4 中的实例程序,刚才上图的 java.lang.reflect.InvocationTargetException这个错误。SendMessage 的代码如下
   
// SendMessage.java - Sample application.
//
// This application shows you the basic procedure for sending messages.
// You will find how to send synchronous and asynchronous messages.
//
// For asynchronous dispatch, the example application sets a callback
// notification, to see what's happened with messages.


package examples.modem;


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 SendMessage2
{
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());

/*

     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.com1", "/dev/ttyUSB3", 115200, "wavecom", "17254");
gateway.setInbound(true);   //设置true,表示该网关可以接收短信,根据需求修改  
       gateway.setOutbound(true);//设置true,表示该网关可以发送短信,根据需求修改   
     gateway.setSimPin("0000");//sim卡锁,一般默认为0000或1234


// Explicit SMSC address set is required for some modems.
// Below is for VODAFONE GREECE - be sure to set your own!
// gateway.setSmscNumber("+306942190000");
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("18612450268", "Hello from SMSLib!");
msg.setEncoding(MessageEncodings.ENCUCS2);//这句话是发中文短信必须的
Service.getInstance().sendMessage(msg);
System.out.println(msg);
// Or, send out a WAP SI message.
//OutboundWapSIMessage wapMsg = new OutboundWapSIMessage("306974000000",  new URL("http://www.smslib.org/"), "Visit SMSLib now!");
//Service.getInstance().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!");
//srv.queueMessage(msg, gateway.getGatewayId());
//msg = new OutboundMessage("308888888888", "Wrong number!");
//srv.queueMessage(msg, gateway.getGatewayId());
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[])
{
SendMessage2 app = new SendMessage2();
try
{
app.doIt();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

4  使用 maven 下载 smslib的3.5.4的源码(在附件中提供下载),查看报错的位置是 SerialModemDriver.java 和 CommTest.java进行对比,发现 CommTest.java用 的 gnu.io.*包中的类,而 SerialModemDriver使用的org.smslib.helper.*。而且测试发现,如果把 CommTest.java中的类改变为 org.smslib.helper.*相应的类,确实再也检测不到端口和驱动了。


5 好了找到问题的原因,这就好办了,动手 把  SerialModemDriver 中的 相应的类替换成 gnu.io.*的类(替换后的 smslib.3.5.4的源码和jar报在下面的附件中),然后运行,出现下图。果然不报错了(说起来很简单,过程很痛苦,实验了多次才成功的,哎),但是还是有问题,发送短信失败了,但是这已经前进了一大步。


6 接着查找上图的错误原因,原来是,由于下面的一段代码。(估计是由于国外的短信服务中心和国内的不相同的缘故),注释掉就可以成功运行了。
//gateway.setSmscNumber("+306942190000");


7 贴一张运行成功的图,共勉一下。





8 最后提供一下 所有包和程序下载的地方(遇到No response from device的错误参考 http://blog.csdn.net/xyang81/article/details/7492161)

http://download.csdn.net/detail/changdejie/6963977


9  注意 centos 下 /var/lock/目录 必须存在,否则 CommTest程序,检测不到端口,直接退出。









  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值