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.
- At
- At + cmgf = 1
- At + cmgs = 138 XXXXXXXX
- 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
- Export LD_LIBRARY_PATH =/home/appusr/<SpanClass= Hilite1> JAVA </span> COMM: $ LD_LIBRARY_PATH
- 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
- 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. Current directory.
- 2. Each directory in $ {<SpanClass= 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
Lists All serial and parallel ports of the system to find the serial port name Java code used by the sms modem.
- Public Static VoidShowcommp orts (){
- Commp ortidentifier portid =Null;
- List port1vector =NewVector (32 );
- List port2vector =NewVector (32 );
- Enumeration ports = comatrix ortidentifier. getportidentifiers ();
- While(Ports. hasmoreelements ()){
- Portid = (comatrix ortidentifier) ports. nextelement ();
- // Debug ("commp ORT:" + portid. getname () + "/type:" + portid. getporttype ());
- Switch(Portid. getporttype ()){
- CaseCommp ortidentifier. port_serial: port1vector. Add (portid. getname ());Break;
- CaseCommp ortidentifier. 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);}
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
- Public Static VoidTest_rates ()ThrowsException {
- Int[] Rates = {2400,480 0, 9600,192 00, 115200,230 400, 460800,921 600 ,};
- Com2sms comm =NewCom2sms ();
- Comm. set_comm_rate (9600 );
- Comm. comm_open (5000 );
- For(IntI = 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);}
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)
- Com2sms comm =NewCom2sms (); // call initdriver for initialization
- Comm. set_comm ("com16", 115200); // Win32
- // 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
- Public Static BooleanInitdriver (){
- BooleanB = initdriver_linux ();
- If(! B) initdriver_win32 ();
- /If(! B) initdriver_solaris ();
- ReturnB;
- }
- Protected Static BooleanInitdriver_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 BooleanInitdriver_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 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 ).