欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java使用短信设备发送sms短信的示例(java发送短信)

程序员文章站 2024-02-26 10:48:46
复制代码 代码如下:import gnu.io.*;import java.util.*;import java.io.*; public class commtest{...

复制代码 代码如下:

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