`
linuxstuding
  • 浏览: 1227151 次
文章分类
社区版块
存档分类
最新评论

Java串口编程(短信Modem)

 
阅读更多
最终目标:在Linux下提供一个稳定可靠的Java短信发送服务器。
第一阶段:在Win32平台下编码并测试;
第二阶段:在Linux平台下部署并测试;
目录:
相关资源:(Java Communication包)
Win32串口编程前期准备
Win32短信Modem的测试步骤和AT指令:
Linux串口编程前期准备
列出系统所有串口、并口,来找到短信Modem所使用的串口名字
测试串口速率
Win32/Linux下串口编程的差异
Win32/Linux下串口编程(屏蔽平台差异)
Win32/Linux下加载Java串口驱动
-------------------------
相关资源:(Java Communication包)
comm3.0_u1_linux.zip http://www.sun.com/download/products.xml?id=43208d3d
comm2.0.3.zip (for solaris)
javacomm20-win32.zip http://mdubuc.freeshell.org/Jolt/javacomm20-win32.zip
rxtx-2.1-7-bins.zip http://www.frii.com/~jarvi/rxtx 支持Windows/MacOS/Solaris/Linux四个平台
注:在java中,利用Java Communication包可以操作串口,但官方的包在3.0之后就支持Linux和Solaris平台了,Windows平台的只支持到98年出的2.0版本,不过在XP下还能使用,google一下就可以下载到。当然,也可以用开源的Rxtx实现串口通信

Win32串口编程前期准备
1,unzip javacomm20-win32.zip 到c:/下
2,copy c:/commapi/win32com.dll c:/jdk1.4.2/bin
3,copy c:/commapi/javax.comm.properties c:/jdk1.1.6/lib
4,copy c:/commapi/comm.jar c:/jdk1.1.6/lib
5,set CLASSPATH=c:/jdk1.1.6/lib/comm.jar;%classpath%
6,如果使用USB口的GSM-Modem,则还需要安装,USB-to-Serial的驱动:http://www.jingweigps.com/xzzx.htm (经纬星航)

Win32短信Modem的测试步骤和AT指令:
1,安装USB驱动:(http://www.jingweigps.com/xzzx.htm 经纬星航 USB接口 GSM/GPRS MODEM驱动程序 )
2,打开设备管理器,看看是使用了哪个COM口(显示USB-to-Serial的为COM15),右键选择属性,查看速率为115200
3,使用Windows的超级终端,连接COM15,设定速率115200,其他缺省;
4,以TEXT模式测试发送短信
Java代码 复制代码
  1. at
  2. at+cmgf=1
  3. at+cmgs=138xxxxxxxx
  4. test<ctrl-z>

Linux串口编程前期准备
1,unzip comm3.0_u1_linux.zip 到/home/appusr/JavaComm下
2,cp /home/appusr/JavaComm/libLinuxSerialParallel.so /usr/lib
3,cp /home/appusr/JavaComm/javax.comm.properties /usr/java/j2sdk1.4.2_11/lib
4,cp /home/appusr/JavaComm/comm_linux.jar /usr/java/j2sdk1.4.2_11/lib
5,set CLASSPATH=.:/home/appusr/JavaComm/comm_linux.jar:$CLASSPATH
6,export LANG=zh_CN.GBK #设置中文,否则针对短信进行UCS2编码不正确。
注1:如果没有ROOT权限,可以直接执行如下命令:
Java代码 复制代码
  1. exportLD_LIBRARY_PATH=/home/appusr/JavaComm:$LD_LIBRARY_PATH
  2. exportCLASSPATH=.:javacomm_linux.jar:commons-logging-1.0.4.jar:log4j-1.2.8.jar:junit-3.8.1.jar:mpsp_bs2.jar
  3. exportLANG=zh_CN.GBK

注2:针对javax.comm.properties的搜索顺序如下:
Java代码 复制代码
  1. 1.Currentdirectory.
  2. 2.Eachdirectoryin${java.classpath}(ie.$CLASSPATHor-classpathsetting).
  3. 3.<JDK>/lib.
  4. 4.<JDK>/jre/lib

列出系统所有串口、并口,来找到短信Modem所使用的串口名字
Java代码 复制代码
  1. publicstaticvoidshowCommPorts(){
  2. CommPortIdentifierportID=null;
  3. Listport1Vector=newVector(32);
  4. Listport2Vector=newVector(32);
  5. Enumerationports=CommPortIdentifier.getPortIdentifiers();
  6. while(ports.hasMoreElements()){
  7. portID=(CommPortIdentifier)ports.nextElement();
  8. //debug("CommPort:"+portID.getName()+"/type:"+portID.getPortType());
  9. switch(portID.getPortType()){
  10. caseCommPortIdentifier.PORT_SERIAL:port1Vector.add(portID.getName());break;
  11. caseCommPortIdentifier.PORT_PARALLEL:port2Vector.add(portID.getName());break;
  12. default:break;
  13. }
  14. }
  15. debug("PORT_SERIAL="+port1Vector);
  16. debug("PORT_PARALLEL="+port2Vector);
  17. }

串口编程速率测试:使用AT指令测试串口速率,高速不一定兼容低速(发送命令,返回OK则表示握手成功)
Java代码 复制代码
  1. publicstaticvoidtest_rates()throwsException{
  2. int[]rates={2400,4800,9600,19200,115200,230400,460800,921600,};
  3. Com2Smscomm=newCom2Sms();
  4. comm.set_comm_rate(9600);
  5. comm.comm_open(5000);
  6. for(inti=0;i<rates.length;i++){
  7. try{
  8. comm.set_comm_rate(rates[i]);
  9. comm.at_hello();
  10. log.info("SUCCforrate:"+rates[i]);
  11. }catch(Exceptione){
  12. log.warn("FAILforrate:"+rates[i]);
  13. }
  14. sleepMSec(1000,"set_comm_rate:"+rates[i]);
  15. }
  16. comm.comm_close(5000);
  17. }

Win32/Linux下串口编程的差异
1,加载的驱动名字不同(com.sun.comm.Win32Driver | com.sun.comm.LinuxDriver)
2,Win32需要单独加载动态库:System.loadLibrary("win32com")
3,所使用的串口名字不同(COM15 | /dev/ttyS0),后者可能还需要root权限。
4,速率可能不一样;

Win32/Linux下串口编程(屏蔽平台差异)
Java代码 复制代码
  1. Com2Smscomm=newCom2Sms();//调用initDriver初始化
  2. comm.set_comm("COM16",115200);//win32
  3. //comm.set_comm("/dev/ttyS0",9600);//linux


Win32/Linux下加载Java串口驱动
Java代码 复制代码
  1. publicstaticbooleaninitDriver(){
  2. booleanb=initDriver_linux();
  3. if(!b)initDriver_win32();
  4. /if(!b)initDriver_solaris();
  5. returnb;
  6. }
  7. protectedstaticbooleaninitDriver_win32(){
  8. try{
  9. System.loadLibrary("win32com");
  10. debug("loadLibrary()...win32com.dll");
  11. StringdriverName="com.sun.comm.Win32Driver";
  12. CommDriverdriver=(CommDriver)Class.forName(driverName).newInstance();
  13. driver.initialize();
  14. debug("initDriver()..."+driverName);
  15. returntrue;
  16. }catch(Throwablee){
  17. //e.printStackTrace();
  18. log.warn("initDriver()"+e);
  19. returnfalse;
  20. }
  21. }
  22. protectedstaticbooleaninitDriver_linux(){
  23. try{
  24. StringdriverName="com.sun.comm.LinuxDriver";
  25. CommDriverdriver=(CommDriver)Class.forName(driverName).newInstance();
  26. driver.initialize();
  27. debug("initDriver()..."+driverName);
  28. returntrue;
  29. }catch(Throwablee){
  30. //e.printStackTrace();
  31. log.warn("initDriver()"+e);
  32. returnfalse;
  33. }
  34. }


下一步工作(20070813):
1,定义独立的服务器,提供http对外接口。(已完成)
2,提供任务队列处理,控制发送流量。(已完成)
3,对WapPush的支持。(已完成)
4,对OMADRM中DRC的支持。(和3一样);
5,测试3和4的超长短信(完成)
6,测试普通文本的超长短信(未完)。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics