2011-12-23 4 views
1

휴대폰 번호로 문자 메시지를 보내고 있습니다. 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."); 
    } 



} 
+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

출력 스트림을 쓰고 난 후 플러시하면 도움이됩니까? 또한 하나의 스레드에서 모든 것을 실행 중이므로 쓰기가 프로그램을 차단할 수있는 경우 고정됩니다. 제가 다량 사용하고 –

답변

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

ur 제안을 주셔서 감사합니다. – Naresh