Beispiel #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();
      }
    }
  }
Beispiel #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("");
  }
Beispiel #3
0
  public boolean openPort(String portName) throws SerialPortException {

    serialPort = new SerialPort(portName);
    if (!serialPort.openPort()) {
      return false;
    }

    if (!serialPort.setParams(
        SerialPort.BAUDRATE_9600,
        SerialPort.DATABITS_8,
        SerialPort.STOPBITS_1,
        SerialPort.PARITY_NONE)) {
      return false;
    }

    serialPort.addEventListener(this::onSerialPortEvent, SerialPort.MASK_RXCHAR);

    return true;
  }
Beispiel #4
0
  public void closePort() {

    if (serialPort != null) {

      try {

        serialPort.closePort();

      } catch (SerialPortException e) {
        e.printStackTrace();
      }
    }
  }
Beispiel #5
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();
    }
  }
Beispiel #6
0
  // open a serial connection to a device.  We won't know it's the robot until
  public int OpenPort(String portName) {
    if (portOpened && portName.equals(recentPort)) return 0;

    ClosePort();

    Log("<font color='green'>Connecting to " + portName + "...</font>\n");

    // find the port
    try {
      portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
    } catch (Exception e) {
      Log("<span style='color:red'>Ports could not be identified:" + e.getMessage() + "</span>\n");
      e.printStackTrace();
      return 1;
    }

    if (portIdentifier.isCurrentlyOwned()) {
      Log(
          "<span style='color:red'>Error: Another program is currently using this port."
              + "</span>\n");
      return 2;
    }

    // open the port
    try {
      commPort = portIdentifier.open("DrawbotGUI", 2000);
    } catch (Exception e) {
      Log("Port could not be opened:" + e.getMessage() + NL);
      e.printStackTrace();
      return 3;
    }

    if ((commPort instanceof SerialPort) == false) {
      Log("<span style='color:red'>Only serial ports are handled by this example." + "</span>\n");
      return 4;
    }

    // set the port parameters (like baud rate)
    serialPort = (SerialPort) commPort;
    try {
      serialPort.setSerialPortParams(
          BAUD_RATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
    } catch (Exception e) {
      Log("<span style='color:red'>Port could not be configured:" + e.getMessage() + "</span>\n");
      return 5;
    }

    try {
      in = serialPort.getInputStream();
      out = serialPort.getOutputStream();
    } catch (Exception e) {
      Log("<span style='color:red'>Streams could not be opened:" + e.getMessage() + "</span>\n");
      return 6;
    }

    try {
      serialPort.addEventListener(this);
      serialPort.notifyOnDataAvailable(true);
    } catch (TooManyListenersException e) {
      Log("<span style='color:red'>Streams could not be opened:" + e.getMessage() + "</span>\n");
      return 7;
    }

    Log("<span style='green'>Opened.</span>\n");
    SetRecentPort(portName);
    portOpened = true;
    UpdateMenuBar();

    return 0;
  }