java发送短信 wavecom

今天网上淘了wavecom短信猫快递回来了,急着测试。遇到问题,分享下

首先先换上sim进去,记得先不要通电,会损坏sim卡,然后安装短信猫驱动,我的是USB口,wavecom 2c2/2c。USB芯片方案是pl2303,驱动下载地址:

安装完毕后,插上短信猫,打开设备管理器。如图

java发送短信 wavecom_第1张图片
com1即为短信猫端口,如要修改端口号,则在属性,高级里修改端口号。

新建工程。

配置系统环境变量 %JDK_HOME%,然后拷贝所需的文件至

%JDK_HOME%\jre\bin\win32com.dll

%JDK_HOME%\jre\lib\javax.comm.properties

%JDK_HOME%\jre\lib\comm.jar

JAVA_HOME是jdk的路径,而非jre,

把smslib-3.3.0b2.jar和comm.jar,放入工程lib中

代码:

test.java,测试端口是否检测到

package com.sms;

import java.util.Enumeration;

import org.smslib.helper.CommPortIdentifier;

public class TEST {
public static void main(String [] args){
Enumeration en = CommPortIdentifier.getPortIdentifiers();
CommPortIdentifier portId;
while (en.hasMoreElements())
{
portId = (CommPortIdentifier) en.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
System.out.println(portId.getName());
}
}

}

如果检测到,即可看到控制台打印出的,如果没有,需要继续调试配置

代码2:

package com.sms;

import org.smslib.IOutboundMessageNotification;
import org.smslib.Library;
import org.smslib.OutboundMessage;
import org.smslib.Service;
import org.smslib.Message.MessageEncodings;
import org.smslib.modem.SerialModemGateway;

public class SendMessage
{
public void doIt() throws Exception
{
Service srv;
OutboundMessage msg;
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());
srv = new Service();

SerialModemGateway gateway = new SerialModemGateway("modem.com1", "COM1", 9600, "wavecom", "11258");//115200是波特率,一般为9600。可以通过超级终端测试出来
gateway.setInbound(true);
gateway.setOutbound(true);
gateway.setSimPin("0000");
gateway.setOutboundNotification(outboundNotification);
srv.addGateway(gateway);
srv.startService();
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() + "%");
System.out.println(" Battery Level: " + gateway.getBatteryLevel() + "%");
System.out.println();
// Send a message synchronously.

msg = new OutboundMessage("138******", "哈哈哈哈哈哈");
msg.setEncoding(MessageEncodings.ENCUCS2);//这句话是发中文短信必须的
srv.sendMessage(msg);
System.out.println(msg);
System.out.println("Now Sleeping - Hit <enter> to terminate.");
srv.stopService();
}

public class OutboundNotification implements IOutboundMessageNotification
{
public void process(String gatewayId, OutboundMessage msg)
{
System.out.println("Outbound handler called from Gateway: " + gatewayId);
System.out.println(msg);
}
}

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

运行main,即可发送短信,如果没有配置好之前的几个文件

就会出现报错,如:

org.smslib.GatewayException: Comm library exception: java.lang.reflect.InvocationTargetException
at org.smslib.modem.SerialModemDriver.connectPort(SerialModemDriver.java:93)
at org.smslib.modem.AModemDriver.connect(AModemDriver.java:106)
at org.smslib.modem.ModemGateway.startGateway(ModemGateway.java:111)
at org.smslib.Service$1Starter.run(Service.java:227)

2011.7.24

妈的!

org.smslib.TimeoutException: No response from device.
at org.smslib.modem.AModemDriver$CharQueue.get(AModemDriver.java:474)
at org.smslib.modem.AModemDriver.getResponse(AModemDriver.java:272)
at org.smslib.modem.athandler.ATHandler.getSimStatus(ATHandler.java:127)
at org.smslib.modem.AModemDriver.connect(AModemDriver.java:118)
at org.smslib.modem.ModemGateway.startGateway(ModemGateway.java:111)
at org.smslib.Service$1Starter.run(Service.java:227)

这个错误!害了我差不多俩小时了,公布出来

SerialModemGateway gateway = new SerialModemGateway("modem.com3", "COM3", 9600, "wavecom", "11258");

千万得配好这个东西,要不然,不止这里可能出错,上面的那个异常也可能出现

附源myeclipse工程:http://download.csdn.net/source/3412500

你可能感兴趣的:(java发送短信 wavecom)