发布于 2016-07-13 07:38:57 | 138 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Java并发编程示例,程序狗速度看过来!

Java程序设计语言

java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE(j2ee), JavaME(j2me), JavaSE(j2se))的总称。


这篇文章主要介绍了java使用短信设备发送sms短信的示例(java发送短信),需要的朋友可以参考下


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("\\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("  没有发现设备!");
                    }
                }
            }
        }
    }
}



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务