title: Reading from COM port in Java, Error 0x5 at ..\rxtx\src\termios.c(892)
body: <p>I'm writing a small app in Java to read from <a href="http://en.wikipedia.org/wiki/COM_%28hardware_interface%29" rel="noreferrer">COM</a> port, and since we use 64 bit systems I had to use RXTX. The problem is when I try to run my app I get the following error:</p>
  
  <blockquote>
    <p>"Error 0x5 at ..\rxtx\src\termios.c(892):Access Denied"</p>
  </blockquote>
  
  <p>Tried my code and also the <a href="https://web.archive.org/web/20121227101123/http://rxtx.qbang.org/wiki/index.php/Two_way_communcation_with_the_serial_port" rel="noreferrer">code</a> from RXTX site, anyone had any experience with this before?</p>
  
  <hr>
  
  <p>Here is my code:</p>
  
  <pre><code>import gnu.io.CommPort;
  import gnu.io.CommPortIdentifier;
  import gnu.io.SerialPort;
  import gnu.io.SerialPortEvent;
  import gnu.io.SerialPortEventListener;
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  
  /**
   * This version of the TwoWaySerialComm example makes use of the 
   * SerialPortEventListener to avoid polling.
   *
   */
  public class TwoWaySerialComm
  {
      public TwoWaySerialComm()
      {
          super();
      }
  
      void connect ( String portName ) throws Exception
      {
          CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
          if ( portIdentifier.isCurrentlyOwned() )
          {
              System.out.println("Error: Port is currently in use");
          }
          else
          {
              CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
  
              if ( commPort instanceof SerialPort )
              {
                  SerialPort serialPort = (SerialPort) commPort;
                  serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
  
                  InputStream in = serialPort.getInputStream();
  //                OutputStream out = serialPort.getOutputStream();
  //                               
  //                (new Thread(new SerialWriter(out))).start();
  
                  serialPort.addEventListener(new SerialReader(in));
                  serialPort.notifyOnDataAvailable(true);
  
              }
              else
              {
                  System.out.println("Not a serial port...");
              }
          }     
      }
  
      public static class SerialReader implements SerialPortEventListener 
      {
          private InputStream in;
          private byte[] buffer = new byte[1024];
  
          public SerialReader ( InputStream in )
          {
              this.in = in;
          }
  
          public void serialEvent(SerialPortEvent arg0) {
              int data;
  
              try
              {
                  int len = 0;
                  while ( ( data = in.read()) &gt; -1 )
                  {
                      if ( data == '\n' ) {
                          break;
                      }
                      buffer[len++] = (byte) data;
                  }
                  System.out.print("Result="+new String(buffer,0,len));
              }
              catch ( IOException e )
              {
                  e.printStackTrace();
                  System.exit(-1);
              }             
          }
  
      }
  
      public static void main ( String[] args )
      {
          try
          {
              (new TwoWaySerialComm()).connect("COM1");
          }
          catch ( Exception e )
          {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  }
  </code></pre>