正在从InputStream获取固定长度消息的字节数



我读了很多方法,现在我的脑子里充满了不同的方法,我不能做我想做的事。从arduino我发送固定的8字节消息。由于波特率、硬件问题或我得到的消息以随机大小分隔(例如:第一条消息=1字节,第二条消息=7字节)。要解决此问题,必须将缓冲区中的字节添加到具有序列的新数组中。为了测试消息是否正确,我将与CRC32进行检查。我想做一个测试程序来获得8字节的消息,计算CRC并将其发送回arduino以检查它是否正确。在这种情况下,我将进行Rx/Tx检查。

如果缓冲区大小是buffer[256],如何获得我接收的字节数,将它们添加到byte[] c = new byte[8],然后计算CRC32。

CCD_ 3总是给出256。

private class ConnectedThread extends Thread {
    private final InputStream mmInStream;
    private final OutputStream mmOutStream;
    int destination = 0;
    public ConnectedThread(BluetoothSocket socket) {
        InputStream tmpIn = null;
        OutputStream tmpOut = null;
        // Get the input and output streams, using temp objects because
        // member streams are final
        try {
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        } catch (IOException e) { }
        mmInStream = tmpIn;
        mmOutStream = tmpOut;
    }
    public void run() {
        byte[] buffer = new byte[256];  // buffer store for the stream
        /* Should i add to c array??? */
        byte[] c      = new byte[8];    // Fixed length message array
        int bytes; // bytes returned from read()
        // Keep listening to the InputStream until an exception occurs
        while (true) {
            try {
                // Read from the InputStream
                bytes = mmInStream.read(buffer);        // Get number of bytes and message in "buffer"
                // Here i should add buffer array to my array
                //System.arraycopy(buffer, 0, c, destination, buffer.length);
                //destination = ??? // number of bytes added to c and then do some if c is full calculate crc
                CRC32 checksum = new CRC32();
                checksum.update(c, 0, c.length);
                long checksumValue = checksum.getValue();
                System.out.println("CRC: " + checksumValue);
                write(""+checksumValue);
            } catch (IOException e) {
                break;
            }
        }
    }
    /* Call this from the main activity to send data to the remote device */
    public void write(String message) {
        Log.d(TAG, "Sent: " + message + "...");
        byte[] msgBuffer = message.getBytes();
        try {
            mmOutStream.write(msgBuffer);
        } catch (IOException e) {
            Log.d(TAG, "Error data send: " + e.getMessage() + "...");     
          }
    }
}

1)buffer.length总是给你256,因为你在下面的行中分配256:

byte[] buffer = new byte[256];

2) mmInStream.read(buffer)返回读取的字节数,如果已到达流的末尾,则返回-1

3) 假设您从流中读取了8个字节。这意味着您的缓冲区将有您刚刚读取的8个字节,其余248个字节为0(因为缓冲区是256)。因此,根据您的数据协议,您必须考虑到这一点。在大多数情况下,您将有一个终止字节(或多个)模式。这会让你知道你得到了一个完整的信息。

假设你的终止模式是0字节,那么它看起来像这样:

if (nrBytes <= BUFFER_SIZE) { // if buffer is enough
    for (int i = 0; i < nrBytes; i++) {
        //append byte to your byte[] c array 
        // or whatever data structure is suppose to hold each message
        if (0 == buffer[i]) {
              // termination pattern = complete message! 
              // CRC check
              // Do work with your message!
        }
    }
    buffer = new byte[BUFFER_SIZE]; //reset buffer (resets all to 0's)
} 

最新更新