Beispiel #1
0
  // connect to the selected port in the combo box
  // pre: ports are already found by using the searchForPorts method
  // post: the connected comm port is stored in commPort, otherwise,
  // an exception is generated
  public void connect() {
    //        String selectedPort = (String)window.cboxPorts.getSelectedItem();
    /** Setting 에서 Comport 불러오는 걸로 수정 * */
    // String selectedPort = "COM4";
    selectedPortIdentifier = (CommPortIdentifier) portMap.get(selectedPort);

    CommPort commPort = null;

    try {
      // the method below returns an object of type CommPort
      commPort = selectedPortIdentifier.open("TigerControlPanel", TIMEOUT);
      // the CommPort object can be casted to a SerialPort object
      serialPort = (SerialPort) commPort;

      // for controlling GUI elements
      setConnected(true);

      // logging
      logText = selectedPort + " opened successfully.";
      System.out.println(logText + "\n");

      // CODE ON SETTING BAUD RATE ETC OMITTED
      // XBEE PAIR ASSUMED TO HAVE SAME SETTINGS ALREADY
    } catch (PortInUseException e) {
      logText = selectedPort + " is in use. (" + e.toString() + ")";
      System.out.println(logText + "\n");
    } catch (Exception e) {
      logText = "Failed to open " + selectedPort + "(" + e.toString() + ")";
      System.out.println(logText + "\n");
    }
  }
Beispiel #2
0
  public static Serial open(String defaultPort) {
    boolean portFound = false;
    final int mComPortIdentifier = CommPortIdentifier.PORT_SERIAL;
    final Enumeration portList = CommPortIdentifier.getPortIdentifiers();
    final int BAUD = 115200;

    while (portList.hasMoreElements()) {
      final CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
      System.out.println("Found port id: " + portId);

      if (portId.getPortType() == mComPortIdentifier) {
        System.out.println("Found CommPortIdentifier.");

        if (portId.getName().equals(defaultPort)) {
          System.out.println("Found port " + defaultPort);

          SerialPort serialPort;
          OutputStream outputStream = null;

          try {
            serialPort = (SerialPort) portId.open("SimpleWrite", 2000);
          } catch (PortInUseException e) {
            System.out.println("Port in use.");
            continue;
          }

          try {
            outputStream = serialPort.getOutputStream();
          } catch (IOException e) {
            e.printStackTrace();
          }

          try {
            serialPort.setSerialPortParams(
                BAUD, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
          } catch (UnsupportedCommOperationException e) {
            e.printStackTrace();
          }

          try {
            serialPort.notifyOnOutputEmpty(true);
          } catch (Exception e) {
            System.out.println("Error setting event notification");
            System.out.println(e.toString());
            System.exit(-1);
          }

          return new Serial(serialPort, outputStream);
        }
      }
    }

    if (!portFound) {
      System.out.println("port " + defaultPort + " not found.");
    }
    return null;
  }
Beispiel #3
0
 // method that can be called to send data
 // pre: open serial port
 // post: data sent to the other device
 public void writeData(int code) {
   try {
     output.write(code);
     output.flush();
     System.out.println("code : " + code);
   } catch (Exception e) {
     logText = "Failed to write data. (" + e.toString() + ")";
     System.out.println(logText);
   }
 }
Beispiel #4
0
 // what happens when data is received
 // pre: serial event is triggered
 // post: processing on the data it reads
 public void serialEvent(SerialPortEvent evt) {
   if (evt.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
     try {
       int readData = input.read();
       System.out.println(readData + "@@@\n");
       if (readData == 51) // 소변통 (Water Sensor)
       {
         Action.SendUrineMessage();
       }
     } catch (Exception e) {
       logText = "Failed to read data. (" + e.toString() + ")";
       System.out.println(logText + "\n");
     }
   }
 }
Beispiel #5
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");
   }
 }