Пример #1
0
  protected void onSerialPortEvent(SerialPortEvent event) {

    if (event.isRXCHAR()) {

      int count = event.getEventValue();

      if (count == 0) return;

      byte[] characters;
      try {

        characters = serialPort.readBytes(count);

        for (byte character : characters) {
          // System.out.println(Integer.toHexString(character) + "\t:\t" + (int) character + "\t:\t"
          // + (char) character);
          onReceiveChar((char) character);
        }

      } catch (SerialPortException e) {
        showErrorMessage("No se pudo leer un byte");
        e.printStackTrace();
      }
    }
  }
Пример #2
0
 public synchronized void serialEvent(SerialPortEvent serialEvent) {
   if (serialEvent.isRXCHAR()) {
     try {
       byte[] buf = port.readBytes(serialEvent.getEventValue());
       if (buf.length > 0) {
         String msg = new String(buf);
         char[] chars = msg.toCharArray();
         message(chars, chars.length);
       }
     } catch (SerialPortException e) {
       errorMessage("serialEvent", e);
     }
   }
 }
    public void serialEvent(SerialPortEvent event) {
      try {

        if (event.isRXCHAR() && !getIgnoreMessage()) {
          int numberOfBytes = event.getEventValue();
          if (numberOfBytes > 0) {
            byte[] bufferOriginal = serialPort.readBytes(numberOfBytes);
            ByteArrayObject frame = new ByteArrayObject(bufferOriginal, numberOfBytes);
            _caller.getDataLayer().notifyFrame(frame);
          }
        }

      } catch (Exception e) {

        LOG.error("Error on read from serial data:" + e.getMessage());
      }
    }
Пример #4
0
 /**
  * @generate serialEvent.xml
  * @webref serial:events
  * @usage web_application
  * @param event the port where new data is available
  */
 public void serialEvent(SerialPortEvent event) {
   if (event.getEventType() == SerialPortEvent.RXCHAR) {
     int toRead;
     try {
       while (0 < (toRead = port.getInputBufferBytesCount())) {
         // this method can be called from the context of another thread
         synchronized (buffer) {
           // read one byte at a time if the sketch is using serialEvent
           if (serialEventMethod != null) {
             toRead = 1;
           }
           // enlarge buffer if necessary
           if (buffer.length < inBuffer + toRead) {
             byte temp[] = new byte[buffer.length << 1];
             System.arraycopy(buffer, 0, temp, 0, inBuffer);
             buffer = temp;
           }
           // read an array of bytes and copy it into our buffer
           byte[] read = port.readBytes(toRead);
           System.arraycopy(read, 0, buffer, inBuffer, read.length);
           inBuffer += read.length;
         }
         if (serialEventMethod != null) {
           if ((0 < bufferUntilSize && bufferUntilSize <= inBuffer - readOffset)
               || (0 == bufferUntilSize && bufferUntilByte == buffer[inBuffer - 1])) {
             try {
               // serialEvent() is invoked in the context of the current (serial) thread
               // which means that serialization and atomic variables need to be used to
               // guarantee reliable operation (and better not draw() etc..)
               // serialAvailable() does not provide any real benefits over using
               // available() and read() inside draw - but this function has no
               // thread-safety issues since it's being invoked during pre in the context
               // of the Processing applet
               serialEventMethod.invoke(parent, this);
             } catch (Exception e) {
               System.err.println("Error, disabling serialEvent() for " + port.getPortName());
               System.err.println(e.getLocalizedMessage());
               serialEventMethod = null;
             }
           }
         }
         invokeSerialAvailable = true;
       }
     } catch (SerialPortException e) {
       throw new RuntimeException(
           "Error reading from serial port " + e.getPortName() + ": " + e.getExceptionType());
     }
   }
 }
Пример #5
0
 public void serialEvent(SerialPortEvent event) {
   final String mn = "serialEvent";
   //			logger.logp(Level.INFO, this.getClass().getName(), mn, "> " + event);
   try {
     if (event.isRXCHAR()) { // If data is available
       String str = serialPort.readString();
       // System.out.println("serialEvent '" + str + "'");
       buildLine(str);
     } else {
       logger.logp(
           Level.WARNING,
           this.getClass().getName(),
           mn,
           "Unknown event " + event.getEventType() + ", " + event.getEventValue());
     }
   } catch (Throwable t) {
     // This Thread should never fail
     String msg = t.getMessage();
     logger.logp(Level.WARNING, this.getClass().getName(), mn, msg);
     setErrorMessage(msg);
     t.printStackTrace();
   }
   //			logger.logp(Level.INFO, this.getClass().getName(), mn, "<");
 }
