Ejemplo n.º 1
0
 private Method findCallback(final String name) {
   try {
     return parent.getClass().getMethod(name, this.getClass());
   } catch (Exception e) {
   }
   // Permit callback(Object) as alternative to callback(Serial).
   try {
     return parent.getClass().getMethod(name, Object.class);
   } catch (Exception e) {
   }
   return null;
 }
Ejemplo n.º 2
0
  /**
   * @param parent typically use "this"
   * @param port port used to transfer data
   * @param host when multiple NICs are in use, the ip (or name) to bind from
   */
  public MyServer(PApplet parent, int port, String host) {
    this.parent = parent;
    this.port = port;

    try {
      if (host == null) {
        server = new ServerSocket(this.port);
      } else {
        server = new ServerSocket(this.port, 10, InetAddress.getByName(host));
      }
      // clients = new Vector();
      clients = new Client[10];

      thread = new Thread(this);
      thread.start();

      parent.registerMethod("dispose", this);

      // reflection to check whether host applet has a call for
      // public void serverEvent(MyServer s, Client c);
      // which is called when a new guy connects
      try {
        serverEventMethod =
            parent.getClass().getMethod("serverEvent", new Class[] {MyServer.class, Client.class});
      } catch (Exception e) {
        // no such method, or an error.. which is fine, just ignore
        serverEventMethod = null;
      }

      try {
        clientValidationMethod =
            parent
                .getClass()
                .getMethod("clientValidation", new Class[] {MyServer.class, Client.class});
      } catch (Exception e) {
        // no such method, or an error.. which is fine, just ignore
        clientValidationMethod = null;
      }

    } catch (IOException e) {
      // e.printStackTrace();
      thread = null;
      throw new RuntimeException(e);
      // errorMessage("<init>", e);
    }
  }
Ejemplo n.º 3
0
  /**
   * @param parity 'N' for none, 'E' for even, 'O' for odd, 'M' for mark, 'S' for space ('N' is the
   *     default)
   * @param dataBits 8 is the default
   * @param stopBits 1.0, 1.5, or 2.0 (1.0 is the default)
   */
  public Serial(
      PApplet parent, String portName, int baudRate, char parity, int dataBits, float stopBits) {
    this.parent = parent;
    parent.registerMethod("dispose", this);
    parent.registerMethod("pre", this);

    // setup parity
    if (parity == 'O') {
      parity = SerialPort.PARITY_ODD;
    } else if (parity == 'E') {
      parity = SerialPort.PARITY_EVEN;
    } else if (parity == 'M') {
      parity = SerialPort.PARITY_MARK;
    } else if (parity == 'S') {
      parity = SerialPort.PARITY_SPACE;
    } else {
      parity = SerialPort.PARITY_NONE;
    }

    // setup stop bits
    int stopBitsIdx = SerialPort.STOPBITS_1;
    if (stopBits == 1.5f) {
      stopBitsIdx = SerialPort.STOPBITS_1_5;
    } else if (stopBits == 2) {
      stopBitsIdx = SerialPort.STOPBITS_2;
    }

    port = new SerialPort(portName);
    try {
      // the native open() call is not using O_NONBLOCK, so this might block for certain operations
      // (see write())
      port.openPort();
      port.setParams(baudRate, dataBits, stopBitsIdx, parity);
      // we could register more events here
      port.addEventListener(this, SerialPort.MASK_RXCHAR);
    } catch (SerialPortException e) {
      // this used to be a RuntimeException before, so stick with it
      throw new RuntimeException(
          "Error opening serial port " + e.getPortName() + ": " + e.getExceptionType());
    }

    serialEventMethod = findCallback("serialEvent");
    serialAvailableMethod = findCallback("serialAvailable");
  }
Ejemplo n.º 4
0
 protected void addClient(Client client) {
   if (clientCount == clients.length) {
     clients = (Client[]) PApplet.expand(clients);
   }
   clients[clientCount++] = client;
 }