144 lines
3.2 KiB
Plaintext
Raw Normal View History

2023-02-05 20:00:17 +01:00
# Messages
2023-05-11 11:14:17 +02:00
Examples of command/answer protocols over RS485.
2023-02-05 20:00:17 +01:00
(to be elaborated)
2023-05-11 11:14:17 +02:00
This is part of the RS485 class - https://github.com/RobTillaart/RS485
2023-02-05 20:00:17 +01:00
#### Guidelines
2023-05-11 11:14:17 +02:00
Designing a protocol is most often not trivial and might need more
than one iteration.
2023-02-05 20:00:17 +01:00
- Keep protocols as simple as possible.
2023-05-11 11:14:17 +02:00
- Make sure that baud rates match between the nodes in the network.
- Software Serial can work when time between requests is long enough.
It depends on how much processing is needed.
- Be aware of implications of future expansion.
Design your protocol with room for additional commands.
Or even a version number in it.
- In designing a protocol be aware of ambiguities.
An request or answer may contain bytes (sequences)
that might trigger other nodes.
- If you request data from a node, it might happen
that it won't answer or that the answer fails to
arrive in correct state (missing bytes, flipped bits etc.)
2023-02-05 20:00:17 +01:00
#### ASCII codes
Possible useful char codes.
| commando | value | meaning |
|:----------:|:-------:|:----------|
| SOH | 0x01 | start of header
| STX | 0x02 | start of text
| ETX | 0x03 | end of text
| EOT | 0x04 | end of transmission
| ACK | 0x06 | ACKnowledge
| NAK | 0x15 | Not Acknowledge
| CAN | 0x18 | CANcel
2023-05-11 11:14:17 +02:00
See also **ASCII_CONTROL.h**
2023-02-05 20:00:17 +01:00
----
## example 1
A minimal message protocol consisting of 1 byte commands
command = (0x80 | 7 bits command)
answer = (0x00 | 7 bits answer)*
All command are coded in a single byte with 0x80 bit set.
All answers bytes 0 or more (sender knows how many to expect, or a specific end char.
requirement:
- All devices listen to a disjunct command set.
2023-05-11 11:14:17 +02:00
----
2023-02-05 20:00:17 +01:00
## example 2
2023-05-11 11:14:17 +02:00
Using a device id to send the command to.
2023-02-05 20:00:17 +01:00
command :=
SOH start of header, (attention new command)
deviceID to (is it for me?)
command (if so, exec command)
bytes expected (to generate the bytes)
answer :=
bytes requested bytes (send them back)
2023-05-11 11:14:17 +02:00
----
2023-02-05 20:00:17 +01:00
## example 3
2023-05-11 11:14:17 +02:00
Command and answer have same layout.
2023-02-05 20:00:17 +01:00
Uses device ID's to address specific device.
SOH start of header 0x01
deviceID to
deviceID sender
length length of message
message idem in ASCII
optional extend with:
checksum optional
EOT optional (end of transmission)
2023-05-11 11:14:17 +02:00
----
2023-02-05 20:00:17 +01:00
## example 4
More complex package with multiple fields and crc per message.
SOH start of header
deviceID to
deviceID sender
fields 0 or more
length length of field 1
STX start of text
message idem
CHECKSUM idem, message only!
ETX end of text
length length of field 2
STX start of text
message idem
CHECKSUM idem, message only!
ETX end of text
...
EOT end of transmission
----
2023-05-11 11:14:17 +02:00
## example 5 (still alive)
Maybe most simple protocol, just send an ID,
and if the slave is alive it responds with its ID.
Think of it as the **PING** command
command = ID
answer = ID
Could be embedded in other protocols too,
----
2023-02-05 20:00:17 +01:00
## Future
- binary protocols
2023-05-11 11:14:17 +02:00
2023-02-05 20:00:17 +01:00