Пример #6
0
  @Override
  public synchronized void serialEvent(SerialPortEvent event) {

    if (log.isTraceEnabled()) {
      log.trace("SerialEvent '" + event.getEventType() + "' on [" + event.getPortName() + "]");
    }

    if (event.isRXCHAR() && hasListeners()) { // If data is available
      byte[] data;
      try {
        data = serialPort.readBytes(event.getEventValue());
        getStreamReader().processPacketRead(data, data.length);
      } catch (SerialPortException e) {
        log.error(e.getMessage(), e);
        throw new RuntimeException(e);
      }

    } else if (event.isCTS()) { // If CTS line has changed state
      if (event.getEventValue() == 1) { // If line is ON
        log.debug("SerialEvent - CTS = ON");
      } else {
        log.debug("SerialEvent - CTS = OFF");
      }
    } else if (event.isBREAK()) { // /If DSR line has changed state
      log.debug("SerialEvent - isBREAK");
    } else if (event.isERR()) { // /If DSR line has changed state
      log.debug("SerialEvent - isERR");
      try {
        disconnect();
      } catch (ConnectionException ex) {
        log.error(ex.getMessage(), ex);
      }
    } else if (event.isRING()) { // /If DSR line has changed state
      log.debug("SerialEvent - isRING");
    }
  }
  @Override
  public void serialEvent(SerialPortEvent serialPortEvent) {
    int val = serialPortEvent.getEventValue();
    if (serialPortEvent.isRXCHAR() && val > 0) {
      if (SerialConnection.DEBUG_SERIAL) {
        System.out.println("Received " + val + " bytes");
      }
      try {

        byte[] preambleData = new byte[0];
        byte[] payloadData = new byte[0];

        preambleData = mPort.readBytes(PREAMBLE_SIZE, 500);

        if (SerialConnection.DEBUG_SERIAL) {
          System.out.println(
              "Preamble: "
                  + ByteArrayHelpers.getByteArrayString(preambleData)
                  + "("
                  + new String(preambleData)
                  + ")");
        }

        if (preambleData.length == PREAMBLE_SIZE) {
          dataSize = preambleData[2];
          if (dataSize > 0) {
            payloadData = mPort.readBytes(dataSize, 500);

            if (SerialConnection.DEBUG_SERIAL) {
              System.out.println(
                  "Data: "
                      + ByteArrayHelpers.getByteArrayString(payloadData)
                      + "("
                      + new String(payloadData)
                      + ")");
            }

            networkDelegate.handlePacket(ByteArrayHelpers.concat(preambleData, payloadData));

          } else if (0 == dataSize) {
            networkDelegate.handlePacket(preambleData);
          }
        } else {
          if (SerialConnection.DEBUG_MSGS) {
            System.out.println(
                "preamble data length: "
                    + preambleData.length
                    + " does not match correct size: "
                    + PREAMBLE_SIZE);
          }
        }

        //				switch (state) {
        //				case PREAMBLE:
        //					preambleData = mPort.readBytes(PREAMBLE_SIZE);
        //
        //					if (SerialConnection.DEBUG_SERIAL) {
        //						System.out.println("Preamble: " + ByteArrayHelpers.getByteArrayString(preambleData)
        // + "(" + new String(payloadData) + ")");
        //					}
        //
        //					if (preambleData.length == PREAMBLE_SIZE)
        //					{
        //						dataSize = preambleData[PREAMBLE_SIZE - 1];
        //						if (dataSize > 0)
        //						{
        //							state = ListenerState.DATA;
        //							payloadData = mPort.readBytes(dataSize, 50);
        //
        //							if (SerialConnection.DEBUG_SERIAL) {
        //								System.out.println("Data: " + ByteArrayHelpers.getByteArrayString(payloadData) +
        // "(" + new String(payloadData) + ")");
        //							}
        //							state = ListenerState.PREAMBLE;
        //
        //						} else if (0 == dataSize)
        //						{
        //							networkDelegate.handlePacket(preambleData);
        //						}
        //					} else
        //					{
        //						if (SerialConnection.DEBUG_MSGS)
        //						{
        //							System.out.println("preamble data length: " + preambleData.length + " does not
        // match correct size: " + PREAMBLE_SIZE);
        //						}
        //					}
        //					break;
        //
        //				case DATA:
        //					payloadData = mPort.readBytes(dataSize);
        //
        //					if (SerialConnection.DEBUG_SERIAL) {
        //						System.out.println("Data: " + ByteArrayHelpers.getByteArrayString(payloadData));
        //					}
        //
        //					if (dataSize == payloadData.length)
        //					{
        //						byte[] concatData = ByteArrayHelpers.concat(preambleData, payloadData);
        //						networkDelegate.handlePacket(concatData);
        //					}
        //					state = ListenerState.PREAMBLE;
        //					break;
        //
        //				default:
        //					break;
        //				}

      } catch (SerialPortException e) {
        if (SerialConnection.DEBUG_MSGS) {
          e.printStackTrace();
        }

      } catch (SerialPortTimeoutException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }