Hi,
I am working on my first project. Pretty simple project. But I struggled several days with the modbus write function. Reading from the slave is no problem.
I use this library GitHub - 4-20ma/ModbusMaster: Enlighten your Arduino to be a Modbus master
Hardware: ESP8266 and MAX485 board.
Slave device: ID 1, address 145. I know that 0x00 is value off. And writing 0x01 is value on
My Slave device only support read 0x03 FC and write 0x10 FC.
I have tested with qModbus that it works from my PC and to the slave device.
I get the following raw modbus RTU message in the log from qModbus:
Tx -> 01 10 00 91 00 01 02 00 00 BA D1 = value 0 in slave device
Tx -> 01 10 00 91 00 01 02 00 01 7B 11 = value 1 in slave device.
When I break down messages I get the following, if correct:
01 = Slave ID
10 = Function code
00 = Address of the first register Hi bytes
91 = Address of the first register Lo bytes
00 = Number of registers to write Hi bytes
01 = Number of registers to write Lo bytes
02 = Number of bytes
00 = Value Hi
00 = Value Lo (This one is 01 when raw message value is 1)
Please help me, I am totally out of my mind and think I have tried everything.
#include <SoftwareSerial.h>
#include <ModbusMaster.h>
// Define the pins for SoftwareSerial
#define RX_PIN 5 // Max485 Rx connected to D5 (GPIO 5)
#define TX_PIN 4 // Max485 Tx connected to D4 (GPIO 4)
#define RE_DE_PIN 16 // RE/DE control pin for MAX485 (GPIO 16)
// Create a SoftwareSerial object
SoftwareSerial mySerial(RX_PIN, TX_PIN); // RX, TX
// Create a ModbusMaster object
ModbusMaster node;
void preTransmission() {
digitalWrite(RE_DE_PIN, HIGH); // Set RE/DE pin HIGH for transmit mode
}
void postTransmission() {
digitalWrite(RE_DE_PIN, LOW); // Set RE/DE pin LOW for receive mode
}
void setup() {
Serial.begin(9600); // Initialize the Serial monitor
mySerial.begin(9600, SWSERIAL_8N1); // Initialize SoftwareSerial
pinMode(RE_DE_PIN, OUTPUT); // Set RE/DE pin as OUTPUT
digitalWrite(RE_DE_PIN, LOW); // Initialize the pin as LOW
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
node.begin(1, mySerial); // Initialize ModbusMaster with slave ID 1
}
void loop() {
if (Serial.available()) {
int command = Serial.parseInt(); // Read the command from Serial monitor
switch (command) {
case 0:
writeModbusMessageOff();
break;
case 1:
writeModbusMessageOn();
break;
case 2:
readModbusMessage();
break;
default:
Serial.println("Invalid command.");
break;
}
delay(10000); // Add delay here, after the switch block
}
}
void writeModbusMessageOff() {
node.setTransmitBuffer(0, 0000);
node.setTransmitBuffer(1, 0000);
uint8_t result = node.writeMultipleRegisters(0x0091, 1);
if (result == node.ku8MBSuccess) {
Serial.println("Write Sell Off successful!");
} else {
Serial.println("Write Sell Off failed!");
}
}
void writeModbusMessageOn() {
node.setTransmitBuffer(0, 0000);
node.setTransmitBuffer(1, 0001);
uint8_t result = node.writeMultipleRegisters(0x0091, 1);
if (result == node.ku8MBSuccess) {
Serial.println("Write Sell On successful!");
} else {
Serial.println("Write Sell On failed!");
}
}
void readModbusMessage() {
// Read the Modbus message
uint8_t result = node.readHoldingRegisters(0x0091, 1);
if (result == node.ku8MBSuccess) {
uint16_t value = node.getResponseBuffer(0x00);
Serial.print("Solarsell: ");
Serial.print(value, HEX); // Display the value in hexadecimal
Serial.println();
} else {
Serial.println("Read failed!");
}
}
