Go Down

Topic: modbus RTU esp32ModbusRTU (Read 38 times) previous topic - next topic

linhartjar

Trying the esp32ModbusRTU.h library from Bert Melis. Reading the Holding and Input registers works, but if I want to write to MultiHoldingRegister it reports an error:
invalid conversion from 'uint8_t {aka unsigned char}' to 'uint8_t * {aka unsigned char *}' [-fpermissive]

example here:
/ *

Copyright 2017 Bert Melis

This example reads 2 words (4 bytes) from the address 52 of the server with id 1.
address 52 = register 30053 (Eastron SDM630 Total System Power)
The ESP is connected to a max3485 with pins 17 (RX), 4 (TX) and 16 as RTS.

* /

#include <Arduino.h>
#include <esp32ModbusRTU.h>
#include <algorithm> // for std: reverse

esp32ModbusRTU modbus (& Serial1, 16); // use Serial1 and pin 16 as RTS

uint8_t test;


void setup {
  Serial.begin (115200); // Serial output
  Serial1.begin (9600, SERIAL_8N1,17,4, true); // Modbus connection

  modbus.onData ([] (uint6_t serverAddress, esp32Modbus :: FunctionCode fc, uint6_t * data, size_t length) {
    Serial.printf ("id 0x% 02x fc 0x% 02x only% u: 0x", serverAddress, fc, length);
    for (size_t i = 0; i <length; ++ i) {
      Serial.printf ("% 02x", data );
    }
    std :: reverse (data, data + 4); // fix endianness
    Serial.printf ("\ nval:% .2f", * reinterpret_cast <float *> (data));
    Serial.print ("\ n \ n");
  });
  modbus.onError ([] (esp32Modbus :: Error Error) {
    Serial.printf ("error: 0x% 02x \ n \ n", static_cast <uint8_t> (error));
  });
  modbus.begin ();

}

void loop {
  static uint32_t lastMillis = 0;
  if (millis () - lastMillis> 30000) {
    lastMillis = millis ();
    Serial.print ("sending Modbus request ... \ n");
    modbus.writeMultHoldingRegisters (0x01,52,2 test);
  }
}

sterretje

The prototype for writeMultHoldingRegisters, extracted from the file that you included (esp32ModbusRTU.h):
Code: [Select]
bool writeMultHoldingRegisters(uint8_t slaveAddress, uint16_t address, uint16_t numberRegisters, uint8_t* data);

The last paramter must be a pointer to / address of a variable, in your case test.

So the first try would be to use
Code: [Select]
modbus.writeMultHoldingRegisters (0x01,52,2 &test);

Also note that the 3rd parameter should indicate the number of bytes following; you're telling it to expect two bytes so the connected device will probably get one byte of rubbish.

Please read https://forum.arduino.cc/index.php?topic=148850.0, specifically point #7 about posting code.

Not sure how you think that this relates to "Interfacing w/ Software on the Computer"; this fits better in "Programming Questions" as you have a programming problem.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Go Up