Hi,
I'm doing some test with the ArduinoModbus Library and a MAX485 module. In my computer (macOS Big Sur) I installed minimalmodbus. My objective is to get some data from a DHT22 on Arduino Uno R3 (server) and send the data to my computer (client) using this USB-RS4845 adapter.
I wrote this code for the Arduino:
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
#include <ArduinoModbus.h>
#include <DHT.h>
#define DHTPIN 13 // Connected to Pin Digital 13 on Arduino
#define DHTTYPE DHT22 // DHT22 temp & hum sensor (AM2302)
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// dht begin
dht.begin();
// start the Modbus RTU server, with (slave) id 42
if (!ModbusRTUServer.begin(42, 9600)) {
// do something if there is an error
while (1);
}
// configure holding registers at address 0x00
ModbusRTUServer.configureHoldingRegisters(0x00, 2);
}
void loop() {
delay(2000); // Wait a few seconds between measurements.
float h = dht.readHumidity(); // Read humidity in %
float t = dht.readTemperature(); // Read temperature as Celsius
if (isnan(h) || isnan(t)) {
// Do something to manage the error
return;
}
ModbusRTUServer.holdingRegisterWrite(0, h);
ModbusRTUServer.holdingRegisterWrite(1, t);
// poll for Modbus RTU requests
ModbusRTUServer.poll();
}
and this is the Python3 code running in my computer:
#!/usr/bin/env python3
import minimalmodbus
import serial
instrument = minimalmodbus.Instrument('/dev/tty.usbserial-1420', 42) # port name, slave address (in decimal)
instrument.serial.port
instrument.serial.baudrate = 9600
instrument.serial.bytesize = 8
instrument.serial.parity = serial.PARITY_NONE
instrument.serial.stopbits = 1
instrument.serial.timeout = 0.25 # seconds
# print (instrument)
# n = instrument.read_bit(1,1)
# instrument.write_bit(0,1)
# print(n)
## Read temperature (PV = ProcessValue) ##
temperature = instrument.read_register(0, 2) # Registernumber, number of decimals
print(temperature)
humidity = instrument.read_register(1, 2) # Registernumber, number of decimals
print(humidity)
## Change temperature setpoint (SP) ##
# NEW_TEMPERATURE = 95
# instrument.write_register(24, NEW_TEMPERATURE, 2) # Registernumber, value, number of decimals for storagehumidity
I'm getting this CRC error:
Traceback (most recent call last): File "/Users/jjmuriel/Documents/python_work/RS485-MODBUS/read_register.py", line 18, in <module> temperature = instrument.read_register(0, 2) # Registernumber, number of decimals File "/usr/local/lib/python3.9/site-packages/minimalmodbus.py", line 441, in read_register return self._generic_command( File "/usr/local/lib/python3.9/site-packages/minimalmodbus.py", line 1170, in _generic_command payload_from_slave = self._perform_command(functioncode, payload_to_slave) File "/usr/local/lib/python3.9/site-packages/minimalmodbus.py", line 1243, in _perform_command payload_from_slave = _extract_payload( File "/usr/local/lib/python3.9/site-packages/minimalmodbus.py", line 1756, in _extract_payload raise InvalidResponseError(text) minimalmodbus.InvalidResponseError: Checksum error in rtu mode: 'þï' instead of 'ð&' . The response is: 'ïþïþïþï' (plain response: 'ïþïþïþï') [Finished in 0.1s with exit code 1] [cmd: ['python3', '-u', '/Users/jjmuriel/Documents/python_work/RS485-MODBUS/read_register.py']] [dir: /Users/jjmuriel/Documents/python_work/RS485-MODBUS] [path: /opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Wireshark.app/Contents/MacOS]
This is the wire schema used with the MAX854:
(MAX485 A-A Computer)
(MAX485 B-B Computer)
I can't understand the addressing method that implements the ArduinoModbus library and why I'm getting this error. Please, any help will be welcome.