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.
|
2023-11-21 16:01:37 +01:00
|
|
|
Or even add a version number in it.
|
2023-05-11 11:14:17 +02:00
|
|
|
- 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
|
|
|
|
|
|
|
|
|
|
|
----
|
|
|
|
|
2023-11-21 16:01:37 +01:00
|
|
|
## example 1 - simplest?
|
2023-02-05 20:00:17 +01:00
|
|
|
|
|
|
|
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:
|
2023-11-21 16:01:37 +01:00
|
|
|
- All devices listen to a (possibly) disjunct command set.
|
2023-02-05 20:00:17 +01:00
|
|
|
|
|
|
|
|
2023-05-11 11:14:17 +02:00
|
|
|
----
|
2023-02-05 20:00:17 +01:00
|
|
|
|
2023-11-21 16:01:37 +01:00
|
|
|
## example 2 - add device ID
|
2023-02-05 20:00:17 +01:00
|
|
|
|
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 :=
|
2023-11-21 16:01:37 +01:00
|
|
|
{
|
2023-02-05 20:00:17 +01:00
|
|
|
SOH start of header, (attention new command)
|
2023-11-21 16:01:37 +01:00
|
|
|
deviceID to (is it for me?) should not equal SOH
|
2023-02-05 20:00:17 +01:00
|
|
|
command (if so, exec command)
|
|
|
|
bytes expected (to generate the bytes)
|
2023-11-21 16:01:37 +01:00
|
|
|
}
|
2023-02-05 20:00:17 +01:00
|
|
|
|
|
|
|
answer :=
|
2023-11-21 16:01:37 +01:00
|
|
|
{
|
2023-02-05 20:00:17 +01:00
|
|
|
bytes requested bytes (send them back)
|
2023-11-21 16:01:37 +01:00
|
|
|
}
|
2023-02-05 20:00:17 +01:00
|
|
|
|
2023-05-11 11:14:17 +02:00
|
|
|
----
|
|
|
|
|
2023-11-21 16:01:37 +01:00
|
|
|
## example 3 - PRO I
|
2023-02-05 20:00:17 +01:00
|
|
|
|
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.
|
|
|
|
|
2023-11-21 16:01:37 +01:00
|
|
|
{
|
2023-02-05 20:00:17 +01:00
|
|
|
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-11-21 16:01:37 +01:00
|
|
|
}
|
2023-02-05 20:00:17 +01:00
|
|
|
|
2023-05-11 11:14:17 +02:00
|
|
|
----
|
|
|
|
|
2023-11-21 16:01:37 +01:00
|
|
|
## example 4 - PRO II
|
|
|
|
|
|
|
|
More complex package with multiple fields and Checksum / CRC per message.
|
|
|
|
In fact this is an elaborated variation on previous example.
|
2023-02-05 20:00:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
SOH start of header
|
|
|
|
deviceID to
|
|
|
|
deviceID sender
|
|
|
|
fields 0 or more
|
2023-11-21 16:01:37 +01:00
|
|
|
|
2023-02-05 20:00:17 +01:00
|
|
|
length length of field 1
|
|
|
|
STX start of text
|
|
|
|
message idem
|
|
|
|
CHECKSUM idem, message only!
|
|
|
|
ETX end of text
|
2023-11-21 16:01:37 +01:00
|
|
|
|
2023-02-05 20:00:17 +01:00
|
|
|
length length of field 2
|
|
|
|
STX start of text
|
|
|
|
message idem
|
|
|
|
CHECKSUM idem, message only!
|
|
|
|
ETX end of text
|
|
|
|
...
|
|
|
|
EOT end of transmission
|
|
|
|
|
|
|
|
----
|
|
|
|
|
2023-11-21 16:01:37 +01:00
|
|
|
## example 5 - PING still alive
|
2023-05-11 11:14:17 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2023-11-21 16:01:37 +01:00
|
|
|
If the slave is a single function device (light switch) it might just
|
|
|
|
toggle upon receiving its ID.
|
|
|
|
|
|
|
|
Note this simple protocol cannot discriminate between an command and answer.
|
|
|
|
|
|
|
|
|
|
|
|
A **PING** command could be embedded in other protocols too,
|
|
|
|
|
|
|
|
----
|
|
|
|
|
|
|
|
## example 6 - Get value
|
|
|
|
|
|
|
|
Reading a simple slave could be done with a one byte command.
|
|
|
|
|
|
|
|
command = ID
|
|
|
|
answer = returns value. // this should never contain an ID of another device.
|
|
|
|
|
|
|
|
An example is remote reading a sensor or an analog port.
|
|
|
|
|
|
|
|
To improve the reliability the answer can be a "packet".
|
|
|
|
|
|
|
|
command = ID
|
|
|
|
answer = { SOH, IDmaster, IDslave, length, value }
|
|
|
|
|
|
|
|
|
2023-05-11 11:14:17 +02:00
|
|
|
|
|
|
|
----
|
|
|
|
|
|
|
|
|
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
|
|
|
|