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>  
	at
	at+cmgf=1
	at+cmgs=138xxxxxxxx
	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. export LD_LIBRARY_PATH=/home/appusr/JavaComm:$LD_LIBRARY_PATH    
  2. export CLASSPATH=.:javacomm_linux.jar:commons-logging-1.0.4.jar:log4j-1.2.8.jar:junit-3.8.1.jar:mpsp_bs2.jar   
  3. export LANG=zh_CN.GBK     
	export LD_LIBRARY_PATH=/home/appusr/JavaComm:$LD_LIBRARY_PATH 
	export CLASSPATH=.:javacomm_linux.jar:commons-logging-1.0.4.jar:log4j-1.2.8.jar:junit-3.8.1.jar:mpsp_bs2.jar
	export LANG=zh_CN.GBK	

注2:针对javax.comm.properties的搜索顺序如下:
Java代码 复制代码
  1. 1. Current directory.     
  2. 2. Each directory in ${java.classpath}   (ie.  $CLASSPATH or -classpath setting).   
  3. 3. <JDK>/lib.     
  4. 4. <JDK>/jre/lib  
	1. Current directory.  
	2. Each directory in ${java.classpath}   (ie.  $CLASSPATH or -classpath setting).
	3. <JDK>/lib.  
	4. <JDK>/jre/lib

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

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

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. Com2Sms comm = new Com2Sms();       //调用initDriver初始化   
  2. comm.set_comm("COM16"115200);     //win32   
  3. //comm.set_comm("/dev/ttyS0", 9600);    //linux  
	Com2Sms comm = new Com2Sms();		//调用initDriver初始化
	comm.set_comm("COM16", 115200);		//win32
	//comm.set_comm("/dev/ttyS0", 9600);	//linux


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


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值