Java serial port programming (SMS Modem ).

Source: Internet
Author: User
Keywords: Java serial port programming, SMS Modem, AT command

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.ziphttp: // mdubuc.freeshell.org/Jolt/javacomm20-win32.zip

Rxtx-2.1-7-bins.ziphttp: // www.frii.com /~ Jarvi/rxtx supports Windows, MacOS, Solaris, and Linux platforms.

Note: in java, Java
The Communication package can be used to operate the serial port, but the official package will support the Linux and Solaris platforms after 3.0. The Windows platform only supports
Version 2.0, but it 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. jarc: \ 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 driver: http://www.jingweigps.com/xzzx.htm (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 TEXT message sending mode.
Java code

  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/JavaComm: $ LD_LIBRARY_PATH
  2. Export CLASSPATH =.: javacomm_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_PATHexport 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 $ {java. 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 used by the SMS Modem.
Java code

  1. Public static void showcommp orts (){
  2. Commp ortidentifier portID = null;
  3. List port1Vector = new Vector (32 );
  4. List port2Vector = new Vector (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. Case commp ortidentifier. PORT_SERIAL: port1Vector. add (portID. getName (); break;
  11. Case commp 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 the command. If OK is returned, the handshake is successful)
Java code

  1. Public static void test_rates () throws Exception {
  2. Int [] rates = {2400,480 0, 9600,192 00, 115200,230 400, 460800,921 600 ,};
  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);}

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;

Win32/Linux serial programming (shielding platform differences)
Java code

  1. Com2Sms comm = new Com2Sms (); // 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

Java serial port driver loaded in Win32/Linux
Java code

  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;}}

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 ).

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.