天天看點

linux下用java通過序列槽連接配接手機或短信貓發送中文短信的方法

到http://java.sun.com/products/javacomm/ 下載下傳jar包

解壓,把其中so檔案複制到/usr/lib

把javax.comm.properties複制到jre目錄下

把comm.jar放到classpath目錄下

序列槽連接配接:

        CommPortIdentifier portID;

        String owner = new String("modem");

        int keeptime = 5000;

        Enumeration portList;

        portList = CommPortIdentifier.getPortIdentifiers();

//        String driverName = "com.sun.comm.Win32Driver";

        String driverName = "com.sun.comm.LinuxDriver";

        CommDriver driver = null;

        try {

//            System.loadLibrary("win32com");

            driver = (CommDriver) Class.forName(driverName).newInstance();

            driver.initialize();

            System.out.println("Win32Driver Initialized");

        } catch (Exception e) {

            e.printStackTrace();

        }

        // 如果有多個端口

        while (portList.hasMoreElements()) {

            portID = (CommPortIdentifier) portList.nextElement();

            System.out.println("COM:" + portID.getName());

//            if (portID.getName().equals("COM1"))

            if (portID.getName().equals("/dev/ttyS0"))

            try {

                sPort = (SerialPort) portID.open(owner, keeptime);

                break;

            }catch (PortInUseException e) {

                e.printStackTrace();

                System.exit(1);

            }

        }

        try{

            if (sPort != null)

            {

                in = sPort.getInputStream();

                out = sPort.getOutputStream();

                try {

                    sPort.addEventListener(this);

                    sPort.notifyOnDataAvailable(true);

                 } catch (TooManyListenersException e) {

                     e.printStackTrace();

                 }

            }

        }catch(Exception e)

        {

            e.printStackTrace();

        }

其中注釋掉的是windows下的連接配接方法

全部代碼

import javax.comm.*;

import java.util.Enumeration;

import java.util.TooManyListenersException;

import java.io.*;

public class ATSms implements SerialPortEventListener {

    private SerialPort sPort = null;

    private InputStream in  = null;

    private OutputStream out = null;

    private byte b[] = new byte[1024];

    private String cmd = "";

    private int len = 0;

    private String recvMsg = "";

    public ATSms()

    {

        init();

    }

    public synchronized void serialEvent(SerialPortEvent serialPortEvent) {

        int c = 0;

        // 如果有序列槽事件發生

        if (serialPortEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE)

        {

            try {

                System.out.print("Recv  :" );

                while ((c = in.read()) != -1) {

                    System.out.print((char)c);

                    recvMsg += ((char)c);

                }

            }// try

            catch (IOException e) {

                e.printStackTrace();

            }

        }

    }

    private static String getUnicodeString(String s)

    {

        String unicodeStr = "";

        for(int i=0;i<s.length();i++)

        {

            String c = Integer.toHexString(s.charAt(i)   &   0xffff);

            while(c.length()<4)

            {

                c = "0" + c;

            }

            unicodeStr += c;

        }

        return unicodeStr;

    }

    private static String getSMobile(String mobile)

    {

        StringBuffer sMobile = new StringBuffer("68");

        for( int i=0;i<mobile.length();i=i+2)

        {

            sMobile.append((i+1<mobile.length())?mobile.charAt(i+1):"F");

            sMobile.append(mobile.charAt(i));

        }

        return sMobile.toString();

    }

    private void init()

    {

        CommPortIdentifier portID;

        String owner = new String("modem");

        int keeptime = 5000;

        Enumeration portList;

        portList = CommPortIdentifier.getPortIdentifiers();

//        String driverName = "com.sun.comm.Win32Driver";

        String driverName = "com.sun.comm.LinuxDriver";

        CommDriver driver = null;

        try {

//            System.loadLibrary("win32com");

//            System.out.println("Win32Com Library Loaded");

            driver = (CommDriver) Class.forName(driverName).newInstance();

            driver.initialize();

            System.out.println("Win32Driver Initialized");

        } catch (Exception e) {

            e.printStackTrace();

        }

        // 如果有多個端口

        while (portList.hasMoreElements()) {

            portID = (CommPortIdentifier) portList.nextElement();

            System.out.println("COM:" + portID.getName());

//            if (portID.getName().equals("COM1"))

            if (portID.getName().equals("/dev/ttyS0"))

            try {

//                System.out.println("try to open the /dev/ttyS0");

                sPort = (SerialPort) portID.open(owner, keeptime);

//                System.out.println("try to open the /dev/ttyS0 OK");

                break;

            }catch (PortInUseException e) {

                e.printStackTrace();

                System.exit(1);

            }

        }

        try{

            if (sPort != null)

            {

                in = sPort.getInputStream();

                out = sPort.getOutputStream();

                try {

                    sPort.addEventListener(this);

                    sPort.notifyOnDataAvailable(true);

                 } catch (TooManyListenersException e) {

                     e.printStackTrace();

                 }

            }

        }catch(Exception e)

        {

            e.printStackTrace();

        }

    }

    public void close()

    {

        try {

            Thread.sleep(4000);

            in.close();

            out.close();

        } catch (Exception e) {

            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.

        }

        sPort.close();

    }

    public int sendSms(String recv,String content)

    {

        int sendok = 1;

        for(int i=0;i<content.length();i=i+70)

        {

            sendok = sendShortSms(recv, ((i+70<=content.length())?content.substring(i,i+70):content.substring(i))  );

        }

        return sendok;

    }

    private int sendShortSms(String recv,String content)

    {

        int sendok = -1;

        try{

            System.out.println("send sms to:" + recv + "/ncontent:" + content);

            content = getUnicodeString(content);

//            content = (j+70>uc.length()/2)?uc.substring(j):uc.substring(j,j+140);

//            System.out.println(j + ":" + content);

            cmd = "at+cmgs=" + Integer.toString(15+content.length()/2) + "/r";

            out.write(cmd.getBytes());

            out.flush();

            System.out.println("Send:" + cmd);

            for(int i=0;i<5;i++)

            {

                if(recvMsg.indexOf(">")>=0)

                {

                    recvMsg = recvMsg.substring(recvMsg.indexOf(">")+1);

                    break;

                }else

                {

                    try

                    {

                        Thread.sleep(1000);

                    }catch(Exception e)

                    {

                    }

                }

            }

            cmd =  Integer.toHexString(content.length()/2  &   0xffff);

            if(cmd.length()<2)cmd="0"+cmd;

            cmd =  "0011000D91" + getSMobile(recv) + "000800" + cmd + content;

            cmd = cmd.toUpperCase();

            out.write(cmd.getBytes());

            out.write(26);

            out.flush();

            System.out.println("Send:" + cmd);

            for(int i=0;i<5;i++)

            {

                if(recvMsg.indexOf("OK")>=0)

                {

                    recvMsg = recvMsg.substring(recvMsg.indexOf("OK")+2);

                    sendok = 1;

                    break;

                }else

                {

                    try

                    {

                        Thread.sleep(1000);

                    }catch(Exception e)

                    {

                    }

                }

            }

        }catch(Exception e)

        {

            sendok = -1;

            e.printStackTrace();

        }

        return sendok;

    }

    public static void main(String args[])

    {

        String c = ("16日" );

        ATSms at = new ATSms();

        at.sendSms("13999999999",c);

        at.close();

    }

}