Java的串行通信:使用方法时,异步设置receieve门槛。阅读通知 [英] Java serial comms : way to set receieve threshold when using async. read notification

查看:123
本文介绍了Java的串行通信:使用方法时,异步设置receieve门槛。阅读通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的使用JavaComm API一些code。它实现SerialPortEventListener和人物的接收异步发生。 = 6个字符已经收到,这只是已收到后约17个字符,我包解析我需要它的时候&LT通知我的serialEvent回调通知工作正常。有什么办法来配置串行API调用异步。当指定任何通知。字符已收到?

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 no. of characters have been received?

感谢您,
弗雷德。

Thank you, fred.

推荐答案

所有你得到的是一个流和SerialPortEvent.DATA_AVAILABLE当数据流中可用。你可以做的是增加了一个间接层,并创建自己的侦听器时6个字符,已通过并简单的字节数组中传递与thoose 6个字符,将被调用。我说,你会在下面插入TE code。执行是由你。

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;
    }
  }

这篇关于Java的串行通信:使用方法时,异步设置receieve门槛。阅读通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