最终目标:在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模式测试发送短信
- at
- at+cmgf=1
- at+cmgs=138xxxxxxxx
- 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权限,可以直接执行如下命令:
- 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
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的搜索顺序如下:
- 1. Current directory.
- 2. Each directory in ${java.classpath} (ie. $CLASSPATH or -classpath setting).
- 3. <JDK>/lib.
- 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所使用的串口名字
- 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();
-
- 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);
- }
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则表示握手成功)
- 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);
- }
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下串口编程(屏蔽平台差异)
- Com2Sms comm = new Com2Sms();
- comm.set_comm("COM16", 115200);
-
Com2Sms comm = new Com2Sms(); //调用initDriver初始化
comm.set_comm("COM16", 115200); //win32
//comm.set_comm("/dev/ttyS0", 9600); //linux
Win32/Linux下加载Java串口驱动
- 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) {
-
- 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) {
-
- log.warn("initDriver() "+e);
- return false;
- }
- }
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,测试普通文本的超长短信(未完)。