-
Notifications
You must be signed in to change notification settings - Fork 368
Open
Description
ModbusMaster version
4-20ma/ModbusMaster@^2.0.1
Arduino IDE version
Visual Studio Code - PlatformIO
Arduino Hardware
ESP32 on Arduino codebase via PlatformIO. With a MAX13487e.
Scenario:
Reading a three phase network analyzer, slave device 1, baud 9600.
Steps to Reproduce:
A raw serial message read/wrtie does produce the right message and response from the device. Somehow when using the ModbusMaster library I keep getting an Error 227.
Response with code below: 01 03 01 66 00 02 25 E8 01 03 04 3C A6 43 6A A7 5F;
void sendModbusRequest() {
uint8_t request[] = {0x01, 0x03, 0x01, 0x66, 0x00, 0x02}; // Device 1, Function 3
uint16_t crc = calculateCRC(request, sizeof(request));
// Append CRC to request
uint8_t crcLow = crc & 0xFF;
uint8_t crcHigh = (crc >> 8) & 0xFF;
Serial2.write(request, sizeof(request));
Serial2.write(crcLow);
Serial2.write(crcHigh);
}
//Serial2.begin(9600, SERIAL_8N1, BOARD_485_RX, BOARD_485_TX);
void readModbusResponse() {
int availableBytes = Serial2.available();
if (availableBytes > 0) {
Serial.print("Response: ");
while (availableBytes--) {
int byteReceived = Serial2.read();
if (byteReceived == -1) break; // Just in case
// Print each byte in HEX format
if (byteReceived < 16) {
Serial.print("0"); // Print leading zero for single digit hex values
}
Serial.print(byteReceived, HEX);
Serial.print(" "); // Add space between bytes for readability
}
Serial.println(); // Newline after the complete message
} else {
Serial.println("No response");
}
}
Current not-working code for ModbusMaster:
void connectmodbus(){
Serial2.begin(9600, SERIAL_8N1, BOARD_485_RX, BOARD_485_TX);
node.begin(1, Serial2);
pinMode(GPIO_PUSE, OUTPUT);
tick.attach_ms(1000, []() {
digitalWrite(GPIO_PUSE, 1 - digitalRead(GPIO_PUSE));
});
pinMode(BOARD_POWER_ON, OUTPUT);
digitalWrite(BOARD_POWER_ON, HIGH);
pinMode(BOARD_485_EN, OUTPUT);
digitalWrite(BOARD_485_EN, LOW);
}
void loop(){
static uint32_t i;
uint8_t j, result;
i++;
// set word 0 of TX buffer to least-significant word of counter (bits 15..0)
//node.setTransmitBuffer(0, lowWord(i));
// set word 1 of TX buffer to most-significant word of counter (bits 31..16)
//node.setTransmitBuffer(1, highWord(i));
node.clearResponseBuffer();
result = node.readHoldingRegisters(0x0166,2);
//readModbusResponse();
if (result == node.ku8MBSuccess) {
Serial.println("Request successful");
// If successful, read the data
for (uint8_t i = 0; i < 2; i++) { // Assuming 2 registers were requested
uint16_t data = node.getResponseBuffer(i);
Serial.print("Register ");
Serial.print(i);
Serial.print(": ");
Serial.println(data, HEX);
}
} else {
Serial.print("Request failed with error code: ");
Serial.println(result);
}
//sendModbusRequest();
delay(1000); // Wait a bit for the response
}
Expected Result:
Response value from device >> 3C A6 43 6A
Actual Result:
Error 227.
Metadata
Metadata
Assignees
Labels
No labels