...
Code Block |
---|
#include <iostream> #include <string.h> #include <vector> #include <math.h> using namespace std; // our structure for sending data struct MY_DATA { unsigned char packet_id; int latitude; int longitude; unsigned short heading; }; int main() { // fill a struct with data MY_DATA data_to_send; data_to_send.packet_id = 0x01; data_to_send.latitude = (int) (42.358456 * pow(10,7)); data_to_send.longitude = (int) (-71.087589 * pow(10,7)); data_to_send.heading = 185; // construct our array/vector and fill it with data vector<unsigned char> packet (sizeof(data_to_send), 0); memcpy(&packet[0], &data_to_send.packet_id, sizeof(data_to_send)); // to get our data out, do the reverse MY_DATA data_received; memcpy(&data_received.packet_id, &packet[0], sizeof(data_to_send)); // print out the data cout << "packet id: 0x" << hex << (int) data_received.packet_id << dec << endl; cout << "lat: " << data_received.latitude / pow(10,7) << endl; cout << "lon: " << data_received.longitude / pow(10,7) << endl; cout << "heading: " << data_received.heading << endl; return 0; } |
Posting to MOOS
Writing a MOOS app and need to post your array of data to ACOMMS_TRANSMIT_DATA? Here's two ways - one good and one bad:
Code Block |
---|
// bad - pass a pointer to Notify along with the size
m_Comms.Notify( "ACOMMS_TRANSMIT_DATA", &packet[0], packet.size() );
// good - convert to a string and pass to Notify
m_Comms.Notify( "ACOMMS_TRANSMIT_DATA", string( (char*) &packet[0], packet.size() ) );
|
While these two methods might look the same, and in fact calling the first will ultimate use the same string constructor as the second inside the Notify method, they do have slightly different outcomes. Using the first method will flag the message as a binary string while the latter will flag it as a normal string. Just like a moos variable can't be a double and a string, it also can't be a binary string and a normal string (even though they're the same). 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:
...
See Binary acomms data in MOOS for information on getting your serialized data into or out of a MOOS message.