views:

33

answers:

1

Hi All,

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.

+1  A: 

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;
    }
  }
Romain Hippeau
Thanks for the response Romain. The problem I have is that serialEvent is only called after approx. 17 chars are received, and at the end of parsing the first 6 chars I need to send an ACK response, so I am effectively notified too late in the callback. Later today I am going to try setting the input buffer size on the input stream to a small known value to see if that has any effect. Thanks - Fred
fred basset
@fred basset - If the other party you are taloiking to is expecting an ACK after 6 bvytes, then wht are they sending you more data ? It seems like your protocol is not well defined. By setting the buffer in Java smaller you are not going to change what has already been received in the UART chip. You have a protocol problem not a Java problem.
Romain Hippeau
Romain, thank you again for the response. The situation is that the device will send me a 6 byte packet and expect an ACK. However, the async. serial callback is only called after the system has received about 17 bytes. The device has sent me the first 6 byte packet, but I have not been notified yet than anything's received, so it re-transmits because it doesn't get the ack. After about the 3rd re-transmit I finally have received 17 bytes and can parse the data and send an ACK, but the device has had to re-transmit 3 times. I need to somehow configure javacomm to notify me for each 1 byte
fred basset
@fred basset Are you using a USB port for your serial port with an adapter. Many of these are problematic. Also what OS are you using ?
Romain Hippeau
Good point, I have a laptop and am using a docking station which has a COM1 on the back. This may use USB internally to connect. It would be a good test to test with a real serial port, I will try that. OS is WinXP, nothing I can do about that.
fred basset
Another tip - try using Hyperterminal and see what you get.
Romain Hippeau
@fred basset Here is a post with everything you never wanted to know about the serial port on win32: http://www.daniweb.com/forums/thread8020.html also see my comment on using Hyperterminal. I am not sure you have a Java problem, either a protocol or hardware issue. If you could remove Java from the equation you could verify that. I have used the javaComm API in the past and it is solid. Even done some realtime hardware control using java.
Romain Hippeau