Com端口InputStream

时间:2011-12-23 10:36:18

标签: java io

我正在向手机号码发送短信。我使用o / p流写入数据,并使用来自Port的i / p流读取数据。 o / p流工作正常,但我无法从i / p流中读取数据。这是我的代码:

public class SendMsg implements  SerialPortEventListener
{
    Enumeration portList;
    CommPortIdentifier portId;
    SerialPort serialPort;

    OutputStream outputStream;
    InputStream inputStream;
    Thread readThread;

     String messageString;
     String messageString1;

     String strResponse="";
     SendMsg pWriter;
     String msg[]=new String[200];
     int ix=0;

     boolean msgEnd=true;

    String className;
     static Enumeration ports;
    static CommPortIdentifier pID;
    static String messageToSend = "ComPortSendMsg deatails!\n";


    public SendMsg(String className) throws NoSuchPortException, IOException
    {

            this.className=className;

           ports = CommPortIdentifier.getPortIdentifiers();
         System.out.println("ports name"+ports);
        while(ports.hasMoreElements())
        {
            pID = (CommPortIdentifier)ports.nextElement();
            System.out.println("Port Name " + pID.getName());

            if (pID.getPortType() == CommPortIdentifier.PORT_SERIAL)
            {
                System.out.println("Port Name 1 " + pID.getName());
                if (pID.getName().equals("COM1"))
                {
                    try {
                        System.out.println("Port Name 2 " + pID.getName());
                         System.out.println("COM1 found");
                         serialPort=(SerialPort)pID.open(className, 9600);

                         outputStream=serialPort.getOutputStream();
                         inputStream=serialPort.getInputStream();
                         break;
                    } catch (PortInUseException ex) {
                        Logger.getLogger(SendMsg.class.getName()).log(Level.SEVERE, null, ex);
                    }


                }
            }
        }
}

      public void closePort()
    {
        try
        {
            inputStream.close();
            System.out.println("Finished2");
            outputStream.close();
            System.out.println("Finished1");

            serialPort.close();
            System.out.println("Finished");


        }
        catch(Exception e)
        {
            System.out.println("Close Error"+e);
        }


    }


    public void send(String phno,String msg)
    {
String s = "AT+CMGF="+1;
System.out.println("AT+CMGF command :"+s);

       messageString = "AT+CMGS=\""+phno+"\"\r";
        messageString1 = msg+"\n" +(char)26;
        System.out.println("AT CMGS "+messageString);
        System.out.println("AT CMGS  "+messageString1);


        try
        {
            outputStream.write(s.getBytes());

           System.out.print("this is send try block");
         outputStream.write(messageString.getBytes());

             outputStream.write(messageString1.getBytes());

               Thread.sleep(2000);

     byte[] b = new byte[1000];
     String r="";
     String r1="";

     System.out.println(inputStream.available());
     while (inputStream.available() > 0) {
                    int n = inputStream.read(b);
                    System.out.println("number of bytes"+n);
                    r= new String(b);

     }
     System.out.println("this is input stream msg"+r);
        }
         catch (Exception e)
         {
           System.out.println(e);
          }

    }
public static void main(String args[]) throws NoSuchPortException, IOException
        {

            SendMsg f=new SendMsg("Msg Sending");
            f.send("9884345649","Wish U Happy");
 System.out.println("---------END--------");





            f.closePort();


        }

    @Override
    public void serialEvent(SerialPortEvent spe) {
        throw new UnsupportedOperationException("Not supported yet.");
    }



}

1 个答案:

答案 0 :(得分:0)

您应该从serialEvent(..)方法中读取InputStream。

这样的事情:

public void serialEvent(SerialPortEvent spe) {
        int data;
        String r;
        try
        {
            int len = 0;
            while ( ( data = in.read()) > -1 )
            {
                buffer[len++] = (byte) data;
            }
            r = new String(buffer,0,len);
            System.out.println("this is input stream msg"+r);
        }
        catch ( IOException e )
        {
            e.printStackTrace();
            System.exit(-1);
        }
}

其次,你可以长时间睡觉,例如Thread.sleep(100000);,在主要方法中,之前 致电f.closePort();

或者,serialEvent(..)方法可能会在收到inputStream的数据后关闭端口。

希望这有帮助!