Java Serial Programming (SMS modem) __ algorithm

Source: Internet
Author: User
Tags throwable win32 log4j
Final goal: to provide a stable and reliable Java SMS sending server under Linux.
The first stage: coding and testing under the Win32 platform;
Phase two: Deploy and test under Linux platform;
Directory:
Related resources: (Java communication package)
Pre-preparation of WIN32 serial port programming
Win32 SMS Modem test steps and at instructions:
Pre-preparation of Linux serial port programming
List all serial ports and ports of the system to find the serial name used in the SMS modem
Test serial port Rate
The difference of serial programming under Win32/linux
Win32/linux under the serial port programming (shielding platform difference)
Win32/linux load Java serial driver
-------------------------
Related resources: (Java communication package)
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 supports windows/macos/solaris/linux four platforms
Note: In Java, the use of Java Communication Package can operate the serial port, But the official package supports the Linux and Solaris platforms after 3.0, and the Windows platform only supports the 2.0 version from 98, but it can be used under XP, and Google can download it. Of course, can also use open source RXTX to achieve serial communication

Pre-preparation of WIN32 serial port 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 the use of USB port Gsm-modem, you also need to install, usb-to-serial Drive: http://www.jingweigps.com/xzzx.htm (Jingwei Star)

Win32 SMS Modem test steps and at instructions:
1, the installation of USB drive: (http://www.jingweigps.com/xzzx.htm-Jingwei Satellite USB Interface Gsm/gprs Modem driver)
2, open Device Manager to see which COM port is used (display usb-to-serial for COM15), right-click to select Properties, view rate of 115200
3, use Windows HyperTerminal, connect COM15, set rate 115200, other defaults;
4, text-mode test to send SMS
Java code at at+cmgf=1 at+cmgs=138xxxxxxxx test <ctrl-z>
	At
	at+cmgf=1
	at+cmgs=138xxxxxxxx
	test <ctrl-z>

Pre-preparation of Linux serial port programming
1,unzip Comm3.0_u1_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 #设置中文, otherwise UCS2 encoding is incorrect for SMS.
Note 1: If you do not have root permissions, you can execute the following command directly:
Java code 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	

Note 2: The search order for Javax.comm.properties is as follows:
Java code 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

List all serial ports and ports of the system to find the serial name used in the SMS modem
Java code public static void showcommports ()  {        commportidentifier portid = null;        list port1vector = new vector (; )        list port2vector = new vector (; )         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;           &nbsp}        }        debug ("port_serial   = " +port1vector);        debug ("port_parallel = " +port2vector);   }  
	public static void Showcommports () {
		commportidentifier portid = null;
		List port1vector = new Vector (a); 
		List port2vector = new Vector (a); 
		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 port Programming rate test: Use at command to test the serial port rate, high-speed is not compatible with low speed (Send command, return OK means handshake success)
Java code 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, 921 ,};
		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);
	}

The difference of serial programming under Win32/linux
1, loaded drive names differ (Com.sun.comm.Win32Driver | Com.sun.comm.LinuxDriver)
2,win32 need to load dynamic libraries individually: system.loadlibrary ("win32com")
3, the serial names used are different (COM15 | /DEV/TTYS0), the latter may also require root permissions.
4, the rate may not be the same;

Win32/linux under the serial port programming (shielding platform difference)
Java code com2sms comm = new Com2sms ();     Invoke Initdriver initialization Comm.set_comm ("COM16", 115200);    Win32//comm.set_comm ("/dev/ttys0", 9600); Linux
	Com2sms comm = new Com2sms ();		Invoke Initdriver initialization
	comm.set_comm ("COM16", 115200);		Win32
	//comm.set_comm ("/dev/ttys0", 9600);	Linux


Win32/linux load Java serial driver
Java code 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 ();    &Nbsp;     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; }
	}


Next work (20070813):
1, the definition of a stand-alone server, providing HTTP external interface. (completed)
2, provide task queue processing, control the sending traffic. (completed)
3, support for Wappush. (completed)
4, support for the DRC in OMADRM. (same as 3);
5, test 3 and 4 of the Super Long text message (complete)
6, test the ordinary text of the long message (not finished).
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.