Please follow the protocol

As you have seen, the hardware part of the Glowing Marque prototype, build from an old scanner, is functioning. The last step I did is the programming of a protocol to communicate with the Arduino by the Insek Proxy Internet to Serial Proxy app.

The first issue is that I want to send a string of hexadecimal values and then convert it to real hex in the Arduino for displaying this data. The second problem to solve is to send a longer string that the serial buffer and the buffer of the Messenger library.

There is the piece of code I did for the protocol:

#include <Messenger.h>

#define DATASIZE 70
unsigned char data[DATASIZE+1]; // + 1 space for the closing \0 char.

Messenger message = Messenger();

/*
	2010-11-03: Append data.
	Args: source, target, position to start from zero to DATASIZE.
*/
void appendData(char * s, unsigned char * dp, int pos) {

	// Create and reset the pointer to display data.
	dp = dp + pos; // Move to starting position.

	char monkey[3];
	long chimp;
	for (int i=0; i<strlen(s); i=i+2) {
		monkey[0] = s[i];
		monkey[1] = s[i+1];
		monkey[2] = '\0';

		// Convert the monkey string to long int.
		chimp = strtol(monkey, NULL, 16);

		// Append chimp to displayData;
		*dp++ = (unsigned char)chimp; // Cast the long to an unsigned char.

		//Serial.print(chimp, BYTE);
	}

	*dp = '\0'; // Close the string.

}

void messageCompleted() {

	//Serial.println("Message completed.");

	while ( message.available() ) {
		//Serial.print(".");
		if ( message.checkString("data") ) { // Show text.

			if( message.checkString("flush")) {
				flushData();
				Serial.println("\nData flushed.");

			} else {
				// 1st get position
				int pos = message.readInt();

				// 2nd get length
				int length = message.readInt();

				// 3rd get data
				char tmp_data[128];

				// Move data to the temps string.
				message.copyString(tmp_data, 128);

				// Check if data length
				// Find the first position of the \0.
				int data_length = strlen(tmp_data);

				// Data are always 2 char to build one hex value.
				if( length*2 == data_length) {

						if( pos + data_length - 1 > DATASIZE) {
							Serial.println("\nData overflow.");
						} else {

						//Serial.println(strlen(tmp_data));

						//Serial.println(tmp_data);

						appendData(tmp_data, &data[0], pos);

						Serial.println("");
						Serial.println("Data:");

						for(int a=0; a < DATASIZE; a++) {
							if( data[a] == '\0' ) {
								Serial.print("-");
							} else {
								Serial.print(data[a], BYTE);
							}
						}
					}
				} else {
					Serial.println("Bad checksum.");
				}

			} // End append data.

		} else {
			message.readChar(); // Flush the rest of the message buffer.
		}

	}
}

void flushData() {
	for(int i=0; i<DATASIZE+1; i++) {
		data[i] = '\0';
	}
}

void setup() {

	Serial.begin(9600);
	message.attach(messageCompleted);

}
void loop() {
	while ( Serial.available( ) ) message.process(Serial.read( ) );
}

The protocol is relatively simple, there is the structure:

header position length data
  • The ‘header’ is the command, what the Arduino have to do with that string. I use ‘data’ as the header to send data.
  • The ‘position’ is the position in the array the data will be copied.
  • The ‘length’ is the quantity of numbers to send. It’s used to check if the Arduino received the right length of data and to verify if the data will not overflow the data array.

There is an example to send a first packet of data:

data 0 4 ff00ff00

Here I send four numbers build with two hexadecimal characters (from 00 to ff). They will be placed at the beginning of the array.

If I want to append ‘ddee33′ to this value in the array, I shall use this call:

data 4 3 ddee

So this way I can send many blocks of data and built an array longer than the serial buffer.

After that I can implement other calls to work with this data. A sample call can be (not built in this sample code): show data. I did that in code of the Glowing Marquee project, so I send packets of hex data, and ask the Arduino to show it. I will be back with more on this project in my next posts.

5 thoughts on “Please follow the protocol

  1. Hi. Thanks for creating and sharing this software. I think it’s going to be the bridge I require to be able to send data from an Arduino to the Internet

    Specifically, I want to be able to populate a Google Spreadsheet – without, of course, needing an Ethernet Shield.

    Unfortunately, I’m struggling to work out what serial output I require from the Arduino that will make it to the gDoc. Hacking about is getting me nowhere and I haven’t found any online tutorials or examples to shed some light on it.

    Could you assist me at all? Sorry to ask, I know it’s drag.

    with thanks and kind regards

    Marcel

  2. I’ve tried squirt out the appropriate ‘submit’ code for the Google Spreadsheet but ‘Internet to Serial Proxy’s window then just says “Connection failed! Error unsupported URL”

  3. I think you will net to create a PHP script to bridge to the Google Docs API. So the Arduino will talk to the Internet to Serial Proxy, this one will talk to your php (or other language) script and this last one will talk to the Google API. You can’t handle complex connection with the app directly. You can find more about the API, but you will need to understand the protocol.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>