예제 #1
0
  public static void main(String[] args) {
    portList = CommPortIdentifier.getPortIdentifiers();

    while (portList.hasMoreElements()) {
      portId = (CommPortIdentifier) portList.nextElement();
      if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
        if (portId.getName().equals("COM17")) {
          // if (portId.getName().equals("/dev/term/a")) {
          try {
            serialPort = (SerialPort) portId.open("SimpleWriteApp", 2000);
          } catch (PortInUseException e) {
          }

          try {
            outputStream = serialPort.getOutputStream();
          } catch (IOException e) {
          }
          try {
            serialPort.setSerialPortParams(
                9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
          } catch (UnsupportedCommOperationException e) {
          }

          try {
            outputStream.write(messageString.getBytes());
          } catch (IOException e) {
          }
        }
      }
    }
  }
  /** Opens the Serial port with the baud and port_name given */
  public boolean OpenSerial(int baud, String port) {
    CommPortIdentifier portId = null;
    Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();

    while (portEnum.hasMoreElements()) {
      CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
      if (currPortId.getName().equals(port)) {
        portId = currPortId;
        break;
      }
    }

    if (portId == null) {
      System.err.println("Can not open serial port");
      return false;
    }

    try {
      serialPort = (SerialPort) portId.open(this.getClass().getName(), 2000);

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

      input = serialPort.getInputStream();
      output = serialPort.getOutputStream();

      serialPort.addEventListener(this);
      serialPort.notifyOnDataAvailable(true);
      Thread.sleep(1500);
    } catch (Exception e) {
      return false;
    }

    return true;
  }
