This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How can I create a user defined protocol for communication over UART?

I need to use STX, ETX, Length, Data, Checksum , CMD(Command) bytes in my protocol structure. Data and Length will be variable fields where length refers to length of data. I am new to Embedded c and just need an idea, where i can start as I am confused that, what kind of data(structure of data) user should sent over uart to initiate communication with micro controller. The protocol structure I am trying to develop is as follows:

STX|CMD|LENGTH|DATA|CHECKSUM|ETX.

Thanks in advance.

Parents
  • This is a topic that is frequently underestimated. There are so many bad protocols (especially in terms of resilience and efficiency).

    I'll start you off with what I think is one of the most fundamental questions to consider:

    You would potentially use the ASCII control characters STX and ETX to represent 'Start Of Transmission' and 'End Of Transmission' respectively. Would you allow the characters 0x02 and 0x03 to appear within the remainder of your message (such as within CMD, Length, Data, Checksum)?

    If you don't, then synchronisation within your receive routine is considerably easier, since you can treat any received STX as a start of a message and restart message accumulation accordingly. Then keep accumulating until you receive an ETX (or detect an error). You can consider having something like an escape sequence to allow the bytes 0x02 and 0x03 to appear within your data lying between the message delimiters.

    You can, of course have a protocol that use the message delimiters and also permit any data within the block.

    I've seen (and had to use) many badly designed protocols. Some done by self-proclaimed communication experts. They are, in my opinion, an indication of an amateur design. It's often obvious how fudges and kludges are added to both protocol and communication routines to work around flaws introduced at the start.

    If you can, look around for examples of existing protocols and see what you can learn from them.

Reply
  • This is a topic that is frequently underestimated. There are so many bad protocols (especially in terms of resilience and efficiency).

    I'll start you off with what I think is one of the most fundamental questions to consider:

    You would potentially use the ASCII control characters STX and ETX to represent 'Start Of Transmission' and 'End Of Transmission' respectively. Would you allow the characters 0x02 and 0x03 to appear within the remainder of your message (such as within CMD, Length, Data, Checksum)?

    If you don't, then synchronisation within your receive routine is considerably easier, since you can treat any received STX as a start of a message and restart message accumulation accordingly. Then keep accumulating until you receive an ETX (or detect an error). You can consider having something like an escape sequence to allow the bytes 0x02 and 0x03 to appear within your data lying between the message delimiters.

    You can, of course have a protocol that use the message delimiters and also permit any data within the block.

    I've seen (and had to use) many badly designed protocols. Some done by self-proclaimed communication experts. They are, in my opinion, an indication of an amateur design. It's often obvious how fudges and kludges are added to both protocol and communication routines to work around flaws introduced at the start.

    If you can, look around for examples of existing protocols and see what you can learn from them.

Children