Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

Re : Serial data is limited to signed 8-bit data???

$
0
0
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:


  1. points = new float[(int)dataLength];
  2. for (int i = 0; i < points.length; i++) {
  3.       float value = get4ByteFloat(i*4+headerSize, data);
  4.       points[i] = value;
  5. }


and then I had three helper methods depending on the data I was expecting:
  1. long get2Bytes(int offset, byte[] data)
  2. {
  3.   return ((((int) data[offset + 1]) & 0xff) << 8 |
  4.     (((int) data[offset]) & 0xff));
  5. }

  6. long get4Bytes(int offset, byte[] data)
  7. {    
  8.   return ((((int) data[offset + 3]) & 0xff) << 24 | 
  9.     (((int) data[offset + 2]) & 0xff) << 16 |
  10.     (((int) data[offset + 1]) & 0xff) << 8 |
  11.     (((int) data[offset]) & 0xff));
  12. }
  13. float get4ByteFloat (int offset, byte[] data) {
  14.   return Float.intBitsToFloat((int)get4Bytes(offset, data));
  15. }
Hopefully that points you in the right direction, sorry if Im totally off base.

Viewing all articles
Browse latest Browse all 1768

Trending Articles