コード例 #1
0
  private void write(byte[] data) {
    try {
      if (m_out == null) {
        m_PortIdPrinter =
            CommPortIdentifier.getPortIdentifier(
                m_sPortScale); // Tomamos el puerto
        m_CommPortPrinter =
            (SerialPort) m_PortIdPrinter.open("PORTID", 2000); // Abrimos el puerto

        m_out = m_CommPortPrinter.getOutputStream(); // Tomamos el chorro de escritura
        m_in = m_CommPortPrinter.getInputStream();

        m_CommPortPrinter.addEventListener(this);
        m_CommPortPrinter.notifyOnDataAvailable(true);

        m_CommPortPrinter.setSerialPortParams(
            4800,
            SerialPort.DATABITS_8,
            SerialPort.STOPBITS_1,
            SerialPort.PARITY_ODD); // Configuramos el puerto
      }
      m_out.write(data);
    } catch (NoSuchPortException e) {
      e.printStackTrace();
    } catch (PortInUseException e) {
      e.printStackTrace();
    } catch (UnsupportedCommOperationException e) {
      e.printStackTrace();
    } catch (TooManyListenersException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
コード例 #2
0
  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;
    }
  }