Hi Matt,
Im not sure this is really what you are getting at here, I haven't worked with RS232 or really much in the way of serial data, but I did have an opportunity to work on a project that dealt with bytestreams and parsing data out of the 8bit streams. I only needed to get ints and floats out of 4-byte chunks of the stream, but it seemed to relate perhaps to what you are doing.
I would receive the data stream and then loop through it 4 numbers at a time:
- points = new float[(int)dataLength];
- for (int i = 0; i < points.length; i++) {
- float value = get4ByteFloat(i*4+headerSize, data);
- points[i] = value;
- }
and then I had three helper methods depending on the data I was expecting:
- long get2Bytes(int offset, byte[] data)
- {
- return ((((int) data[offset + 1]) & 0xff) << 8 |
- (((int) data[offset]) & 0xff));
- }
- long get4Bytes(int offset, byte[] data)
- {
- return ((((int) data[offset + 3]) & 0xff) << 24 |
- (((int) data[offset + 2]) & 0xff) << 16 |
- (((int) data[offset + 1]) & 0xff) << 8 |
- (((int) data[offset]) & 0xff));
- }
- float get4ByteFloat (int offset, byte[] data) {
- return Float.intBitsToFloat((int)get4Bytes(offset, data));
- }
Hopefully that points you in the right direction, sorry if Im totally off base.