用心得(1)!
领导要求在开发的系统中添加发送短信的功能,但又不想使用人家的短信服务器(想省钱,哈哈),只好自己开发。上网找了很久,最好找到了一个不错的发送短信的jar包——jsmsengine。它是一个开源的东东,你可以访问http://jsmsengine.sourceforge.net/ 来了解更详细的信息。下面就是我在使用的过程中遇到的一些问题,在这里与大家一起分享。
将jsmsengine_1_2_6-B1下载并解压,可以看到相关的jar包、源码、文档、例子以及一个简单的SMS服务器程序。闲话少说,赶紧在eclipse中新建一个项目将源码和例子引进来,然后又向同事借了个支持AT指令的手机(LG8390),连接到串口上。运行SendMessage.java
//下面是解压后自带的SendMessage.java的原文件
class SendMessage
{
public static void main(String[] args)
{
int status;
// Create jSMSEngine service.
CService srv = new CService("com1", 9600);
System.out.println();
System.out.println("SendMessage(): sample application.");
System.out.println(" Using " + srv._name + " " + srv._version);
System.out.println();
try
{
// Initialize service.
srv.initialize();
// Set the cache directory.
srv.setCacheDir(".//");
// Set the phonebook.
// srv.setPhoneBook("../misc/phonebook.xml");
// Connect to GSM device.
status = srv.connect();
// Did we connect ok?
if (status == CService.ERR_OK)
{
// Set the operation mode to PDU - default is ASCII.
srv.setOperationMode(CService.MODE_PDU);
// Set the SMSC number (set to default).
srv.setSmscNumber("");
// Print out GSM device info...
System.out.println("Mobile Device Information: ");
System.out.println(" Manufacturer : " + srv.getDeviceInfo().getManufacturer());
System.out.println(" Model : " + srv.getDeviceInfo().getModel());
System.out.println(" Serial No : " + srv.getDeviceInfo().getSerialNo());
System.out.println(" IMSI : " + srv.getDeviceInfo().getImsi());
System.out.println(" S/W Version : " + srv.getDeviceInfo().getSwVersion());
System.out.println(" Battery Level : " + srv.getDeviceInfo().getBatteryLevel() + "%");
System.out.println(" Signal Level : " + srv.getDeviceInfo().getSignalLevel() + "%");
// Create a COutgoingMessage object and dispatch it.
// *** Please update the phone number with one of your choice ***
COutgoingMessage msg = new COutgoingMessage("此处为你要发送的手机号", "Message from jSMSEngine API.");
// Character set is 7bit by default - lets make it UNICODE :)
// We can do this, because we are in PDU mode (look at line 63). When in ASCII mode,
// this does not make ANY difference...
msg.setMessageEncoding(CMessage.MESSAGE_ENCODING_UNICODE);
if (srv.sendMessage(msg) == CService.ERR_OK) System.out.println("Message Sent!");
else System.out.println("Message Failed!");
// Disconnect from GSM device.
srv.disconnect();
}
else System.out.println("Connection to mobile failed, error: " + status);
}
catch (Exception e)
{
e.printStackTrace();
}
System.exit(0);
}
}
程序,出现如下错误:
Error loading win32com: java.lang.UnsatisfiedLinkError: no win32com in java.library.path
Connection to mobile failed, error: -11
根据错误提示,引用的一个java在windowns下访问串口的必备动态链接库——win32com.dll,找到这个东东后把它拷贝到JDK安装目录下的bin文件夹下。
这次我没有急于运行程序,首先确定串口是否好用,我打开了系统自带的“超级终端”,然后输入‘at’并回车,程序响应如下:
这说明串口与手机连通正常,并且手机已经可以响应AT指令。