title: Serial Port Two Way Communication Java Comm API
body: <p>Hello Guys i have got following Problem.
  I have got a scanner which I am adressing with commands and receive some lines.
  At The moment i tried so far with hyperterminal which works perfectly. But now i need those lines in my Programme so I set up Java Comm API and RXTX ( just because i couldnt bring it to work with Comm API). </p>
  
  <p>I already read a lot in forums but I couldnt bring it to work. </p>
  
  <p>I mean there are only 3 Parts.
  First I set up the Port, InputStream and OutputStream which works fine.</p>
  
  <pre><code>    portList = CommPortIdentifier.getPortIdentifiers();
      int i;
      while (portList.hasMoreElements()) {
          portId = (CommPortIdentifier) portList.nextElement();
          if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                   if (portId.getName().equals("COM3")) {
             // if (portId.getName().equals("/dev/term/a")) {
  
      try {
                      serialPort = (SerialPort)
                          portId.open("SimpleWriteApp", 2000);
                  } catch (PortInUseException e) {System.out.println(e);}
                  try {
                      outputStream = serialPort.getOutputStream();
                      inputStream = serialPort.getInputStream();
                  } catch (IOException e) {System.out.println(e);}
                  try {
                      serialPort.setSerialPortParams(9600,
                          SerialPort.DATABITS_8,
                          SerialPort.STOPBITS_1,
                          SerialPort.PARITY_NONE);
                      serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_XONXOFF_IN);
  
                  } catch (UnsupportedCommOperationException e) {System.out.println(e);}
  </code></pre>
  
  <p>And Then i Want to Send a Command with outputStream.write("xYZ".getByteS());
  which should also work I assume.
  But then i cant get the response back.</p>
  
  <p>The Response should look like this
  02349234235883
  NOK
  But it just stucks.
  The Code looks like this</p>
  
  <pre><code>                            try {               
                      System.out.println(messageString);
                      outputStream.write(messageString.getBytes());
                      BufferedReader portReader = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
                      String line = portReader.readLine();
                      System.out.println(line);
  
  
                      if (inputStream != null) inputStream.close();
                      if (outputStream != null) outputStream.close();
                      if (serialPort != null) serialPort.close();
                  } catch (IOException e) {System.out.println(e);}    
  </code></pre>
  
  <p>can somebody help me?
  Thank u very much for ur efforts</p>