2

I have an Arduino program that sends via Bluetooth a byte array to a Java program.

The Java program receives the data on JSSC. The array is 72 bytes long (18 floats a´ 4byte). How JSSC recognizes the end of transmission, or know that a new byte array becomes available?

In most cases, the transfer is correct. But from time to time waste is received.

I've found this example here.

http://www.javaprogrammingforums.com/java-se-api-tutorials/5603-jssc-library-easy-work-serial-ports.html

This if(event.getEventValue() == 10) means an linefeed, but bytearray have no linefeeds.

Arduino:

Serial.write(bytearray, size of bytearray);

Java:

    class SerialPortReader implements SerialPortEventListener {

    public void serialEvent(SerialPortEvent event) {
        int dataNumber = 0;           
        try {             
            if(event.isRXCHAR()){
                if(event.getEventValue() >= BYTE_NUMBER){
                    receivedByte = usbPort.readBytes(BYTE_NUMBER);

                    if(receivedByte != null) {
                        isRequestOK = true;       
                        byte[] myArray = new byte[FLOATSIZE];

                        for(int i = 0, y = 0; i < receivedByte.length; i++, y++) {  //i < receivedByte.length-1
                            if(myArray == null) {
                                y = 0;
                                myArray = new byte[FLOATSIZE];
                            }
                            if((i + 1) % FLOATSIZE != 0) {
                                myArray[y] = receivedByte[i];
                            } 
                            else {
                                myArray[y] = receivedByte[i];               
                                receivedValue[dataNumber] = ByteBuffer.wrap(myArray).order(ByteOrder.LITTLE_ENDIAN).getFloat();
                                myArray = null;                                 
                                dataNumber++;  
                            }
                        }                         
                    }
                }                          
            }                
        } catch (SerialPortException e) { }    ///InvocationTargetException oder NullPointerException  
    }//--------------------------- End of serialEvent ------------------------------------------------------------- 
}//--------------------------- End of SerialPortReader ------------------------------------------------------------

Can anybody help me?

Best regards Willi

P.S. This is my first question in this forum. Hopefully I have followed the rules.

0 Answers0