title: A good serial communications protocol/stack for embedded devices?
body: <p>After writing several different custom serial protocols for various projects, I've started to become frustrated with re-inventing the wheel every time.  In lieu of continuing to develop custom solutions for every project,  I've been searching for a more general solution.  I was wondering if anyone knows of a serial protocol (or better yet, implementation) that meets the following requirements:</p>
  
  <ul>
  <li>Support multiple devices.  We'd like to be able to support an RS485 bus.</li>
  <li>Guaranteed delivery.  Some sort of acknowledgement mechanism, and some simple error detection (CRC16 is probably fine).</li>
  <li>Not master/slave.  Ideally the slave(s) would be able to send data asynchronously.  This is mostly just for aesthetic reasons, the concept of polling each slave doesn't feel right to me.</li>
  <li>OS independence.  Ideally it wouldn't rely on a preemptive multitasking environment at all.  I'm willing to concede this if I can get the other stuff.</li>
  <li>ANSI C.  We need to be able to compile it for several different architectures.</li>
  </ul>
  
  <p>Speed isn't too much of an issue, we're willing to give up some speed in order to meet some of those other needs.  We would, however, like to minimize the amount of required resources.</p>
  
  <p>I'm about to start implementing a sliding window protocol with piggybacked ACKs and without selective repeat, but thought that perhaps someone could save me the trouble.  Does anyone know of an existing project that I could leverage?  Or perhaps a better strategy?</p>
  
  <p><strong>UPDATE</strong><br>
  I have seriously considered a TCP/IP implementation, but was really hoping for something more lightweight.  Many of the features of TCP/IP are overkill for what I'm trying to do.  I'm willing to accept (begrudgingly) that perhaps the features I want just aren't included in lighter protocols.</p>
  
  <p><strong>UPDATE 2</strong><br>
  Thanks for the tips on CAN.  I have looked at it in the past and will probably use it in the future.  I'd really like the library to handle the acknowledgements, buffering, retries etc, though.  I guess I'm more looking for a network/transport layer instead of a datalink/physical layer.</p>
  
  <p><strong>UPDATE 3</strong><br>
  So it sounds like the state of the art in this area is:  </p>
  
  <ul>
  <li>A trimmed down TCP/IP stack.  Probably starting with something like <a href="http://savannah.nongnu.org/projects/lwip/" rel="noreferrer">lwIP</a> or <a href="http://www.sics.se/~adam/uip/index.php/Main_Page" rel="noreferrer">uIP</a>.  </li>
  <li>A CAN based implementation, it would probably rely heavily on the CAN bus, so it wouldn't be useful on other physical layers.  Something like <a href="http://www.canfestival.org/" rel="noreferrer">CAN Festival</a> could help along the way.</li>
  <li>An HDLC or SDLC implementation (like <a href="http://sourceforge.net/projects/opensourcehdlc/" rel="noreferrer">this one</a>).  This is probably the route we'll take.</li>
  </ul>
  
  <p>Please feel free to post more answers if you come across this question.</p>