Example #1
0
 private synchronized void closeSerialPort() {
   if (serialPort != null) {
     serialPort.removeEventListener();
     serialPort.close();
     serialPort = null;
   }
 }
Example #2
0
 @Override
 public void stopPolling() {
   if (_comPort != null && _usable && _isQuestionSupported) {
     ((SerialPort) _comPort).removeEventListener();
     _comPort.close();
   }
 }
Example #3
0
  /**
   * End call.
   *
   * @throws Exception the exception
   */
  private void endCall() throws Exception {
    this.incomingCall = false;
    if (serialVoiceReader != null) {
      serialVoiceReader.terminate();
      serialVoiceReader = null;
    }
    if (serialVoiceWriter != null) {
      serialVoiceWriter.terminate();
      serialVoiceWriter = null;
    }
    if (voiceInputStream != null) {
      voiceInputStream.close();
    }
    if (voiceOutputStream != null) {
      voiceOutputStream.close();
    }
    if (voiceStreamCommPort != null) {
      voiceStreamCommPort.notifyOnDataAvailable(false);
      voiceStreamCommPort.removeEventListener();
      voiceStreamCommPort.close();
      voiceStreamCommPort = null;
    }
    // Thread jsed = Utils.getThread("Java Sound Event Dispatcher");

  }
 public void disconnect() throws Exception {
   if (is != null) is.close();
   if (os != null) os.close();
   if (serialPort != null) {
     serialPort.removeEventListener();
     serialPort.close();
   }
 }
 public synchronized void close() {
   try {
     in.close();
     output.close();
   } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
   if (serialPort != null) {
     serialPort.removeEventListener();
     serialPort.close();
   }
 }
Example #6
0
 // disconnect the serial port
 // pre: an open serial port
 // post: clsoed serial port
 public void disconnect() {
   // close the serial port
   try {
     //            writeData(0, 0);
     serialPort.removeEventListener();
     serialPort.close();
     input.close();
     output.close();
     setConnected(false);
     logText = "Disconnected.";
     System.out.println(logText + "\n");
   } catch (Exception e) {
     logText = "Failed to close " + serialPort.getName() + "(" + e.toString() + ")";
     System.out.println(logText + "\n");
   }
 }
Example #7
0
 /** Terminate. */
 public void terminate() {
   try {
     endCall();
     if (commandOutputStream != null) {
       commandOutputStream.close();
     }
     if (commandInputStream != null) {
       commandInputStream.close();
     }
     if (modemComPort != null) {
       modemComPort.notifyOnDataAvailable(false);
       modemComPort.removeEventListener();
       modemComPort.close();
       modemComPort = null;
     }
   } catch (Exception e) {
     logger.error(e.getMessage(), e);
   }
 }
Example #8
0
 /*
  * Code cribbed from RXTX example
  */
 public static void closePort(SerialPort serialPort) {
   if (serialPort != null) {
     serialPort.notifyOnDataAvailable(false);
     serialPort.removeEventListener();
     if (inputStream != null) {
       try {
         inputStream.close();
         inputStream = null;
       } catch (IOException e) {
       }
     }
     if (outputStream != null) {
       try {
         outputStream.close();
         outputStream = null;
       } catch (IOException e) {
       }
     }
     serialPort.close();
     serialPort = null;
   }
 }
Example #9
0
  public void ClosePort() {
    if (portOpened) {
      if (serialPort != null) {
        try {
          // Close the I/O streams.
          out.close();
          in.close();
          // Close the port.
          serialPort.removeEventListener();
          serialPort.close();
        } catch (IOException e) {
          // Don't care
        }
      }

      ClearLog();
      portOpened = false;
      portConfirmed = false;
      previewPane.setConnected(false);
      UpdateMenuBar();
    }
  }
 /**
  * This should be called when you stop using the port. This will prevent port locking on platforms
  * like Linux.
  */
 public synchronized void closeSerial() {
   if (serialPort != null) {
     serialPort.removeEventListener();
     serialPort.close();
   }
 }
Example #11
0
 /** Close this serial device */
 public void close() {
   serialPort.removeEventListener();
   IOUtils.closeQuietly(inputStream);
   IOUtils.closeQuietly(outputStream);
   serialPort.close();
 }
Example #12
0
  /**
   * Sends a string to the serial port of this device. The writing of the msg is executed
   * synchronized, so it's guaranteed that the device doesn't get multiple messages concurrently.
   *
   * @param msg the string to send
   * @return true, if the message has been transmitted successfully, otherwise false.
   */
  synchronized boolean writeString(final String msg) {
    logger.debug("Writing '{}' to serial port {}", msg, port);

    final long earliestNextExecution = lastCommandTime + interval;
    while (earliestNextExecution > System.currentTimeMillis()) {
      try {
        Thread.sleep(100);
      } catch (InterruptedException ex) {
        return false;
      }
    }
    try {
      final List<Boolean> listenerResult = new ArrayList<Boolean>();
      serialPort.addEventListener(
          new SerialPortEventListener() {
            @Override
            public void serialEvent(SerialPortEvent event) {
              if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
                // we get here if data has been received
                final StringBuilder sb = new StringBuilder();
                final byte[] readBuffer = new byte[20];
                try {
                  do {
                    // read data from serial device
                    while (inputStream.available() > 0) {
                      final int bytes = inputStream.read(readBuffer);
                      sb.append(new String(readBuffer, 0, bytes));
                    }
                    try {
                      // add wait states around reading the stream, so that interrupted
                      // transmissions are
                      // merged
                      Thread.sleep(100);
                    } catch (InterruptedException e) {
                      // ignore interruption
                    }
                  } while (inputStream.available() > 0);
                  final String result = sb.toString();
                  if (result.equals(msg)) {
                    listenerResult.add(true);
                  }
                } catch (IOException e) {
                  logger.debug("Error receiving data on serial port {}: {}", port, e.getMessage());
                }
              }
            }
          });
      serialPort.notifyOnDataAvailable(true);
      outputStream.write(msg.getBytes());
      outputStream.flush();
      lastCommandTime = System.currentTimeMillis();
      final long timeout = lastCommandTime + 1000;
      while (listenerResult.isEmpty() && System.currentTimeMillis() < timeout) {
        // Waiting for response
        Thread.sleep(100);
      }
      return !listenerResult.isEmpty();
    } catch (Exception e) {
      logger.error("Error writing '{}' to serial port {}: {}", msg, port, e.getMessage());
    } finally {
      serialPort.removeEventListener();
    }
    return false;
  }