...
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 slightly different ways.
Code Block |
---|
// 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:
...
See Binary acomms data in MOOS for information on getting your serialized data into or out of a MOOS message.