俗话说:巧妇难为无米之炊,第一步我们需要什么
首先,在window中进行java串口编程要下载javaxcomm组件,里面有三个重要的文件要进行配置:
1)首先将comm.jar拷贝到$JAVA_HOME/jre/lib/ext下面
2)将java.comm.properties拷贝到$JAVA_HOME/jre/lib下
3)将win32com.dll拷贝到$JAVA_HOME/bin下面
第二,下载smslib包http://smslib.org/
第三,我们用的时xfire框架,所以我们同样要去下载xfirehttp://xfire.codehaus.org/xfire现在已经与Celtix 合并为 CXF,xfire已经不在进行研发了,但是我们依旧可以使用
有了米之后我们开始我们的编程部分,首先我使用myeclipse6.5 + jdk1.6
我们新建一个webproject 命名为WebServiceMessage,然后把sms中的jar一级xfire中的jar导入到工程环境变量中
然后我们新建一接口(xfire相关)
package com.iteye.javaflex.webservice; /** * * @author http://javaflex.iteye.com/ * */ public interface MessageService { public String sendMessage(String telphoneNumber,String content); }
在建一实现类(xfire相关)
package com.iteye.javaflex.webservice.impl; import com.iteye.javaflex.webservice.MessageService; /** * * @author http://javaflex.iteye.com/ * */ public class MessageServiceImpl implements MessageService { public String sendMessage(String telphoneNumber, String content) { String result; SM sm=new SM(); result = sm.doIt(telphoneNumber, content); System.out.println(result); return "成功"; } }
下面创建发送短信的类(这个是真正和串口编程相关的)
package com.iteye.javaflex.webservice.impl; 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; /** * * @author http://javaflex.iteye.com/ * */ public class SM { public String doIt(String telNumber, String content) { 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.com1", "COM5", 115200, "Huawei", "9600"); gateway.setInbound(true); gateway.setOutbound(true); gateway.setSmscNumber("+8613010200500");// 短信中心号码 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(telNumber, content); msg.setEncoding(MessageEncodings.ENCUCS2); Service.getInstance().sendMessage(msg); System.out.println(msg); } catch (Exception e) { //e.printStackTrace(); return "发送失败"; } try { Service.getInstance().stopService(); } catch (Exception e) {// TODO Auto-generated catch block //e.printStackTrace(); return "服务停止出错"; } return "发送成功"; } 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[]) { SM app = new SM(); try { String string=app.doIt("", "java客户端测试"); System.out.println(string); } catch (RuntimeException e) { e.printStackTrace(); } } }
我们要在src下面新建MEAT-INF\xfire\service.xml这个最终到class里面
<?xml version="1.0" encoding="UTF-8"?> <!-- @author http://javaflex.iteye.com/ --> <beans xmlns="http://xfire.codehaus.org/config/1.0"> <service> <name>MessageService</name> <serviceClass>com.iteye.javaflex.webservice.MessageService</serviceClass> <implementationClass>com.iteye.javaflex.webservice.impl.MessageServiceImpl</implementationClass> </service> </beans>
最后配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>XFireServlet</servlet-name> <servlet-class> org.codehaus.xfire.transport.http.XFireConfigurableServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>XFireServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
至此一个基于webservice的短信* 短信发送系统已经完成
然后部署到tomcat,看看我们的webservice是否配置成功
我们在浏览器中输入http://127.0.0.1:8080/WebServiceMessage/services/MessageService?wsdl
如果现实如图则说明配置成功
webservice配置成功后,我们就要用它,下面我用delphi去连接我们的短信发送服务
新建一application,然后新建wsdl importer(按步骤输入http://127.0.0.1:8080/WebServiceMessage/services/MessageService?wsdl)
delphi都是控件拖拽的没有什么可说的下面截图看一下效果
下面把delphi的代码贴上供大家下载,首先声明我是delphi菜鸟,java代码代码量jar包太多,而且我已经把代码都贴出来了,所以就不传java代码了
至于用vs2010去实现,那个和delphi如出一辙