Java serial Programming

Source: Internet
Author: User

Ultimate Goal: provide a stable and reliable Java SMS sending server in Linux.
Stage 1: Coding and testing on the Win32 platform;
Stage 2: deploy and test on the Linux platform;
Directory:
Related Resources: (Java communication package)
Preparation for Win32 serial Programming
Test procedure and at command for Win32 sms modem:
Preparations for Linux Serial Port Programming
Lists All serial and parallel ports of the system to find the serial port name used by the sms modem.
Test serial port speed
Differences in serial programming in Win32/Linux
Win32/LINUX serial programming (shielding platform differences)
Java serial port driver loaded in Win32/Linux
-------------------------
Related Resources: (Java communication package)
Comm3.0_uw.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/rxtxSupports windows, MacOS, Solaris, and Linux platforms.
Note: in Java, you can use the Java communication package to operate the serial port, but the official package will support Linux and Solaris platforms after 3.0, windows only supports version 2.0 after 98 years, but can still be used in xp. You can download it by Google. Of course, you can also use the open-source rxtx to implement serial communication.

Preparation for Win32 serial Programming
1, unzip javacomm20-win32.zip to 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. If you use the GSM-modem of the USB port, you also need to install the USB-to-serial driver:Http://www.jingweigps.com/xzzx.htm(Xing Xing hang)

Test procedure and at command for Win32 sms modem:
1, install the USB driver :( http://www.jingweigps.com/xzzx.htm Xing hang USB interface GSM/GPRS Modem driver)
2. Open the Device Manager and check which comport is used (displaying com15 for USB-to-serial). Right-click and select Properties to view the speed of 115200.
3. Use a Windows Super Terminal to connect to com15 and set the rate to 115200. Other Default values are as follows;
4. Test the Java code for sending text messages in text mode.

  1. At
  2. At + cmgf = 1
  3. At + cmgs = 138 XXXXXXXX
  4. Test <Ctrl-z>
atat+cmgf=1at+cmgs=138xxxxxxxxtest <ctrl-z>

Preparations for Linux Serial Port Programming
1. Unzip comm3.0_uw.linux.zip to/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 # Set Chinese characters. Otherwise, the ucs2 encoding for the SMS is incorrect.
Note 1: If you do not have the root permission, you can directly execute the following command: Java code

  1. Export LD_LIBRARY_PATH =/home/appusr/<SpanClass= Hilite1> JAVA </span> COMM: $ LD_LIBRARY_PATH
  2. Export classpath =.: <SpanClass= 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

Note 2: The search order for javax. Comm. properties is as follows: Java code

  1. 1. Current directory.
  2. 2. Each directory in $ {<SpanClass= 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

Lists All serial and parallel ports of the system to find the serial port name Java code used by the sms modem.

  1. Public Static VoidShowcommp orts (){
  2. Commp ortidentifier portid =Null;
  3. List port1vector =NewVector (32 );
  4. List port2vector =NewVector (32 );
  5. Enumeration ports = comatrix ortidentifier. getportidentifiers ();
  6. While(Ports. hasmoreelements ()){
  7. Portid = (comatrix ortidentifier) ports. nextelement ();
  8. // Debug ("commp ORT:" + portid. getname () + "/type:" + portid. getporttype ());
  9. Switch(Portid. getporttype ()){
  10. CaseCommp ortidentifier. port_serial: port1vector. Add (portid. getname ());Break;
  11. CaseCommp ortidentifier. port_parallel: port2vector. Add (portid. getname ());Break;
  12. Default:Break;
  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);}

Serial programming speed test: Use the AT command to test the serial speed. High speed is not necessarily compatible with low speed (send command, Return OK indicates a successful handshake) Java code

  1. Public Static VoidTest_rates ()ThrowsException {
  2. Int[] Rates = {2400,480 0, 9600,192 00, 115200,230 400, 460800,921 600 ,};
  3. Com2sms comm =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 ("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);}

Differences in serial programming in Win32/Linux
1. The name of the loaded driver is different (COM. Sun. Comm. win32driver | com. Sun. Comm. linuxdriver)
2. Win32 needs to load the dynamic library separately: system. loadlibrary ("win32com ")
3. The name of the serial port used is different (com15 |/dev/ttys0). The latter may require root permission.
4. The speed may be different;

Java code for Win32/LINUX serial programming (shielding platform differences)

  1. Com2sms comm =NewCom2sms (); // call initdriver for initialization
  2. Comm. set_comm ("com16", 115200); // Win32
  3. // Comm. set_comm ("/dev/ttys0", 9600); // Linux
Com2sms comm = new com2sms (); // call initdriver to initialize comm. set_comm ("com16", 115200); // Win32 // comm. set_comm ("/dev/ttys0", 9600); // Linux

Loading Java serial port driver Java code in Win32/Linux

  1. Public Static BooleanInitdriver (){
  2. BooleanB = initdriver_linux ();
  3. If(! B) initdriver_win32 ();
  4. /If(! B) initdriver_solaris ();
  5. ReturnB;
  6. }
  7. Protected Static BooleanInitdriver_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 BooleanInitdriver_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;}}

Next step (20070813 ):
1. Define an independent server and provide an HTTP external interface. (Completed)
2. Provides task queue processing to control sending traffic. (Completed)
3. Support for wappush. (Completed)
4. Support for DRC in omadrm. (Same as 3 );
5, Test 3 and 4 ultra-long messages (complete)
6. Test the ultra-long text message of common text (not completed ).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.