Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

First we look at how to post binary data to a MOOS variable.  The type of MOOS variable you post depends on how you call the Notify() method.  

Code Block
// some data we want to send
vector<unsigned char> packet;
packet.push_back(0x61);
packet.push_back(0x00);
packet.push_back(0x62);

// binary string - pass a pointer to Notify along with the size 
m_Comms.Notify( "ACOMMS_TRANSMIT_DATA_BINARY", &packet[0], packet.size() );

// string - convert to a string and pass to Notify
m_Comms.Notify( "ACOMMS_TRANSMIT_DATA", string( (char*) &packet[0], packet.size() ) );

The In the first method will yield we get a binary string moos variable with the second will give us just a normal string.  The first is safer because it will correctly handle an 0x00 value whereas the second will not.  We want our variables to be flagged as normal strings only, so use the second method.  To get your data from ACOMMS_RECEIVED_DATA on the receiving side:

...

MOOS variable by passing Notify() a pointer to our data along with the size of the data.  In the second method we pass Notify() a string, so we get a string MOOS variable.  Using the code above, ACOMMS_TRANSMIT_DATA_BINARY will contain our complete data - 0x61,0x00,0x62 - whereas ACOMMS_TRANSMIT_DATA will only contain 0x61.  iacomms_driver posts to both of these variables when it starts up, so if you try to post the wrong format in your program it simply won't get posted.  

Reading binary data is simple because you can simply use GetString() in either case.  Once you have a string, the .data() method will give you a pointer to the data itself inside the string that you can then use with methods like memcpy.  See endianness and data serialization for more.