⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 commdictate.java

📁 实现串口手机短信发送程序很全面的源代码
💻 JAVA
字号:
package com.sunfruit.comm.dictate;

import javax.comm.CommPortIdentifier;
import java.util.Enumeration;
import java.util.ArrayList;
import com.sunfruit.comm.NokiaCoding;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.comm.CommPort;
import com.sunfruit.comm.util.GetResource;
import org.apache.log4j.Logger;
import com.sunfruit.comm.util.PDUDataFormat;
/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class CommDictate {
    private static Logger log = Logger.getLogger(CommDictate.class);

    public CommDictate() {
    }

    public static String[] getComName()
    {
        ArrayList arrayList=new ArrayList();
        Enumeration en = CommPortIdentifier.getPortIdentifiers();
        CommPortIdentifier portId=null;
        while (en.hasMoreElements()) {
            portId = (CommPortIdentifier) en.nextElement();
            /*如果端口类型是串口,则打印出其端口信息*/
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
            {
                arrayList.add(portId.getName());
            }
        }
        String[] name=null;
        if(arrayList.size()>0)
            name=(String[])(arrayList.toArray(new String[0]));
        else
        {
            name = new String[0];
            name[0]="无可用端口";
        }
        return name;
    }

    public static boolean isUseCom(String comname)
    {
        boolean bool=false;

        Enumeration en = CommPortIdentifier.getPortIdentifiers();
        CommPortIdentifier portId=null;
        while (en.hasMoreElements()) {
            portId = (CommPortIdentifier) en.nextElement();
            /*如果端口类型是串口,则打印出其端口信息*/
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
            {
                if(portId.getName().equals(comname))
                {
                    break;
                }
            }
        }

        byte[] bytes=new byte[128];
        CommPort commPort=null;
        OutputStream out=null;
        InputStream input=null;
        try {
            commPort=portId.open("nokia", 120);
            out=commPort.getOutputStream();
            input=commPort.getInputStream();
            out.write(("AT"+GetResource.getNewLine()).getBytes());
            out.flush();
            Thread.sleep(2000);
            bytes=new byte[input.available()];
            input.read(bytes);
            String retstr=new String(bytes,"UTF-8");
            if(retstr.toUpperCase().indexOf("OK")>=0)
            {
                bool=true;
            }
            log.info(retstr);
        } catch (Exception ex) {
            bool=false;
            log.error(ex);
        }
        finally
        {
            try {
                if(out!=null)
                    out.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            try {
                if(input!=null)
                    input.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            if(commPort!=null)
                commPort.close();
        }
        return bool;
    }

    public static boolean sendMessage(String sendnumber,String text)
    {
        boolean bool=true;

        Enumeration en = CommPortIdentifier.getPortIdentifiers();
        CommPortIdentifier portId=null;
        while (en.hasMoreElements()) {
            portId = (CommPortIdentifier) en.nextElement();
            /*如果端口类型是串口,则打印出其端口信息*/
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
            {
                if(portId.getName().equals(GetResource.getCenterNumber().getComname()))
                {
                    break;
                }
            }
        }

        byte[] bytes=new byte[128];
        CommPort commPort=null;
        OutputStream out=null;
        InputStream input=null;
        try {
            commPort=portId.open("nokia", 120);
            out=commPort.getOutputStream();
            input=commPort.getInputStream();
            out.write(("AT+CMGF=0"+GetResource.getNewLine()).getBytes());
            out.flush();
            Thread.sleep(2000);
            bytes=new byte[input.available()];
            int i=input.read(bytes);
            String retstr=(new String(bytes,0,i,"UTF-8")).toUpperCase();
            if(retstr.indexOf("OK")>=0)
            {
                String PDU=PDUDataFormat.getPDUCoding(sendnumber,PDUDataFormat.getASCII(text));
                out.write(("AT+CMGS="+PDUDataFormat.getPDULength()+GetResource.getNewLine()).getBytes());
                out.flush();
                Thread.sleep(2000);
                bytes=new byte[input.available()];
                input.read(bytes);
                log.info(new String(bytes,"UTF-8"));
                out.write((PDU+(char)0X001A+"\r").getBytes());
                out.flush();
                Thread.sleep(3000);
                bytes=new byte[input.available()];
                input.read(bytes);
                String returnstr=new String(bytes,"UTF-8");
                if(returnstr.toUpperCase().indexOf("OK")>=0)
                {
                    bool=true;
                }
                else
                {
                    bool=false;
                }
                log.info(returnstr);
            }
            else
            {
                bool=false;
                log.info(retstr);
            }


        } catch (Exception ex) {
            bool=false;
            log.error(ex);
        }
        finally
        {
            try {
                if(out!=null)
                    out.close();
            } catch (IOException ex) {
                log.error(ex);
            }
            try {
                if(input!=null)
                    input.close();
            } catch (IOException ex) {
                log.error(ex);
            }
            if(commPort!=null)
                commPort.close();
        }
        return bool;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -