最終目標:在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代碼
- at
- at+cmgf=1
- at+cmgs=138xxxxxxxx
- test <ctrl-z>
atat+cmgf=1at+cmgs=138xxxxxxxxtest <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代碼
- export LD_LIBRARY_PATH=/home/appusr/<SPAN class=hilite1>Java</SPAN>Comm:$LD_LIBRARY_PATH
- export CLASSPATH=.:<SPAN class=hilite1>java</SPAN>comm_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.jarexport LANG=zh_CN.GBK
注2:針對javax.comm.properties的搜尋順序如下: Java代碼
- 1. Current directory.
- 2. Each directory in ${<SPAN class=hilite1>java</SPAN>.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所使用的串口名字 Java代碼
- 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);
- }
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代碼
- 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下串口編程(屏蔽平台差異) Java代碼
- Com2Sms comm = new Com2Sms(); //調用initDriver初始化
- comm.set_comm("COM16", 115200); //win32
- //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代碼
- 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;
- }
- }
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,測試普通文本的超長簡訊(未完)。