Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

...

Now say we want to send a uint16 (unsigned short) through acomms along with a bunch of other data.  Well  We'll probably be constructing a long array or of bytes and copying our data into it for transmission, then picking it back out at the other end.  Sticking with our simple uint16, let's look at how we can reconstruct the number given an array of bytes.  

...

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:

Code Block

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

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

Notify will actually use the string constructor in the second method if you pass it a pointer as in the first method, so these two methods are identical.  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.