Esempio n. 1
0
  public void serialEvent(SerialPortEvent event) {
    System.out.println("Serial Port event");
    switch (event.getEventType()) {
      case SerialPortEvent.DATA_AVAILABLE:
        @SuppressWarnings("unused")
        String Interrupt = "";

        try {
          while (reader.ready() == false) {}
          ;
          Interrupt = reader.readLine();
        } catch (IOException e) {
        }
        break;
    }
  }
Esempio n. 2
0
 // Deal with something robot has sent.
 public void serialEvent(SerialPortEvent events) {
   switch (events.getEventType()) {
     case SerialPortEvent.DATA_AVAILABLE:
       try {
         final byte[] buffer = new byte[1024];
         int len = in.read(buffer);
         if (len > 0) {
           String line2 = new String(buffer, 0, len);
           Log("<span style='color:#FFA500'>" + line2.replace("\n", "<br>") + "</span>");
           line3 += line2;
           // wait for the cue ("> ") to send another command
           if (line3.lastIndexOf(cue) != -1) {
             if (ConfirmPort()) {
               line3 = "";
               SendFileCommand();
             }
           }
         }
       } catch (IOException e) {
       }
       break;
   }
 }
  public synchronized void serialEvent(SerialPortEvent oEvent) {
    if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
      try {
        int available = input.available();
        if (count < BUF_SIZE) {
          count =
              count
                  + input.read(
                      buf, count, (available > BUF_SIZE - count) ? BUF_SIZE - count : available);
        }

        start = newline;
        for (int i = newline; i <= count; i++) {
          if (buf[i] == 'U' || buf[i] == '$') { // start byte
            start = i;
            break;
          }
        }
        for (int i = start; i <= count; i++) {
          if (buf[i] == '\n') { // end byte
            newline = i;
            break;
          }
        }

        if (newline <= start) {
          if (count == BUF_SIZE) { // buf full an no valid datagram found, clear the buffer
            // need to copy the stuff after the start byte to the beginning
            if ((count - start) <= 128) {
              System.arraycopy(buf, start, buf, 0, count - start);
              count = count - start;
              start = 0;
              newline = 0;
            } else { // buf full and last start byte is more than max packet size clear all of the
                     // buffer
              count = 0; // no need to copy since the buf is grabage
              start = 0;
              newline = 0;
            }
          }
          return;
        }

        if (buf[start] == '$') {
          buf[newline] = '\0';
          System.out.println(new String(buf, start + 1, newline - start));
        }

        // found valid datagram
        // parse datagram comma delimited
        buf[newline] = ',';
        String newbuf = new String(buf, start, newline - start);
        StringTokenizer st = new StringTokenizer(newbuf, ",");
        int[] data = new int[5];

        int i = 0;
        while (st.hasMoreElements() && i < 5) {
          String temp = st.nextToken();
          try {
            data[i] = Integer.parseInt(temp, 16);
          } catch (NumberFormatException e) {
          }
          i++;
        }

        RobotEvent ev = new RobotEvent();
        int checksum;

        ev.setCommand(EventEnum.getEvent(data[1]));
        ev.setIndex((short) data[2]);
        ev.setValue((int) data[3]);
        checksum = data[4];

        int checksum2 =
            (int)
                (((int) (ev.getCommand().getValue() & 0x000000FF)
                        + (int) (ev.getIndex() & 0x000000FF)
                        + (int) (ev.getValue() & 0x000000FF)
                        + (int) ((ev.getValue() & 0x0000FF00) >> 8))
                    % 256);

        if (checksum2 == checksum) {
          // log event received
          recv.put(ev);
        }
      } catch (Exception e) {
      }
    }
  }