Java串口編程

來源:互聯網
上載者:User
 

最終目標:在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>  
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代碼

  1. export LD_LIBRARY_PATH=/home/appusr/<SPAN class=hilite1>Java</SPAN>Comm:$LD_LIBRARY_PATH    
  2. 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   
  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.jarexport LANG=zh_CN.GBK

注2:針對javax.comm.properties的搜尋順序如下: Java代碼

  1. 1. Current directory.     
  2. 2. Each directory in ${<SPAN class=hilite1>java</SPAN>.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 = {2400, 4800, 9600, 19200, 115200, 230400, 460800, 921600, };   
  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,測試普通文本的超長簡訊(未完)。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.