title: Open and Close Serial Ports
body: <p>I am trying to connect to a <a href="http://en.wikipedia.org/wiki/Serial_port" rel="nofollow">Serial Port</a> ... but once I open the <code>Serial Port</code> in the first time. I can't open it again, I've tried to apply. Here is my code:</p>
  
  <pre><code>public static void main(String[] args) {
      portList = CommPortIdentifier.getPortIdentifiers();
      while (portList.hasMoreElements()) {
          portId = (CommPortIdentifier) portList.nextElement();
          if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
               if (portId.getName().equals("COM1")) {
                  try {
                      serialPort = (SerialPort)
                          portId.open("SimpleWriteApp", 2000);
                  } catch (PortInUseException e) {}
                  try {
                      outputStream = serialPort.getOutputStream();
                  } catch (IOException e) {}
                  try {
                      serialPort.setSerialPortParams(9600,
                          SerialPort.DATABITS_8,
                          SerialPort.STOPBITS_1,
                          SerialPort.PARITY_NONE);
                  } catch (UnsupportedCommOperationException e) {}
                  try {
                      outputStream.write(messageString.getBytes());
                  } catch (IOException e) {}
              }
          }
      }
  }
  </code></pre>
  
  <p>I want to close that port so I can use it for another task.</p>