A Python library and command-line interface for communicating with R4DCB08 8-channel temperature collectors using Modbus RTU (serial) and TCP protocols.
You can check my blog to more usage examples: https://hackthechaos.com/blog/ds18b20-rs485-raspberry-pi/
- Read temperatures from all 8 channels
- Read temperature from individual channels
- Set temperature correction values for calibration
- Read current temperature corrections
- Support for both Modbus RTU (serial) and TCP connections
- Command-line interface for easy automation
- Simple Python API for integration
| Feature | Details |
|---|---|
| Operating Voltage | 6-24V DC |
| Operating Current | 8-13mA (depends on connected DS18B20 sensors) |
| Temperature Range | -55°C to +125°C |
| Accuracy | ±0.5°C from -10°C to +85°C |
| Baud Rates | 1200, 2400, 4800, 9600 (default), 19200 |
| Data Format | N, 8, 1 (No parity, 8 data bits, 1 stop bit) |
| Communication Protocol | Modbus RTU |
| Temperature Channels | 8 channels (DS18B20 sensors) |
| Default Address | 1 |
- Create a Python virtual environment:
python -m venv .venv
source .venv/bin/activate # Linux/macOS
# or
.venv\Scripts\activate # Windows- Install dependencies:
pip install -r requirement.txtpyserial
pymodbus
The r4dcb08_cli.py script provides a complete command-line interface supporting both Modbus RTU (serial) and TCP connections:
python r4dcb08_cli.py {rtu|tcp} [OPTIONS] COMMANDpython r4dcb08_cli.py rtu --port /dev/ttyUSB0 [OPTIONS] COMMANDRTU Connection Options:
--port, -p: Serial port (required, e.g.,/dev/ttyUSB0,COM3)--address, -a: Modbus device address (1-247, default: 1)--baudrate, -b: Baud rate (1200, 2400, 4800, 9600, 19200, default: 9600)--timeout, -t: Communication timeout in seconds (default: 1.0)
python r4dcb08_cli.py tcp --host 192.168.1.100 [OPTIONS] COMMANDTCP Connection Options:
--host, -H: TCP host IP address (required, e.g.,192.168.1.100)--port, -p: TCP port (default: 502)--address, -a: Modbus device address (1-247, default: 1)--timeout, -t: Communication timeout in seconds (default: 1.0)
Read temperatures from all 8 channels:
RTU:
python r4dcb08_cli.py rtu --port /dev/ttyUSB0 --address 1 read-allTCP:
python r4dcb08_cli.py tcp --host 192.168.1.100 read-allOutput:
R4DCB08 Temperature Readings:
-----------------------------------
Channel 0: 22.5°C
Channel 1: 23.1°C
Channel 2: No sensor
Channel 3: 21.8°C
...
Read temperature from a specific channel (0-7):
RTU:
python r4dcb08_cli.py rtu --port /dev/ttyUSB0 read-channel 0TCP:
python r4dcb08_cli.py tcp --host 192.168.1.100 read-channel 3Output:
Channel 0: 22.5°C
Set a temperature correction value for calibration:
RTU:
python r4dcb08_cli.py rtu --port /dev/ttyUSB0 set-correction 3 1.5TCP:
python r4dcb08_cli.py tcp --host 192.168.1.100 set-correction 0 2.1This adds a correction to the specified channel. Corrections can be negative:
python r4dcb08_cli.py rtu --port /dev/ttyUSB0 set-correction 2 -0.8View current correction values for all channels:
RTU:
python r4dcb08_cli.py rtu --port /dev/ttyUSB0 read-correctionsTCP:
python r4dcb08_cli.py tcp --host 192.168.1.100 read-correctionsOutput:
Temperature Correction Values:
-----------------------------------
Channel 0: +0.0°C
Channel 1: +1.2°C
Channel 2: -0.8°C
Channel 3: +1.5°C
...
# RTU (Serial) examples - Linux/macOS
python r4dcb08_cli.py rtu --port /dev/ttyUSB0 --address 1 read-all
python r4dcb08_cli.py rtu --port /dev/ttyUSB0 read-channel 5
python r4dcb08_cli.py rtu --port /dev/ttyUSB0 set-correction 0 2.1
# RTU (Serial) examples - Windows
python r4dcb08_cli.py rtu --port COM3 --address 2 read-all
python r4dcb08_cli.py rtu --port COM3 --baudrate 19200 read-channel 3
# RTU with different device address and baud rate
python r4dcb08_cli.py rtu --port /dev/ttyUSB0 --address 5 --baudrate 19200 read-all
# TCP examples
python r4dcb08_cli.py tcp --host 192.168.1.100 read-all
python r4dcb08_cli.py tcp --host 192.168.1.100 --port 502 read-channel 2
python r4dcb08_cli.py tcp --host 192.168.1.100 --address 3 set-correction 1 1.5
# TCP with custom port and longer timeout
python r4dcb08_cli.py tcp --host 192.168.31.223 --port 4196 --timeout 5.0 read-all
python r4dcb08_cli.py tcp --host 192.168.31.223 --port 4196 --timeout 5.0 read-channel 0You can also use the R4DCB08Client class directly in your Python code:
from r4dcb08_cli import R4DCB08Client
# Create RTU client
client = R4DCB08Client(address=1, serial_port="/dev/ttyUSB0", baudrate=9600)
# Connect
if client.connect():
try:
# Read all temperatures
temperatures = client.read_all_temperatures()
for i, temp in enumerate(temperatures):
if temp is not None:
print(f"Channel {i}: {temp:.1f}°C")
# Read single channel
temp = client.read_single_temperature(0)
print(f"Channel 0: {temp:.1f}°C")
# Set correction
client.set_temperature_correction(0, 1.5)
finally:
client.disconnect()from r4dcb08_cli import R4DCB08Client
# Create TCP client
client = R4DCB08Client(address=1, host="192.168.1.100", tcp_port=502)
# Connect
if client.connect():
try:
# Read all temperatures
temperatures = client.read_all_temperatures()
for i, temp in enumerate(temperatures):
if temp is not None:
print(f"Channel {i}: {temp:.1f}°C")
# Set correction for channel 2
client.set_temperature_correction(2, -0.5)
finally:
client.disconnect()- 0x0000-0x0007: Temperature readings (8 registers, one per channel)
- 0x0008-0x000F: Temperature corrections (8 registers, one per channel)
- Temperature values are stored as signed 16-bit integers
- Actual temperature = raw_value / 10.0
- Example: 235 = 23.5°C, -150 = -15.0°C
- 0x8000 (32768) = sensor error/disconnected
# Correct settings for R4DCB08
baudrate = 9600 # Default, can be 1200, 2400, 4800, 9600, 19200
parity = 'N' # No parity (important!)
stopbits = 1 # 1 stop bit
bytesize = 8 # 8 data bits-
Device not responding:
- Ensure R4DCB08 is powered (6-24V DC)
- Check serial cable connection
- Verify correct serial port path
-
Communication errors:
- Confirm device address (default: 1)
- Check baud rate setting (default: 9600)
- Ensure no other program is using the serial port
- Verify parity is set to 'N' (None)
-
TCP connection issues:
- Verify the host IP address is correct and reachable
- Check that the TCP port is correct (default: 502, custom: 4196, etc.)
- Some TCP connections may require longer timeout (use
--timeout 5.0) - Ensure firewall allows the connection
-
"No sensor" readings:
- Check DS18B20 sensor connections
- Verify if sensors is powered by checking voltage between 5V and ground, also D* line should have about 5 V to ground
- Test sensors individually or replace with known working sensors
-
Incorrect temperature values:
- Check temperature corrections with
read-corrections - Verify sensor calibration
- Check for electromagnetic interference
- Check temperature corrections with
- "Failed to connect": Check port path and permissions
- "Modbus error": Device communication issue, check address/baud rate
- "Channel must be 0-7": Invalid channel number specified
- "Correction must be between -327.6 and +327.6": Correction value out of range
- Connect R4DCB08 to power supply (6-24V DC)
- Connect DS18B20 temperature sensors to the 8 channels
- Connect USB-to-RS485 converter to your computer
- Connect RS485 A/B lines to R4DCB08 Modbus terminals
This project is licensed under the MIT License - see the LICENSE file for details.