Sign up ×
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

In the post Sending Large Amounts of Serial Data Steven10172 posted a StreamSend class to send data to and from Arduinos. I was able to incorporate it into my app with no problems. The question is how to convert the c++ receiveObject object to Processing. Real newbie here so any help is good.

This is what I have done so far as compared to the original code:

Original code:


/**
  * receiveObject
  *
  * Gets the data from the stream and stores to supplied object
  *
  * @param Stream to read data from
  * @param ptr to struct to fill
  * @param size of struct
  * @param character to send before the data stream (optional)
  * @param character to send after the data stream (optional)
  */
byte StreamSend::receiveObject(Stream &ostream;, void* ptr, unsigned int objSize) {
    return receiveObject(ostream, ptr, objSize, _prefixChar, _suffixChar);
}
byte StreamSend::receiveObject(Stream &ostream;, void* ptr, unsigned int objSize, char prefixChar, char suffixChar) {
  return receiveObject(ostream, ptr, objSize, 0, prefixChar, suffixChar);
}

byte StreamSend::receiveObject(Stream &ostream;, void* ptr, unsigned int objSize, unsigned int loopSize, char prefixChar, char suffixChar) {
  int maxLoops = (_maxLoopsToWait == -1) ? (objSize+getWrapperSize()) : _maxLoopsToWait;
  if(loopSize >= maxLoops) {
      return PACKET_NOT_FOUND;
  }
  if(ostream.available() >= (objSize+getWrapperSize())) { // Packet meets minimum size requirement
    if(ostream.read() != (byte)prefixChar) {
      // Prefix character is not found
      // Loop through the code again reading the next char
      return receiveObject(ostream, ptr, objSize, loopSize+1, prefixChar, suffixChar);
    }

    char data[objSize]; //Create a tmp char array of the data from Stream
    ostream.readBytes(data, objSize); //Read the # of bytes
    memcpy(ptr, data, objSize); //Copy the bytes into the struct

    if(ostream.read() != (byte)suffixChar) {
      //Suffix character is not found
      return BAD_PACKET;
    }
      return GOOD_PACKET;
  }
  return PACKET_NOT_FOUND; //Prefix character wasn't found so no packet detected
}


boolean StreamSend::isPacketNotFound(const byte packetStatus) {
    return (packetStatus == PACKET_NOT_FOUND);
}

boolean StreamSend::isPacketCorrupt(const byte packetStatus) {
    return (packetStatus == BAD_PACKET);
}

boolean StreamSend::isPacketGood(const byte packetStatus) {
    return (packetStatus == GOOD_PACKET);
}


This is conversion at this point [updated]:

int PACKET_NOT_FOUND = 0;
int BAD_PACKET = 1;
int GOOD_PACKET = 2;

// Set the Max size of the Serial Buffer or the amount of data you want to send+2
// You need to add 2 to allow the prefix and suffix character space to send.
int MAX_SIZE = 128;


public class StreamSend {
  //Preset Some Default Variables
  //Can be modified when seen fit
  char _prefixChar = 's';   // Starting Character before sending any data across the Serial
  char _suffixChar = 'e';   // Ending character after all the data is sent
  int _maxLoopsToWait = -1; //Set to -1 for size of current Object and wrapper

  public byte receiveObject(float ptr, int objSize, int loopSize, char prefixChar, char suffixChar) {

    int maxLoops = (_maxLoopsToWait == -1) ? (objSize + getWrapperSize()/8) : _maxLoopsToWait;

    if (loopSize >= maxLoops)
    {
      return (byte) PACKET_NOT_FOUND;
    }
    if (myPort.available() >= (objSize + getWrapperSize()/8)) // Packet meets minimum size requirement
    {
    if (myPort.read() !=  (byte) prefixChar)
    {
      // Prefix character is not found
      // Loop through the code again reading the next char
      return receiveObject(ptr, objSize, loopSize+1, prefixChar, suffixChar);
    }

    byte[] inBuffer = new byte[73];
    inBuffer = myPort.readBytes();
    //myPort.readBytes(data, 72); //Read the # of bytes
    //String myString = new String(inBuffer);

    //memcpy(ptr, data, objSize); //Copy the bytes into the struct

    if (inBuffer[72] != (byte) suffixChar)
    {    
      //Suffix character is not found
      return (byte) BAD_PACKET;
    }
      for(int i = 1; i < 72; i+=4) {
        println(get4bytesFloat(inBuffer, i));
      }
      println("--------------------------------");
      return (byte) GOOD_PACKET;
    }
    return (byte) PACKET_NOT_FOUND; //Prefix character wasn't found so no packet detected
  }

  public boolean isPacketNotFound(byte packetStatus)
  {
    return (packetStatus == PACKET_NOT_FOUND);
  }
  public boolean isPacketCorrupt(byte packetStatus)
  {
return (packetStatus == BAD_PACKET);
  }
  public boolean isPacketGood(byte packetStatus)
  {
   return (packetStatus == GOOD_PACKET);
  }

  private int getWrapperSize()
  {
    return Byte.SIZE * 2;
  }

  float get4bytesFloat(byte[] data, int offset) { 
    String hexint=hex(data[offset+3])+hex(data[offset+2])+hex(data[offset+1])+hex(data[offset]); 
    //String hexint= hex(data[offset]) +hex(data[offset+1])+hex(data[offset+2])+hex(data[offset+3]); 
    return Float.intBitsToFloat(unhex(hexint)); 
  } 
}

What I should be seeing is numbers on the of: 0.03867566 -0.0266627 1.0671086

but what I am getting is: 3.745884E30 -23.604702 5.834679E-39

if I reverse the byte order in get4floats: 4.0090885E-7 -2.666742E-34 2.0740043E25

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.