Skip to main content
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

This is a follow-up to yesterday's codereview-question about reading serial data and parsing itreading serial data and parsing it.

This is a follow-up to yesterday's codereview-question about reading serial data and parsing it.

This is a follow-up to yesterday's codereview-question about reading serial data and parsing it.

Source Link
Hedge
  • 171
  • 1
  • 5

Serial-data read buffer handling

This is a follow-up to yesterday's codereview-question about reading serial data and parsing it.

The code below is run into a seperate thread looped endlessly. Currently I track the position in my inputBufer manually and copy all bytes which haven't been processed into a new byte-array and assigning that to the old inputBuffer.

Can the read buffer handling be improved?

bytesRead = SerialPort.Read(inputBuffer, bufferPos, SerialPort.BytesToRead);
bufferPos += bytesRead;
bytesProcessed = 0;

// process data-packets
for (int i = 0; i < this.bufferPos; i++) {

  // Flex sensor data
  //
  if (this.bufferPos-i >= 14 && inputBuffer[i] == 'F'){
    i++;
    short[] sensorData = new short[6];
    sensorData[0] = (short)((short)inputBuffer[i++] << 8 | inputBuffer[i++]); // thumb
    sensorData[1] = (short)((short)inputBuffer[i++] << 8 | inputBuffer[i++]); // index finger (finger knuckle)
    sensorData[2] = (short)((short)inputBuffer[i++] << 8 | inputBuffer[i++]); // middle finger
    sensorData[3] = (short)((short)inputBuffer[i++] << 8 | inputBuffer[i++]); // ring finger
    sensorData[4] = (short)((short)inputBuffer[i++] << 8 | inputBuffer[i++]); // not used
    sensorData[5] = (short)((short)inputBuffer[i++] << 8 | inputBuffer[i++]); // index finger (hand knucke)
    // checksum
    byte checksum = inputBuffer[i++];
    ParseSerialData(PacketType.Flex, sensorData, checksum);
    bytesProcessed = i;
    //Debug.Log (sensorData[0] + " then " + bytesProcessed);
  }

  // Raw sensor data
  //
  if (this.bufferPos-i >= 20 && inputBuffer[i] == 'S'){
    i++;
    short[] sensorData = new short[9];
    sensorData[0] = (short)((short)inputBuffer[i++] << 8 | inputBuffer[i++]); // gyroscope X
    sensorData[1] = (short)((short)inputBuffer[i++] << 8 | inputBuffer[i++]); // gyroscope Y
    sensorData[2] = (short)((short)inputBuffer[i++] << 8 | inputBuffer[i++]); // gyroscope Z
    sensorData[3] = (short)((short)inputBuffer[i++] << 8 | inputBuffer[i++]); // accelerometer X
    sensorData[4] = (short)((short)inputBuffer[i++] << 8 | inputBuffer[i++]); // accelerometer Y
    sensorData[5] = (short)((short)inputBuffer[i++] << 8 | inputBuffer[i++]); // accelerometer Z
    sensorData[6] = (short)((short)inputBuffer[i++] << 8 | inputBuffer[i++]); // magnetometer X
    sensorData[7] = (short)((short)inputBuffer[i++] << 8 | inputBuffer[i++]); // magnetometer Y
    sensorData[8] = (short)((short)inputBuffer[i++] << 8 | inputBuffer[i++]); // magnetometer Z
    // checksum
    byte checksum = inputBuffer[i++];
    ParseSerialData(PacketType.Raw, sensorData, checksum);
    bytesProcessed = i;
  }

  // Quaternion of device's rotation
  //
  if (this.bufferPos-i >= 20 &&  inputBuffer[i] == 'Q'){
    i++;
    short[] sensorData = new short[4];
    sensorData[0] = (short)((short)inputBuffer[i++] << 8 | inputBuffer[i++]); // W
    sensorData[1] = (short)((short)inputBuffer[i++] << 8 | inputBuffer[i++]); // X
    sensorData[2] = (short)((short)inputBuffer[i++] << 8 | inputBuffer[i++]); // Y
    sensorData[3] = (short)((short)inputBuffer[i++] << 8 | inputBuffer[i++]); // Z
    // checksum
    byte checksum = inputBuffer[i++];
    ParseSerialData(PacketType.Quaternion, sensorData, checksum);
    bytesProcessed = i;
  }
}

// Copy unprocessed rest of stream
int rest = bufferPos - bytesProcessed;
  if (rest > 0){
  byte[] tempBuffer = new byte[4096];
  Array.Copy (inputBuffer, bytesProcessed, tempBuffer, 0, rest);
  inputBuffer = tempBuffer;
  bufferPos = rest;
}
else {
  inputBuffer = new byte[4096];
  bufferPos = 0;
}