[last updated: 2025-01-27]
RS485 home page
GMCGUI RS485 software - data packet protocol and data validation
-----
Hardware Control of send/receive:
- MAX modules send on their RO lines (Receiver Output), receive on their DI lines (Driver Input).
- They cannot send and receive at the same time, so you have to tell them what to do, through their hardware "direction control" lines, DE & RE
- DE is Driver Enable (for transmitting), and is active HIGH
- RE is Receive Enable, and is active LOW
- In all my projects so far, DE & RE are tied together and connected to an Arduino output pin. When the Arduino outputs a HIGH, sending is enabled and receiving is disabled; when the Arduino outputs a low to DE&RE, sending is disabled and receiving is enabled.
- I saw a forum note that if there are other masters on the same bus, then it might be useful to control these lines separately, but I have not investigated to understand how that might work.
-------------------------------------
SoftwareSerial library (built in to IDE) is used to communicate with the MAX module:
- Basic sketch code:
- in globals:
#include <SoftwareSerial.h>
// Arduino pin definitions:
const int RxPin = 4; // Serial Receive pin
const int TxPin = 3; // Serial Transmit pin
const int TxRxPin = 2; // forRS485 Direction control
SoftwareSerial RS485(RxPin, TxPin); // Rx, Tx
baudRate485 = 57600;
byte byteRcvd;
- in setup:
pinMode(TxRxPin, OUTPUT);
// (setting pinMode for TxPin and RxPin seems to be done in SoftwareSerial instance above)
// Start the software serial port on RS485 bus
RS485.begin(baudRate485); // set the data rate
digitalWrite(TxRxPin, LOW); // Initialize MAX485 module as Receiver
- convenient functions to define:
void setTransmitMode () {
digitalWrite(TxRxPin, HIGH);
}
void setReceiveMode () {
digitalWrite(TxRxPin, LOW);
}
- in Loop: (for sending/receiving single bytes of data)
// to send a byte to the bus:
setTransmitMode();
RS485.write(0x44); // send some arbitrary byte
// to receive a byte from the bus:
setReceiveMode();
if (RS485.available()) { //Look for data from RS485 bus
byteRcvd = RS485.read(); // Read received byte
}
- NOTE:
In a real-world application, this minimal code may not work as is.
Data signal timing considerations, packet design, and data validation protocols
must also be taken into account to build a working system.
-------------------------------------
Packet configuration and Data Validation protocol:
---------------------------------------------------
Programming ideas:
- Nominal/proposed program:
- at default, node 00 owns bus
- at all times, all nodes are listening:
remote nodes are also constantly monitoring their sensors
if they detect sensor activity that they must report,
then they request bus ownership (how?)
- the process of listening:
if start code detected, start recording packet
if addressee not for this node, stop recording
if addressee is this node,
record packet until stop code
test CRC
send response
-
- if a node needs to send a packet to the bus:
- am I master?
- if yes:
listen and wait until bus is quiet
put warning to bus that traffic is coming
wait a moment then put your packet onto the bus
- if no:
listen and wait until bus is quiet
send request for bus ownership
---------------------------------------------------
Reference:
.
.
.
eof