Beispiel #1
0
  @Override
  public void open() {
    // r�cup�ration de l'identifiant du port
    try {
      logger.info("Liste des ports : " + CommPortIdentifier.getPortIdentifiers());
      final Enumeration identifiers = CommPortIdentifier.getPortIdentifiers();
      while (identifiers.hasMoreElements()) {
        logger.info(((CommPortIdentifier) identifiers.nextElement()).getName());
      }
      portId = CommPortIdentifier.getPortIdentifier(connectionDescriptor.getPortName());
    } catch (final NoSuchPortException e) {
      throw new RuntimeException(
          "Rs232Connector (" + connectionDescriptor.getPortName() + ") : " + e.toString());
    }

    // ouverture du port
    try {
      serialPort = (SerialPort) portId.open("driver", 2000);
    } catch (final PortInUseException e) {
      throw new RuntimeException(
          "Rs232Connector (" + connectionDescriptor.getPortName() + ") : " + e.toString());
    }

    // param�trage du port
    // hmmmmmmmmmmmm cf :
    // http://forum.java.sun.com/thread.jspa?threadID=673793&start=15&tstart=0
    boolean result = false;
    while (!result) {
      try {
        serialPort.setSerialPortParams(
            connectionDescriptor.getBaudRate(),
            connectionDescriptor.getDataBits(),
            connectionDescriptor.getStopBits(),
            connectionDescriptor.getParity());
        result = true;
      } catch (final Exception ex) {
        logger.error("Unable to set serial ports parameters", ex);
        result = false;
        try {
          Thread.sleep(200);
        } catch (final InterruptedException exc) {
          logger.error("Error while sleeping", exc);
        }
      }
    }
    /** @todo : test */
    // Suppression
    try {
      serialPort.enableReceiveTimeout(5000); // timeout de 1 ms
    } catch (final UnsupportedCommOperationException ex) {
      logger.warn(
          "Warning : impossible to set timeout for port " + connectionDescriptor.getPortName(), ex);
    }

    try {
      // inputStream = new BufferedReader(new
      // InputStreamReader(serialPort.getInputStream()));
      inputStream = serialPort.getInputStream();
    } catch (final IOException e) {
      serialPort.close();
      throw new RuntimeException(
          "Rs232Connector (" + connectionDescriptor.getPortName() + ") : " + e.toString());
    }

    try {
      serialPort.addEventListener(
          new SerialPortEventListener() {

            @Override
            public void serialEvent(final SerialPortEvent evt) {
              if (evt.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
                synchronized (bufferMonitor) {
                  if (writeIndex >= BUFFER_LENGTH - 1) {
                    flushBuffer();
                  }
                  try {
                    byte newData = 0;
                    while (newData != -1) {
                      newData = (byte) inputStream.read();
                      if (newData == -1) {
                        break;
                      }
                      if (writeIndex >= BUFFER_LENGTH - 1) {
                        flushBuffer();
                      }
                      buffer[writeIndex++] = newData;
                    }
                  } catch (final Exception exc) {
                    logger.error("Error reading input stream from serial port", exc);
                  }
                  bufferMonitor.notify();
                }
              }
            }
          });
      serialPort.notifyOnDataAvailable(true);
      serialPort.enableReceiveTimeout(20);

    } catch (final TooManyListenersException exc) {
      logger.error("", exc);
    } catch (final UnsupportedCommOperationException exc) {
      logger.error("", exc);
    }

    final Thread threadParse =
        new Thread("Rs232Connector_" + connectionDescriptor.getPortName() + "_dispatch_thread") {
          @Override
          public void run() {
            try {
              parseThread();
            } catch (final Exception e) {
              logger.error("Error while parsing thread", e);
            }
          }
        };
    threadParse.start();
  }
  public static void main(String args[]) {

    // Command to turn of GPS
    int[] turn_off = new int[8];
    turn_off[0] = DLE;
    turn_off[1] = Pid_Command_Packet;
    turn_off[2] = 2; // Length of data
    turn_off[3] = Cmnd_Turn_Off_Pwr;
    turn_off[4] = 0;
    turn_off[6] = DLE;
    turn_off[7] = ETX;

    calcChecksum(turn_off);
    System.out.println("Turn off checksum: " + turn_off[5]);

    // Command to ask GPS for time data.
    int[] transfer_time = new int[8];
    transfer_time[0] = DLE;
    transfer_time[1] = Pid_Command_Packet;
    transfer_time[2] = 2;
    transfer_time[3] = Cmnd_Transfer_Time;
    transfer_time[4] = 0;
    transfer_time[6] = DLE;
    transfer_time[7] = ETX;

    calcChecksum(transfer_time);
    System.out.println("Transfer time" + transfer_time[5]);

    // Command to make a product request to the GPS.
    int[] product_request = new int[6];
    product_request[0] = DLE;
    product_request[1] = Pid_Product_Rqst;
    product_request[2] = 0;
    product_request[4] = DLE;
    product_request[5] = ETX;

    calcChecksum(product_request);
    System.out.println("Product request:" + product_request[5]);

    // Command to ask GPS for position data
    int[] transfer_position = new int[8];
    transfer_position[0] = DLE;
    transfer_position[1] = Pid_Command_Packet;
    transfer_position[2] = 2;
    transfer_position[3] = Cmnd_Transfer_Posn;
    transfer_position[4] = 0;
    transfer_position[6] = DLE;
    transfer_position[7] = ETX;

    calcChecksum(transfer_position);

    // Command to ask the GPS to start transmitting PVT data
    int[] start_PVT = new int[8];
    start_PVT[0] = DLE;
    start_PVT[1] = Pid_Command_Packet;
    start_PVT[2] = 2;
    start_PVT[3] = Cmnd_Start_Pvt_Data;
    start_PVT[4] = 0;
    start_PVT[6] = DLE;
    start_PVT[7] = ETX;

    calcChecksum(start_PVT);

    // Acknowledge-packet.
    int[] ack = new int[8];
    ack[0] = DLE;
    ack[1] = Pid_Ack_Byte;
    ack[2] = 2;
    ack[4] = 0;
    ack[6] = DLE;
    ack[7] = ETX;

    SerialPort port;
    BufferedInputStream input;
    BufferedOutputStream output;

    System.out.println("Using COM1.");
    // Open port.
    try {
      port =
          (SerialPort) CommPortIdentifier.getPortIdentifier("COM1").open("dk.itu.haas.GPS", 3000);
    } catch (NoSuchPortException e) {
      System.out.println("No such port!\n" + e.getMessage());
      return;
    } catch (PortInUseException e) {
      System.out.println("Port already in use! (??!)\n" + e.getMessage());
      return;
    }

    try {
      input = new BufferedInputStream(port.getInputStream());
      output = new BufferedOutputStream(port.getOutputStream());
    } catch (IOException e) {
      System.out.println("IOException... ");
      return;
    }

    System.out.println("Sending:");

    printPacket(turn_off);
    sendPacket(output, turn_off);

    /*
    printPacket(transfer_position);
    sendPacket(output, transfer_position);
    */
    /*
    printPacket(start_PVT);
    sendPacket(output, start_PVT);
    */
    /*
    printPacket(product_request);
    sendPacket(output, product_request);
    */
    System.out.println("--");

    int[] packet;
    try {
      Thread.sleep(500);
    } catch (InterruptedException e) {
    }

    try {
      while (input.available() > 0) {
        packet = readPacket(input);
        if (packet != null) {
          System.out.println("Received:");
          printPacket(packet);

          // Send acknowledge.
          ack[3] = packet[1];
          calcChecksum(ack);
          sendPacket(output, ack);

          System.out.println("--");
        } else {
          System.out.println("No packet received.");
        }
        try {
          Thread.sleep(5000);
        } catch (InterruptedException e) {
        }
      }
    } catch (IOException e) {
      System.out.println("IOError!");
      return;
    }
  }