Exemple #1
0
  protected void onSerialPortEvent(SerialPortEvent event) {

    if (event.isRXCHAR()) {

      int count = event.getEventValue();

      if (count == 0) return;

      byte[] characters;
      try {

        characters = serialPort.readBytes(count);

        for (byte character : characters) {
          // System.out.println(Integer.toHexString(character) + "\t:\t" + (int) character + "\t:\t"
          // + (char) character);
          onReceiveChar((char) character);
        }

      } catch (SerialPortException e) {
        showErrorMessage("No se pudo leer un byte");
        e.printStackTrace();
      }
    }
  }
Exemple #2
0
  public void transmitMessage(String message) {

    if (serialPort == null) {
      showErrorMessage("Por favor seleccione un puerto");
      return;
    }

    if (message.isEmpty()) {
      showErrorMessage("No se envian mensajes vacios");
      return;
    }

    String text = message + '\n';

    showSentMessage(text);

    try {
      serialPort.writeBytes(text.getBytes());

    } catch (SerialPortException e) {
      showErrorMessage(e.getMessage());
      e.printStackTrace();
    }

    textField.setText("");
  }
Exemple #3
0
  public void onSerialPortClicked(String portName) {

    closePort();
    try {

      openPort(portName);

    } catch (SerialPortException e) {
      e.printStackTrace();
      showErrorMessage(e.getMessage());
    }
  }
Exemple #4
0
  public void closePort() {

    if (serialPort != null) {

      try {

        serialPort.closePort();

      } catch (SerialPortException e) {
        e.printStackTrace();
      }
    }
  }