国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java使用短信設備發送sms短信的示例(java發送短信)

2019-11-26 15:32:12
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

import gnu.io.*;
import java.util.*;
import java.io.*;

public class CommTest
{
    static CommPortIdentifier portId;
    static Enumeration portList;
    static int bauds[] = { 9600, 19200, 57600, 115200 };    //檢測端口所支持的波特率

    public static void main(String[] args)
    {
        portList = CommPortIdentifier.getPortIdentifiers();
        System.out.println("短信設備端口連接測試...");
        while (portList.hasMoreElements())
        {
            portId = (CommPortIdentifier) portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
            {
                System.out.println("找到串口: " + portId.getName());
                for (int i = 0; i < bauds.length; i++)
                {
                    System.out.print("  Trying at " + bauds[i] + "...");
                    try
                    {
                        SerialPort serialPort;
                        InputStream inStream;
                        OutputStream outStream;
                        int c;
                        String response;
                        serialPort = (SerialPort) portId.open("SMSLibCommTester", 1971);
                        serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN);
                        serialPort.setSerialPortParams(bauds[i], SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
                        inStream = serialPort.getInputStream();
                        outStream = serialPort.getOutputStream();
                        serialPort.enableReceiveTimeout(1000);
                        c = inStream.read();
                        while (c != -1)
                            c = inStream.read();
                        outStream.write('A');
                        outStream.write('T');
                        outStream.write('/r');
                        try
                        {
                            Thread.sleep(1000);
                        }
                        catch (Exception e)
                        {
                        }
                        response = "";
                        c = inStream.read();
                        while (c != -1)
                        {
                            response += (char) c;
                            c = inStream.read();
                        }
                        if (response.indexOf("OK") >= 0)
                        {
                            try
                            {
                                System.out.print("  獲取設備信息...");
                                outStream.write('A');
                                outStream.write('T');
                                outStream.write('+');
                                outStream.write('C');
                                outStream.write('G');
                                outStream.write('M');
                                outStream.write('M');
                                outStream.write('/r');
                                response = "";
                                c = inStream.read();
                                while (c != -1)
                                {
                                    response += (char) c;
                                    c = inStream.read();
                                }
                                System.out.println("  發現設備: " + response.replaceAll("http://s+OK//s+", "").replaceAll("/n", "").replaceAll("/r", ""));
                            }
                            catch (Exception e)
                            {
                                System.out.println("  沒有發現設備!");
                            }
                        }
                        else System.out.println("  沒有發現設備!");
                        serialPort.close();
                    }
                    catch (Exception e)
                    {
                        System.out.println("  沒有發現設備!");
                    }
                }
            }
        }
    }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 贵定县| 武宣县| 横山县| 博爱县| 于田县| 富民县| 阳东县| 兴义市| 阿巴嘎旗| 谷城县| 新巴尔虎左旗| 安多县| 崇礼县| 庆安县| 南安市| 电白县| 江安县| 平武县| 大田县| 凌源市| 吐鲁番市| 微山县| 开封市| 调兵山市| 镇远县| 灵璧县| 汾西县| 邛崃市| 泊头市| 凤庆县| 锡林郭勒盟| 沛县| 东方市| 汉源县| 平武县| 盖州市| 台山市| 胶州市| 海原县| 溧水县| 封丘县|