예제 #3
0
  /**
   * This creates an mbed object for an mbed connected over Serial. <br>
   * Using this class requires the Sun Communications API to be installed
   *
   * @param PortName The Serial Port mbed is connected to eg "COM5" on Windows.
   * @param Baud The baud rate
   */
  public SerialRPC(String PortName, int Baud) {
    // open serial port
    try {
      mbedPortID = CommPortIdentifier.getPortIdentifier(PortName);

      mbedPort = mbedPortID.open("mbed", 100000);
      mbedSerialPort = (SerialPort) mbedPort;
      mbedSerialPort.setSerialPortParams(
          Baud, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

      inputStream = new DataInputStream(mbedPort.getInputStream());
      outputStream = new PrintStream(mbedPort.getOutputStream(), true);

      reader = new BufferedReader(new InputStreamReader(inputStream));
      to_mbed = new PrintWriter(outputStream, true);

      mbedSerialPort.addEventListener(this);
      // Important to set this regardless of whether interrupts are in use to keep the buffer clear
      mbedSerialPort.notifyOnDataAvailable(true);

    } catch (TooManyListenersException e) {
      // Error adding event listener
      System.out.println("Too many Event Listeners");
    } catch (NoSuchPortException e) {
      System.out.println("No Such Port");
    } catch (PortInUseException e) {
      System.out.println("Port Already In Use");
    } catch (UnsupportedCommOperationException e) {
      System.out.println("Unsupported Comm Operation");
    } catch (IOException e) {
      System.out.println("Serial Port IOException");
    }
  }
예제 #4
0
  /** {@inheritDoc} */
  public String RPC(String Name, String Method, String[] Args) {
    // write to serial port and receive result
    String Response;
    String Arguments = "";
    if (Args != null) {
      int s = Args.length;
      for (int i = 0; i < s; i++) {
        Arguments = Arguments + " " + Args[i];
      }
    }
    try {
      to_mbed.println("/" + Name + "/" + Method + Arguments);
      mbedSerialPort.notifyOnDataAvailable(false);
    } catch (NullPointerException e) {

    }
    boolean valid = true;
    try {
      while (reader.ready() == false) {}
      ;
      do {
        Response = reader.readLine();

        if (Response.length() >= 1) {
          valid = Response.charAt(0) != '!';
        }
      } while (valid == false);
    } catch (IOException e) {
      System.err.println("IOException, error reading from port");
      Response = "error";
    }

    mbedSerialPort.notifyOnDataAvailable(true);
    return (Response);
  }
 public synchronized void sendEvent(RobotEvent ev) {
   try {
     output.write(ev.toStringSend().getBytes()); // write needs a byte array instead of a string
     long milli = (long) (1.0 / ((float) serialPort.getBaudRate() / (8.0 * 16.0)) * 1000.0);
     long nano =
         (long) (1.0 / ((float) serialPort.getBaudRate() / (8.0 * 16.0)) * 1000000000.0)
             - milli * 1000000;
     Thread.sleep((int) milli, (int) nano);
   } catch (Exception e) {
   }
 }
예제 #6
0
  /*
   * Open the port, wait for chip init
   */
  public static int initialize(String portName) {
    System.out.println("Enumerating serial ports:");
    portList = CommPortIdentifier.getPortIdentifiers();
    while (portList.hasMoreElements()) {
      portId = (CommPortIdentifier) portList.nextElement();
      if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
        // DDT
        System.out.println(portId.getName());

        if (portId.getName().equals(portName)) {
          System.out.println("Name match on port " + portName);
          // Init the port, matches name we were given
          try {
            serialPort = (SerialPort) portId.open("tx-skeleton", 64);
          } catch (PortInUseException e) {
            e.printStackTrace();
            System.out.println("Port in use!");
            return (4);
          }
          try {
            inputStream = serialPort.getInputStream();
            outputStream = serialPort.getOutputStream();
          } catch (IOException e) {
            System.out.println("Unable to connect to I/O streams");
            return (3);
          }

          try {
            System.out.println("Initializing ADXL202 board...");
            // 38400N81
            serialPort.setSerialPortParams(
                38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

            // ADXL202 is powered via DTR line, so set it on
            serialPort.setDTR(true);

            try {
              // Wait a couple of seconds for chip to initialize (blinking LED)
              Thread.sleep(2000);
            } catch (InterruptedException ie) {
            }

          } catch (UnsupportedCommOperationException e) {
            System.out.println("Unable to configure serial port!");
            return (1);
          }
          return (0);
        }
      }
    }

    return (1);
  }
예제 #7
0
  /** Close the serial port. */
  public void delete() {
    // Close the serial port
    try {
      if (inputStream != null) inputStream.close();
      outputStream.close();
    } catch (IOException e) {

    }
    mbedSerialPort.removeEventListener();
    mbedSerialPort.close();
    mbedPort.close();
  }
 private void init(
     SerialPort serialPort, SerialParameters serialParams, SerialTestResultsDisplay results)
     throws SerialConnectionException {
   _parameters = serialParams;
   _sPort = serialPort;
   _results = results;
   _sendThread = new Thread(this);
   _chunkSize = 4096;
   _savedStream = new ByteArrayOutputStream();
   try {
     _os = _sPort.getOutputStream();
   } catch (IOException e) {
     _sPort.close();
     throw new SerialConnectionException("Error opening i/o streams");
   }
 }
예제 #9
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();
  }
예제 #10
0
  public int setUpBam() {
    Enumeration<?> portIdentifiers = CommPortIdentifier.getPortIdentifiers();
    CommPortIdentifier portId = null;

    while (portIdentifiers.hasMoreElements()) {
      CommPortIdentifier pid = (CommPortIdentifier) portIdentifiers.nextElement();

      if (pid.getPortType() == CommPortIdentifier.PORT_SERIAL
          && pid.getName().equals(wantedPortName)) {
        portId = pid;
        System.out.println("Found port: " + pid.getName());
        break;
      }
    }

    try {
      port = (SerialPort) portId.open("Driver", 10000);
    } catch (PortInUseException e) {
      System.err.println("Port already in use: " + e);
      System.exit(1);
    }

    try {
      port.setSerialPortParams(
          57600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
    } catch (UnsupportedCommOperationException e) {
      System.err.println("Failed to set up port: " + e);
    }
    System.out.println("Port should be open now");
    return 0;
  }
 public void connectionclose() {
   try {
     is.close();
     os.close();
     dis.close();
     dos.close();
     sPort.close();
   } catch (Exception e) {
     System.out.println("close" + e);
   }
 }
예제 #12
0
  public void read(byte[] data) {
    try {
      is = port.getInputStream();
    } catch (IOException e) {
      System.err.println("IOException: " + e);
    }

    try {
      is.read(data);
    } catch (IOException e) {
      System.err.println("IOException: " + e);
    }
  }
예제 #13
0
  public void write(byte[] data) {
    try {
      os = port.getOutputStream();
    } catch (IOException e) {
      System.err.println("IOException: " + e);
    }

    try {
      os.write(data);
    } catch (IOException e) {
      System.err.println("IOException: " + e);
    }
  }
예제 #14
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();
    }
  }
예제 #15
0
 /*
  * Code cribbed from RXTX example
  */
 public static void closePort(SerialPort serialPort) {
   if (serialPort != null) {
     serialPort.notifyOnDataAvailable(false);
     serialPort.removeEventListener();
     if (inputStream != null) {
       try {
         inputStream.close();
         inputStream = null;
       } catch (IOException e) {
       }
     }
     if (outputStream != null) {
       try {
         outputStream.close();
         outputStream = null;
       } catch (IOException e) {
       }
     }
     serialPort.close();
     serialPort = null;
   }
 }
  public void connection() {
    try {
      portId = CommPortIdentifier.getPortIdentifier("COM10");

      sPort = (SerialPort) portId.open("COM10", 1000);
      sPort.setSerialPortParams(9600, 8, 1, sPort.PARITY_NONE);

      is = sPort.getInputStream();
      os = sPort.getOutputStream();

      dis = new DataInputStream(is);
      dos = new DataOutputStream(os);
    } catch (NoSuchPortException e) {
      System.out.println("No Port\n" + e);
    } catch (PortInUseException e) {
      System.out.println("Port\n" + e);
    } catch (UnsupportedCommOperationException e) {
      System.out.println("Unsupported Problem\n" + e);
    } catch (IOException e) {
      System.out.println("IO Problem" + e);
    } catch (Exception e) {
      System.out.println("Unknown Problem" + e);
    }
  }
  public void openStreams() throws IOException {
    try {
      serialPort = new TOSSerial(portName);
    } catch (Exception e) {
      throw new IOException("Could not open " + portName + ": " + e.getMessage());
    }

    try {
      // serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
      serialPort.setSerialPortParams(baudRate, 8, SerialPort.STOPBITS_1, false);

      serialPort.addListener(this);
      serialPort.notifyOn(SerialPortEvent.DATA_AVAILABLE, true);
    } catch (Exception e) {
      serialPort.close();
      throw new IOException("Could not configure " + portName + ": " + e.getMessage());
    }

    is = serialPort.getInputStream();
    os = serialPort.getOutputStream();
  }
 public void closeStreams() throws IOException {
   serialPort.close();
 }
예제 #19
0
 private void printPortStatus() {
   System.out.println("baud rate: " + port.getBaudRate());
   System.out.println("data bits: " + port.getDataBits());
   System.out.println("stop bits: " + port.getStopBits());
   System.out.println("parity:    " + port.getParity());
 }
 protected void finalize() {
   System.out.println("SerialByteSource finalize");
   serialPort.close();
 }
예제 #21
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;
  }
 /**
  * This should be called when you stop using the port. This will prevent port locking on platforms
  * like Linux.
  */
 public synchronized void closeSerial() {
   if (serialPort != null) {
     serialPort.removeEventListener();
     serialPort.close();
   }
 }