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.
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.
www.keil.com/.../
I'm a bit unclear what is the actual question here:
* Is it how to design a protocol?
* Is it how to implement a protocol from a given design?
The former would be entirely independent of the hardware & programming language.
The latter seems to be suggested by giving the frame structure: STX|CMD|LENGTH|DATA|CHECKSUM|ETX.
Or perhaps it's a bit of both - and the OP is confusing himself with implementation details before the design is complete ... ?
When it comes to serial protocols (such as used over RS-485), it is extremely wise to seriously consider implementation when creating the design. Hence my comment regarding kludges and fudges.
Most importantly for many serial implementations is consideration of error detection and recovery. That consideration is frequently not present when protocols are first conceived.
View all questions in Keil forum