You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

The third MOOS variable type

While we usually thing of MOOS variables as having only two types - double and string - a third type called "binary string" is also defined.  While any string can hold binary data, the character 0x00 is special and usually used to indicate the end of a string.  That means if you tried to send some binary data that contained the byte 0x00 in the middle using a normal string MOOS variable, everything after the 0x00 would be lost.  A binary string MOOS variable keeps track of its data length separately so it knows to keep looking for data after the 0x00.  

iacomms_driver has two input variables to transmit data - ACOMMS_TRANSMIT_DATA and ACOMMS_TRANSMIT_DATA_BINARY.  As expected, the first is a string variable and the second a binary string.  You can still post binary data to ACOMMS_TRANSMIT_DATA as long as there isn't an 0x00.  Both messages are handled identically inside the driver.  

Implementation

First we look at how to post binary data to a MOOS variable.  

string data_payload = "hello world";
vector<unsigned char> packet;


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

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

The first method will yield 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:

// perhaps we copied the string in onNewMail
string msg_string = msg.GetString();

// the .data() method gives us a pointer to the string's data in contiguous memory
memcpy(&data_received.packet_id, msg_string.data(), msg_string.size());
  • No labels