开发者

Java serial comms : way to set receieve threshold when using async. read notification

开发者 https://www.devze.com 2023-01-09 06:25 出处:网络
I have some code that\'s using the JavaComm API.It implements SerialPortEventListener and the reception of characters happens asynchronously.This works fine except that my serialEvent callback is noti

I have some code that's using the JavaComm API. It implements SerialPortEventListener and the reception of characters happens asynchronously. This works fine except that my serialEvent callback is notified after about 17 chars have been received, for my packet parsing I need it to be notified when <= 6 characters have been received. Is there any way to configure the serial API to call the async. notification when a specified n开发者_开发问答o. of characters have been received?

Thank you, fred.


All you get is a stream and a SerialPortEvent.DATA_AVAILABLE when data is available in the stream. What you could do is add a level of indirection and create your own listener that would be called when 6 characters have passed through and simply pass in the byte array with thoose 6 characters. I added where you would insert te code below. The implementation is up to you.

  public void serialEvent(SerialPortEvent event) {
    switch (event.getEventType()) {
    case SerialPortEvent.BI:
    case SerialPortEvent.OE:
    case SerialPortEvent.FE:
    case SerialPortEvent.PE:
    case SerialPortEvent.CD:
    case SerialPortEvent.CTS:
    case SerialPortEvent.DSR:
    case SerialPortEvent.RI:
    case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
      break;
    case SerialPortEvent.DATA_AVAILABLE:
      byte[] readBuffer = new byte[20];

      try {
        while (inputStream.available() > 0) {
          int numBytes = inputStream.read(readBuffer);
        }
        // partition readBuffer into chunks of 6 bytes
        ...
        registeredListener.dataReceived(sixByteByteArray);
      } catch (IOException e) {
      }
      break;
    }
  }
0

精彩评论

暂无评论...
验证码 换一张
取 消