Beispiel #1
0
  private void initialize() {

    // ==========================================
    /*
     * get System porperties:
     */
    String operatingSystem = null;
    Properties p = System.getProperties();
    Enumeration e = p.elements();
    while (e.hasMoreElements()) {
      operatingSystem = e.nextElement().toString();
      //				System.out.println(operatingSystem);
      if (operatingSystem.equals("Linux")) {
        logger.debug("found " + operatingSystem + " Operating System");
        comPort = "/dev/ttyS0";
        break;
      }
      if (operatingSystem.equals("Windows XP")) {
        logger.debug("found " + operatingSystem + " Operating System");
        comPort = "COM1";
        break;
      }

      //				if(operatingSystem.equals("Linux") || operatingSystem.equals("Windows")) {
      //					logger.debug("found "+operatingSystem+" Operating System");
      //					break;
      //				}
    }
    //	===============end===========================

    Enumeration pList = CommPortIdentifier.getPortIdentifiers();
    //		logger.debug("(Khepera) initializeeeee()...");

    while (pList.hasMoreElements()) {
      CommPortIdentifier cpi = (CommPortIdentifier) pList.nextElement();

      if (cpi.getName().equals(comPort)) {
        try {
          com = null;
          com = (SerialPort) cpi.open("KHEPERA_" + comPort, 1000);
          try {

            //	========================================
            /* under Linux this block is crucial to setSerialPortParams()
             * - i have no idea why...
             */
            String s =
                "default settings: "
                    + com.getBaudRate()
                    + " "
                    + com.getDataBits()
                    + " "
                    + com.getParity()
                    + " "
                    + com.getStopBits();
            System.out.println(s);
            //	===============end=========================

            com.setSerialPortParams(
                57600, SerialPort.DATABITS_8, SerialPort.STOPBITS_2, SerialPort.PARITY_NONE);
            System.out.println(
                "Current settings: "
                    + com.getBaudRate()
                    + " "
                    + com.getDataBits()
                    + " "
                    + com.getParity()
                    + " "
                    + com.getStopBits());
          } catch (UnsupportedCommOperationException e1) {
            logger.debug("(Khepera) UnsupportedCommOperation: " + e1);
            e1.printStackTrace();
          }
          //					try {
          //						com.setSerialPortParams(BAUDRATE,
          //								com.DATABITS_8,
          //								com.STOPBITS_2,
          //								com.PARITY_NONE);
          //						com.setFlowControlMode(com.FLOWCONTROL_NONE);
          //					} catch (UnsupportedCommOperationException e1) {
          //						logger.debug("(Khepera) UnsupportedCommOperation: "+e1);
          //						e1.printStackTrace();
          //					}
          logger.debug("[Khepera.initialize()] established " + comPort);

          out = null;
          out = com.getOutputStream();
          inStream = null;
          inStream = com.getInputStream();

          byte[] readBuffer2 = new byte[1000];
          if (inStream.available() > 0) {
            int numBytes2 = inStream.read(readBuffer2);
            String result2 = new String(readBuffer2, 0, numBytes2);
            logger.debug("result2: " + result2);
          }

          //					in = null;
          in = new BufferedReader(new InputStreamReader(inStream));

          bufferedKheperaAnswer =
              new BufferedKheperaAnswer(logger, inStream, this, writelock, debug, operatingSystem);
          try {
            com.addEventListener(bufferedKheperaAnswer);

            com.notifyOnOutputEmpty(true);
            com.notifyOnDataAvailable(true);
            com.notifyOnOverrunError(true);
            com.notifyOnBreakInterrupt(true);
            com.notifyOnCarrierDetect(true);
            com.notifyOnCTS(true);
            com.notifyOnDSR(true);
            com.notifyOnFramingError(true);
            com.notifyOnParityError(true);
            com.notifyOnRingIndicator(false);
          } catch (TooManyListenersException e1) {
            logger.debug(
                "[Khepera.initialize()] can not add further eventListeners to serialPort object"
                    + e);
            e1.printStackTrace();
          }
        } catch (PortInUseException e2) {
          logger.debug("[Khepera.initialize()] Port in use: " + e2);
          e2.printStackTrace();
        } catch (IOException e3) {
          logger.debug(
              "[Khepera.initialize()] IOException while initialising COMport: <"
                  + comPort
                  + ">"
                  + e);
          e3.printStackTrace();
        }
        break;
      }
    }
    if (com == null)
      logger.error(
          "[Khepera.initialize()] unable to find specified COMport "
              + comPort
              + ". giving up! (the robot is not connected yet.)");
    else {
      logger.debug("[Khepera.initialize()] starting bufferedKheperaAnswer.start()");
      bufferedKheperaAnswer.start();

      if (debug) logger.debug("[Khepera.initialize()] initialize() finished");

      update = new KheperaUpdateManager(this, logger, debug);
      logger.debug("[Khepera.initialize()] starting KheperaUpdateManager update.start()");
      update.start();
    }
  }
 public Enumeration<Object> elements() {
   return p.elements();
 }
 @Override
 public synchronized Enumeration<Object> elements() {
   // TODO: implement for GridField values too
   return ctx.elements();
 }