Example #1
0
 @NotNull
 private List<String> findAllAvailablePorts() {
   List<String> ports = new ArrayList<>();
   ports.addAll(Arrays.asList(SerialPortList.getPortNames()));
   ports.addAll(TcpConnector.getAvailablePorts());
   return ports;
 }
Example #2
0
 /**
  * Returns the first available port
  *
  * @return If none is available returns NULL
  */
 public static String getFirstAvailable() {
   String[] portNames = SerialPortList.getPortNames();
   if (portNames != null && portNames.length > 0) {
     for (String port : portNames) {
       if (!port.contains("rfcomm0")) {
         return port;
       }
     }
     return null;
   }
   return null;
 }
Example #3
0
  public void refreshSerialPortList() {

    String[] serialPortList = SerialPortList.getPortNames();

    portMenu.removeAll();

    portMenu.add(refreshItem);

    JMenuItem menuItem;
    for (String serialPort : serialPortList) {

      menuItem = new JMenuItem(serialPort);
      menuItem.addActionListener(e -> onSerialPortClicked(e.getActionCommand()));
      portMenu.add(menuItem);
    }
  }
  private void initialize() {
    String[] portNames = SerialPortList.getPortNames();
    _logger.debug("Number of serial port available:{}", portNames.length);
    for (int portNo = 0; portNo < portNames.length; portNo++) {
      _logger.debug("SerialPortJson[{}]:{}", portNo + 1, portNames[portNo]);
    }

    // create an instance of the serial communications class
    serialPort = new SerialPort(gateway.getPortName());
    try {
      serialPort.openPort(); // Open port
      serialPort.setParams(
          gateway.getBaudRate(),
          SerialPort.STOPBITS_1,
          SerialPort.PARITY_NONE,
          SerialPort.DATABITS_8);
      int mask =
          SerialPort.MASK_RXCHAR
              + SerialPort.MASK_CTS
              + SerialPort.MASK_DSR
              + SerialPort.MASK_ERR; // Prepare mask
      serialPort.setEventsMask(mask); // Set mask
      // create and register the serial data listener
      serialPort.addEventListener(
          new SerialDataListenerJssc(serialPort, gateway)); // Add SerialPortEventListener
      _logger.debug("Serial port gateway initialized, GatewayTable[{}]", gateway);
      gateway.setStatus(STATE.UP, "Connected Successfully");
    } catch (SerialPortException ex) {
      gateway.setStatus(STATE.DOWN, "ERROR: " + ex.getMessage());
      if (ex.getMessage().contains("Port not found")) {
        _logger.error("Failed to load serial port: {}", ex.getMessage());
      } else {
        _logger.error("Failed to load serial port, ", ex);
      }
    }
  }
Example #5
0
 /**
  * @generate Serial_list.xml
  * @webref serial
  * @usage web_application
  */
 public static String[] list() {
   // returns list sorted alphabetically, thus cu.* comes before tty.*
   // this was different with RXTX
   return SerialPortList.getPortNames();
 }
Example #6
0
 public static Map<String, String> getProperties(String portName) {
   return SerialPortList.getPortProperties(portName);
 }
Example #7
0
 public static List<String> listAvailablePortNames() {
   String[] portNames = SerialPortList.getPortNames();
   return Arrays.asList(portNames);
 }
Example #8
0
  /**
   * Connects to a known port. Delays for 2s because experience shows that receiving of data only
   * works after a certain time.
   *
   * @param baudRate Value from jssc.SerialPort.BAUDRATE_*
   * @throws DisplayException
   * @throws SerialPortException
   * @throws InterruptedException
   * @throws IOException
   * @throws SecurityException
   */
  public SerialTransport(int baudRate)
      throws DisplayException, SerialPortException, InterruptedException, SecurityException,
          IOException {
    // -------- Initialize Logger --------
    logger = Logger.getLogger("com.activeantiglare.arduino.sp");
    FileHandler fh = new FileHandler("SerialTransport.log");
    fh.setFormatter(new CmdFormatter());

    logger.addHandler(fh);
    logger.setLevel(Level.ALL);

    logger.info("SerialTransport constructor start");

    // -------- Find port --------
    String[] portNames = SerialPortList.getPortNames();
    for (int i = 0; i < portNames.length; i++) {
      String currentName = portNames[i];
      for (int j = 0; j < PORT_NAMES.length; j++) {
        if (currentName.equals(PORT_NAMES[j])) {
          portName = currentName;
          break;
        }
      }
      if (portName != null) {
        break;
      }
    }

    if (portName == null) {
      String msg = "ERROR: No known serial port found";
      logger.log(Level.SEVERE, msg);
      throw new DisplayException(msg);
    } else {
      String msg = "Connecting to port " + portName;
      logger.log(Level.INFO, msg);
      System.out.println(msg);
    }

    // -------- Initialize Port --------
    serialPort = new SerialPort(portName);
    try {
      serialPort.openPort(); // Open serial port
      serialPort.setParams(
          baudRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

      int mask = SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR; // Prepare mask
      serialPort.setEventsMask(mask); // Set mask
      serialPort.addEventListener(new SerialPortReader()); // Add
      // SerialPortEventListener
    } catch (SerialPortException e) {
      logger.log(Level.SEVERE, "", e);
      e.printStackTrace(System.out);
      throw e;
    }

    // No one knows why, but receiving only works reliably after some time
    try {
      Thread.sleep(3000);
    } catch (InterruptedException e) {
      throw e;
    }

    logger.info("SerialTransport constructor end");
  }