Custom Serial Protocol - Help Needed (HEX to Bin, etc)

Hello All

I'm busy creating my own custom serial protocol to read and write to pins (PWM and Analog read). I have a basic structure that I want to use, but just battling to get the correct method to strip out the binary values.

Will the following approach work? Or will it demand too much processing resulting in delays?

I will appreciate any suggestions and help.

I've divided the serial string as follow:

|S|FFF|FF|FF|FF|FF|FF|FF|FF|FF|FF|FF|FF|FF|FF|FF|X| |1| 2 | 3 |---------4------- -| 5|----------6--------|7|

Field 1 = an "S" will indicated the start of the received string, after that I will substring everything.

Field 2 = FFF in hex will give 12 bits "000000000000". This represents pins 2 - 13 on the Arduino. "0" = pin is an Output "1" = pin is an Input

Field 3 = This field will indicated whether PWM is enabled or not. (Arduino Pin 3,5,6,9,10,11) 6 pins in total. Therefor the FF in hex is sufficient to give 8 bits (the first 6 bits will be used). "0" = PWM Disabled "1" = PWM Enabled

Field 4 = This is the aread for the 6 PWM pins (pins 3,5,6,9,10,11). A value of 0-255 can be written to each and then on the receiving end it can be scaled to 0-1023 for a better resolution. if PWM is enabled, the value will range from 0-255 and Will be represented in HEX i.e FF. If PWM is disabled, The value will either be FF for ON, or 00 for OFF. (This can be used as an Input or Output, depending on the definition is Field 2)

Field 5 = This represents Hex value FF, which will be converted to binary 11111111. These bits indicates the states for all the remaining Digital IO pins ie. 2,4,7,8,12,13. "1" = "ON" "0" = "OFF" (This can be used as an Input or Output, depending on the definition is Field 2)

Field 6 = This is the field for Analog Inputs. Each Analog pin will have its own area of Hex FF which will give a value between 0-255. (Can be scaled to 0-1023 in code).

Field 7 = This field indicated the End of the serial string. "X"

It is alot of information and I hope it makes sense. Any [u]Sample code[/u], comments,insight and suggestions on methods to use will be appreciated.

I am a bit new to serial comms and hex-bin conversions on arduino and still trying to figure out things.

If this protocol works, I'm sure it will be able to help alot of people (If there isn't something else out there providing complete control) If there is, please let me know.

Thanks :-)

Field 2, being 12 bits, would be problematic assuming a byte/character oriented protocol. Expand it to two bytes.

OTOH, I might think about sending the PORTx, DDRx, and PINx registers for each port. Each pin has three possible configurations: input, output and input with pullup, as determined by PORTx and DDRx.

One potential problem is getting out of sync because you are using ASCII values as start and end delimiters but have binary in the middle, these middle bytes could have the same value as the start and stop bytes so you can get in a tangle if you get corruption and lose sync with the frames.

Also you have no error checking, you must really trust serial comms.

strip out the binary values.

Sorry, just re read and saw this, belay my first comment :), some error checking would not go astray though.

a value between 0-255. (Can be scaled to 0-1023 in code).

So you lose 2 bits of resolution?


A normal analogWrite only supports 8 bits, so taking analogRead, dividing by 4 (>>2), sending over as a byte seems reasonable.
Especially for LED brightnesses.

If (Serial.available)>16){
if (‘S’){
for (x=0; x<17; x=x+1){
incomingData =;
if (incomingData[16]== ‘X’){ // got a message with good ending?
/* now parse out the data
incomingData[0,1] = section 2
incomingData[2] = section 3
incomingData[3,4,5,6,7,8] = section 4
incomingData[9] = section 5
incomingData[10,11,12,13,14,15] = section 6
// your code for whatever it is you’re doing
} // end message processing
} // check these, I think I have enough, hard to see in this little reply window

Hello All

Thank you for the suggestions thisfar :-)

@ Jack: Thanx for the comment, will rather use FFFF instead of FFF.

@ Graynomad: I will definitely add error checking once i get the protocol to work. I will perhaps use a LEN field, to check the length of characters and a CRC field perhaps? Regarding the 2-bits that is lost, I did it that way just to keep the string a little shorter, but I think the full 10-bit resolution will do, will find a way to implement it. Thanx

@ CrossRoads: Wow thank you for the example, I will definitely give that a try and let you know ;-)

My aim is to be able to use this protocol to give more control over serial from arduino to arduino (via bluetooth or RF) and want to give more options if you want to use VB applications. Firmata is cool, but lacks complete control. Would be better using a protocol than all the different DLL's, and would make the code a little simpler and neater i think.

Any other suggestions are welcome.

Thank you guys, appreciate it :-)