I'm curious, I haven't seen much (at all really) in the way of binary conversion for sending integers over serial communication, and I was wondering if there was a reason for that. It just seems like it would be an easier thing (on the back end) to do, rather than convert the the ints into individual ASCII characters, sending them over, only to convert the characters to a string, then back into a int. For one int, it can take 3-5 bytes with this method (with a line feed) vs. 3 with a binary conversion (since ints are stored as a 2 byte value, and including a line feed).
I have been playing with doing something like this on the Arduino side:
The only reason I frequently advise serially transmitting human-readable text is that it is, well, human readable. It makes it so much easier to debug code with any complexity. It's no coincidence that the WWW uses plain text for encoding pages. You can easily run into synchronization problems when building systems that communicate in binary, but hey, what you propose should work fine and it will certainly be more efficient.
You can definitely do this, although I haven't been using serial much, I've been doing similar w/ i2c that is a one-byte-at-a-time protocol. Fortunately, it has a master/slave concept, so it makes synchronization much easier.
I use the first byte as a header to tell the slave what to expect (3 bits for a command code, 4 bits for the number of bytes I'm going to transmit it afterwards, and one bit could be used for something else later), the slave then reads the required bytes into a buffer, and then passes the command and buffer on to a function that directs the code. It checks for valid command types, the proper number of bytes being filled out, etc. Commands can be things like: set a variable, return a status, take an action. Then I request back a number of bytes as a response. Using serial, you could add one more byte to the header to just include the response count. If the slave responds with a null byte starting, that means it didn't understand the command. All other responses have at least a value of 1 (I use the lowest bit always set to 1, so in a single-byte response there are 7 bits available) for the starting byte.
Of course, I'm with Mikal on this one, if you already have the overhead of Serial, might as well make it easy at that point =)
Makes sense. The problem that I have had, and the reason I went down this path, is the converting of the ASCII text to ints has not always been the most reliable (even in the examples), so I wanted to try and keep the number of conversions to a minimum.
But yeah, keeping it readable and easy makes sense.