Example #1
0
  public TestSer(String port) {

    try {
      portId = CommPortIdentifier.getPortIdentifier(port);
      serialPort = (SerialPort) portId.open("TestSer", 2000);
      is = serialPort.getInputStream();
      os = serialPort.getOutputStream();
      /*
      			serialPort.addEventListener(this);
      			serialPort.notifyOnDataAvailable(true);
      */

      serialPort.setSerialPortParams(
          115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

      serialPort.setFlowControlMode(
          SerialPort.FLOWCONTROL_RTSCTS_OUT | SerialPort.FLOWCONTROL_RTSCTS_IN);

      serialPort.enableReceiveTimeout(TIMEOUT);
      //			serialPort.enableReceiveThreshold(4);

    } catch (Exception e) {
      System.out.println(e.getMessage());
      System.exit(-1);
    }
  }
Example #2
0
  public void open()
      throws NoSuchPortException, PortInUseException, IOException,
          UnsupportedCommOperationException {
    portId = CommPortIdentifier.getPortIdentifier(portName);
    port = (SerialPort) portId.open(CLASS_NAME, 0);
    in = port.getInputStream();
    out = port.getOutputStream();

    port.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN);
    port.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_OUT);

    printPortStatus();
    port.setSerialPortParams(
        19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
    printPortStatus();
  }
  /** @param data */
  @Override
  protected void internalWrite(byte[] data) {
    try {
      if (m_out == null) {
        m_PortIdPrinter =
            CommPortIdentifier.getPortIdentifier(
                m_sPortPrinter); // Tomamos el puerto
        m_CommPortPrinter = m_PortIdPrinter.open("PORTID", 2000); // Abrimos el puerto

        m_out = m_CommPortPrinter.getOutputStream(); // Tomamos el chorro de escritura

        if (m_PortIdPrinter.getPortType() == CommPortIdentifier.PORT_SERIAL) {
          ((SerialPort) m_CommPortPrinter)
              .setSerialPortParams(
                  9600,
                  SerialPort.DATABITS_8,
                  SerialPort.STOPBITS_1,
                  SerialPort.PARITY_NONE); // Configuramos el puerto
          ((SerialPort) m_CommPortPrinter)
              .setFlowControlMode(
                  SerialPort
                      .FLOWCONTROL_RTSCTS_IN); // this line prevents the printer tmu220 to stop
                                               // printing after +-18 lines printed
          // this line prevents the printer tmu220 to stop printing after +-18 lines printed. Bug
          // 8324
          // But if added a regression error appears. Bug 9417, Better to keep it commented.
          // ((SerialPort)m_CommPortPrinter).setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN);
          // Not needed to set parallel properties
          //                } else if (m_PortIdPrinter.getPortType() ==
          // CommPortIdentifier.PORT_PARALLEL) {
          //                    ((ParallelPort)m_CommPortPrinter).setMode(1);

        }
      }
      m_out.write(data);
      // JG 16 May 12 use multicatch
    } catch (NoSuchPortException
        | PortInUseException
        | UnsupportedCommOperationException
        | IOException e) {
      System.err.println(e);
    }
  }
Example #4
0
  /**
   * Sets the connection parameters to the setting in the parameters object. If set fails return the
   * parameters object to origional settings and throw exception.
   *
   * @throws Exception if the configured parameters cannot be set properly on the port.
   */
  public void setConnectionParameters() throws Exception {

    // Save state of parameters before trying a set.
    int oldBaudRate = m_SerialPort.getBaudRate();
    int oldDatabits = m_SerialPort.getDataBits();
    int oldStopbits = m_SerialPort.getStopBits();
    int oldParity = m_SerialPort.getParity();
    int oldFlowControl = m_SerialPort.getFlowControlMode();

    // Set connection parameters, if set fails return parameters object
    // to original state.
    try {
      m_SerialPort.setSerialPortParams(
          m_Parameters.getBaudRate(),
          m_Parameters.getDatabits(),
          m_Parameters.getStopbits(),
          m_Parameters.getParity());
    } catch (UnsupportedCommOperationException e) {
      m_Parameters.setBaudRate(oldBaudRate);
      m_Parameters.setDatabits(oldDatabits);
      m_Parameters.setStopbits(oldStopbits);
      m_Parameters.setParity(oldParity);
      final String errMsg = "Unsupported parameter";
      logger.debug(
          "{} failed to set up one of [baudRate, dataBits, stopBits, parity]: {}",
          errMsg,
          e.getMessage());

      throw new Exception(errMsg);
    }

    // Set flow control.
    try {
      m_SerialPort.setFlowControlMode(
          m_Parameters.getFlowControlIn() | m_Parameters.getFlowControlOut());
    } catch (UnsupportedCommOperationException e) {
      final String errMsg = "Unsupported flow control";
      logger.debug("{}: {}", errMsg, e.getMessage());

      throw new Exception(errMsg);
    }
  } // setConnectionParameters