Back
Loading views...RS_485
RS_485 Protocol
Overview
RS-485 is a differential serial communication standard that allows multi-drop networks of up to 32 (or more with repeaters) devices over distances up to 1200m. It is the most widely used physical layer for industrial serial networks including Modbus RTU.
1. Theory & Fundamentals
- Solves: Reliable serial communication over long distances in noisy industrial environments
- Physical layer: Differential pair (A and B lines); logic based on voltage difference
- Voltage: ±1.5V to ±6V differential; common mode range −7V to +12V
- Distance: Up to 1200m at 100kbps; shorter at higher speeds
- Devices: Up to 32 unit loads (standard) or 256+ with 1/8 UL transceivers
- Half-duplex (2-wire) or full-duplex (4-wire)
- Speed: Up to 10 Mbps (short distances)
2. Frame / Packet Structure
No dedicated frame format — uses UART framing (typically 8N1 or 8E1):
Network topology: Daisy chain or star (with hub)
Bus: A ─────────────────────────── (all devices share)
B ───────────────────────────
Termination: 120Ω at each end of the bus
Biasing: 560Ω pullup on A, 560Ω pulldown on B (failsafe)
One device drives at a time (half-duplex):
DE (Driver Enable) = HIGH to transmit
RE_n (Receiver Enable) = LOW to receive
3. Protocol Mechanics
- Only one device transmits at a time (half-duplex requires direction control)
- Direction control via DE/RE pins using GPIO
- Collision: No built-in — handled by protocol layer (e.g., Modbus token passing)
- Termination: 120Ω at both ends prevents reflections
- Biasing: Failsafe resistors ensure defined idle state when bus is open
4. Hardware Implementation
- MAX485, SP485, SN75176 — RS-485 transceiver ICs
- DE and RE pins connected to one GPIO (active HIGH = transmit)
- Termination: 120Ω end-of-line resistors on long buses
- Bias resistors: 560Ω pull-up on A, 560Ω pull-down on B
- Cable: Use twisted pair (Cat5, RS-485 cable) for differential noise immunity
- Isolation: Use ADM2483 or similar for galvanic isolation in industrial environments
5. Register-Level / Configuration
// Direction control GPIO
#define RS485_DE_PIN GPIO_PIN_1
#define RS485_DE_PORT GPIOA
void RS485_TX_Enable(void) { GPIOA->BSRR = GPIO_BSRR_BS1; } // DE=HIGH
void RS485_RX_Enable(void) { GPIOA->BSRR = GPIO_BSRR_BR1; } // DE=LOW
void RS485_SendBuffer(uint8_t *data, uint16_t len) {
RS485_TX_Enable();
for (int i = 0; i < len; i++) {
while (!(USART1->SR & USART_SR_TXE));
USART1->DR = data[i];
}
while (!(USART1->SR & USART_SR_TC)); // Wait transmission complete
RS485_RX_Enable(); // Switch back to receive
}
6. Driver / Software Development
// Modbus RTU style framing
typedef struct {
uint8_t address;
uint8_t function;
uint8_t data[252];
uint16_t crc;
} ModbusFrame;
uint16_t CRC16_Modbus(uint8_t *buf, uint16_t len) {
uint16_t crc = 0xFFFF;
for (int i = 0; i < len; i++) {
crc ^= buf[i];
for (int j = 0; j < 8; j++)
crc = (crc & 1) ? (crc >> 1) ^ 0xA001 : (crc >> 1);
}
return crc;
}
7. Debugging & Testing
- Check bus voltages: A−B should be ≥+200mV for MARK, ≤−200mV for SPACE
- Common bugs: Missing termination → ringing and bit errors; missing biasing → undefined idle state
- Direction control timing: DE must be asserted before first bit, deasserted after last bit
- Cable not twisted pair → high noise susceptibility
- Grounding: RS-485 requires common ground reference between nodes
8. Real-World Applications
- Modbus RTU: Industrial sensor/actuator networks (temperature, pressure, motor drives)
- DMX512 lighting: RS-485 used as physical layer for stage lighting control
- Building automation (BACnet MS/TP): HVAC and building control systems
- Multi-drop sensor networks: 32+ sensors on one 2-wire bus
- Long-distance SCADA: Remote terminal units communicating to control room
9. Advanced Topics & Edge Cases
- RS-485 repeaters: Extend bus beyond 32 nodes or 1200m
- Galvanic isolation: ADM2483, ISO3082 for safety and noise immunity
- 4-wire RS-485: Full-duplex for simultaneous TX/RX (rare)
- RS-485 to RS-232 converters for legacy equipment
- Automatic direction control: Some UARTs support automatic DE/RE control
10. Standards & Variants
| Standard | Wires | Nodes | Distance | Speed |
|---|---|---|---|---|
| RS-232 | 3+ | 2 | 15m | 115kbps |
| RS-485 2-wire | 2+GND | 32 | 1200m | 10Mbps |
| RS-485 4-wire | 4+GND | 32 | 1200m | 10Mbps |
| RS-422 | 4+GND | 1:10 | 1200m | 10Mbps |
| CAN | 2+GND | 127 | 500m | 1Mbps |
💡 Practical Examples
Example 1: Simple RS-485 send/receive
RS485_TX_Enable();
RS485_Send("READ\r\n", 7);
RS485_RX_Enable();
uint8_t response[64];
RS485_Receive(response, 64, 100); // timeout 100ms
Example 2: Modbus RTU read holding registers
uint8_t req[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x0A}; // addr=1, FC=3, reg=0, count=10
uint16_t crc = CRC16_Modbus(req, 6);
req[6] = crc & 0xFF; req[7] = crc >> 8;
RS485_SendBuffer(req, 8);
Example 3: Multi-slave polling loop
for (uint8_t addr = 1; addr <= 10; addr++) {
ModbusRead(addr, 0x0000, 1); // Poll each slave
delay_ms(10);
}
🧪 Practice Questions
Beginner
- What is the advantage of differential signaling in RS-485?
- How many devices can share one RS-485 bus?
- What is the purpose of termination resistors?
- What is DE pin used for in an RS-485 transceiver?
- What is the maximum cable length at 9600 baud?
Intermediate
- Why do you need failsafe biasing resistors on an RS-485 bus?
- How do you implement direction control in firmware to avoid bus contention?
- Explain the difference between 2-wire and 4-wire RS-485.
- How does Modbus RTU use RS-485 for multi-device communication?
- Calculate the CRC-16 for the Modbus frame: {0x01, 0x03, 0x00, 0x00, 0x00, 0x01}.
Advanced
- Design a 32-node RS-485 Modbus RTU sensor network with error recovery.
- Implement automatic direction control using UART TC interrupt.
- How would you add galvanic isolation to an RS-485 network for safety?
- Design RS-485 bus topology for a 1000m installation with branches.
- Implement a Modbus RTU master with timeout and retry logic.
Hands-on Projects
- Modbus RTU Master: Poll 4 slave devices, collect sensor data, display on LCD.
- DMX512 Controller: Drive 3 RGB LED fixtures with RS-485 DMX protocol.
- RS-485 Sensor Network: 10 nodes, each sends temperature + humidity every second.
Checklist
- [ ] Build MAX485 circuit with DE/RE control
- [ ] Add termination and biasing resistors correctly
- [ ] Implement direction control in firmware with TC interrupt
- [ ] Test with 2 nodes communicating
- [ ] Implement Modbus RTU CRC-16 calculation
- [ ] Poll multiple slaves on bus
- [ ] Measure differential voltages with oscilloscope
- [ ] Debug with logic analyzer
- [ ] Add galvanic isolation for industrial use
- [ ] Build a 10+ node network and test at full distance