2011-12-23 7 views
1

携帯電話番号にテキストメッセージを送信しています。私はo/pストリームを使ってデータを書き、ポートからi/pストリームを使ってデータを読みます。 o/pストリームは正常に動作していますが、i/pストリームからデータを読み取ることができません。ここに私のコードは次のとおりです。ComポートInputStream

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."); 
    } 



} 
+0

入力ポートから読み込むとどうなりますか? –

+0

これは出力が0であることを示しています。これはそのSystem.out.println(inputStream.available())のコードです。 while(inputStream.available()> 0){ int n = inputStream.read(b); System.out.println( "バイト数" + n); r =新しい文字列(b); } – Naresh

+0

出力ストリームを書き込んだ後でそれをフラッシュすると役立ちますか?また、1つのスレッドですべてを実行しているため、書き込みがブロックされる可能性がある場合は、プログラムはフリーズします。私はフラッシュを使用しています –

答えて

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(..)メソッドは、入力ストリームのデータを受け取った後にポートを閉じることができます。

希望すると便利です。

+0

注:バイト配列への読み込みはより効率的かつ適切ですが、主なものはserialEventが起動するのを待つことです。 – laher

+0

ありがとうございました。 – Naresh