emelianov / modbus-esp8266

Most complete Modbus library for Arduino. A library that allows your Arduino board to communicate via Modbus protocol, acting as a master, slave or both. Supports network transport (Modbus TCP) and Serial line/RS-485 (Modbus RTU). Supports Modbus TCP Security for ESP8266/ESP32.
Other
534 stars 188 forks source link

Error compiling an multiple registers read/write example over TCP/IP ethernet #270

Closed electron60 closed 1 year ago

electron60 commented 1 year ago

Hi emelianov, I have compiled one of your multiple-register read-write examples, the one you write as a response to Issue #204 of April 23, 2022. This is the code:

`#include

include

include

IPAddress ip (192, 168, 100, 3 ); // L'adreça IP del controlador dependrà de la xarxa local IPAddress remote( 192, 168, 100, 99 );// Adreça IP del dispositiu servidor Modbus ModbusEthernet mb; // Declara l'instància Modbus TCP uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEE }; // Adreça MAC del controlador (la nostra placa)

const uint16_t REG = 0; uint16_t res[10];

void setup() { Ethernet.init(5); // Inicialitza el dispositiu ethernet (Pin 5 = CS) Ethernet.begin(mac, ip); // Inicia la connexió Ethernet delay(1000); // Donar al mòdul Ethernet un segon per inicialitzar

// Comprovar si hi ha el maquinari Ethernet present if ( Ethernet.hardwareStatus() == EthernetNoHardware ) { Serial.println( "No s'ha trobat el mòdul Ethernet. No es pot executar sense maquinari. :(" ); } if ( Ethernet.linkStatus() == LinkOFF ) { Serial.println( "El cable Ethernet no està connectat." ); }

// INICIALITZACIO MODBUS

mb.client(); // Actua com a client TCP Modbus

}

void loop() { if (mb.isConnected(remote)) { // Check if connection to Modbus Slave is established mb.readHreg(remote, REG, &res, 10); // Initiate Read Hregs from Modbus Server // or mb.writeHreg(remote, REG, &res, 10); // Initiate Write to Modbus Server } else { mb.connect(remote); // Try to connect if no connection } mb.task(); // Common local Modbus task delay(100); // Pulling interval } } `

And when I compile with Arduino Ide I have these errors:

`Arduino:1.8.19 (Windows 10), Tarjeta:"DOIT ESP32 DEVKIT V1, 80MHz, 921600, None, Disabled"

C:\Users\miquel\Desktop\sketch_feb03a\sketch_feb03a.ino: In function 'void loop()':

sketch_feb03a:36:38: error: no matching function for call to 'ModbusEthernet::readHreg(IPAddress&, const uint16_t&, uint16_t (*)[10], int)'

 mb.readHreg(remote, REG, &res, 10);  // Initiate Read Hregs from Modbus Server

                                  ^

In file included from C:\Users\miquel\Documents\Arduino\Projectes\libraries\modbus-esp8266\src/ModbusEthernet.h:11,

             from C:\Users\miquel\Desktop\sketch_feb03a\sketch_feb03a.ino:3:

C:\Users\miquel\Documents\Arduino\Projectes\libraries\modbus-esp8266\src/ModbusAPI.h:94:11: note: candidate: 'template uint16_t ModbusAPI::readHreg(TYPEID, uint16_t, uint16_t*, uint16_t, cbTransaction, uint8_t) [with TYPEID = TYPEID; T = ModbusTCPTemplate<EthernetServerWrapper, EthernetClient>]'

uint16_t readHreg(TYPEID id, uint16_t offset, uint16_t* value, uint16_t numregs = 1, cbTransaction cb = nullptr, uint8_t unit = MODBUSIP_UNIT);

       ^~~~~~~~

C:\Users\miquel\Documents\Arduino\Projectes\libraries\modbus-esp8266\src/ModbusAPI.h:94:11: note: template argument deduction/substitution failed:

C:\Users\miquel\Desktop\sketch_feb03a\sketch_feb03a.ino:36:30: note: cannot convert '& res' (type 'uint16_t ()[10]' {aka 'short unsigned int ()[10]'}) to type 'uint16_t' {aka 'short unsigned int'}

 mb.readHreg(remote, REG, &res, 10);  // Initiate Read Hregs from Modbus Server

                          ^~~~

sketch_feb03a:38:39: error: no matching function for call to 'ModbusEthernet::writeHreg(IPAddress&, const uint16_t&, uint16_t (*)[10], int)'

 mb.writeHreg(remote, REG, &res, 10);  // Initiate Write to Modbus Server

                                   ^

In file included from C:\Users\miquel\Documents\Arduino\Projectes\libraries\modbus-esp8266\src/ModbusEthernet.h:11,

             from C:\Users\miquel\Desktop\sketch_feb03a\sketch_feb03a.ino:3:

C:\Users\miquel\Documents\Arduino\Projectes\libraries\modbus-esp8266\src/ModbusAPI.h:88:11: note: candidate: 'template uint16_t ModbusAPI::writeHreg(TYPEID, uint16_t, uint16_t, cbTransaction, uint8_t) [with TYPEID = TYPEID; T = ModbusTCPTemplate<EthernetServerWrapper, EthernetClient>]'

uint16_t writeHreg(TYPEID id, uint16_t offset, uint16_t value, cbTransaction cb = nullptr, uint8_t unit = MODBUSIP_UNIT);

       ^~~~~~~~~

C:\Users\miquel\Documents\Arduino\Projectes\libraries\modbus-esp8266\src/ModbusAPI.h:88:11: note: template argument deduction/substitution failed:

C:\Users\miquel\Desktop\sketch_feb03a\sketch_feb03a.ino:38:31: note: cannot convert '& res' (type 'uint16_t ()[10]' {aka 'short unsigned int ()[10]'}) to type 'uint16_t' {aka 'short unsigned int'}

 mb.writeHreg(remote, REG, &res, 10);  // Initiate Write to Modbus Server

                           ^~~~

In file included from C:\Users\miquel\Documents\Arduino\Projectes\libraries\modbus-esp8266\src/ModbusEthernet.h:11,

             from C:\Users\miquel\Desktop\sketch_feb03a\sketch_feb03a.ino:3:

C:\Users\miquel\Documents\Arduino\Projectes\libraries\modbus-esp8266\src/ModbusAPI.h:90:11: note: candidate: 'template uint16_t ModbusAPI::writeHreg(TYPEID, uint16_t, uint16_t*, uint16_t, cbTransaction, uint8_t) [with TYPEID = TYPEID; T = ModbusTCPTemplate<EthernetServerWrapper, EthernetClient>]'

uint16_t writeHreg(TYPEID id, uint16_t offset, uint16_t* value, uint16_t numregs = 1, cbTransaction cb = nullptr, uint8_t unit = MODBUSIP_UNIT);

       ^~~~~~~~~

C:\Users\miquel\Documents\Arduino\Projectes\libraries\modbus-esp8266\src/ModbusAPI.h:90:11: note: template argument deduction/substitution failed:

C:\Users\miquel\Desktop\sketch_feb03a\sketch_feb03a.ino:38:31: note: cannot convert '& res' (type 'uint16_t ()[10]' {aka 'short unsigned int ()[10]'}) to type 'uint16_t' {aka 'short unsigned int'}

 mb.writeHreg(remote, REG, &res, 10);  // Initiate Write to Modbus Server

                           ^~~~

C:\Users\miquel\Desktop\sketch_feb03a\sketch_feb03a.ino: At global scope:

sketch_feb03a:45:1: error: expected declaration before '}' token

}

^

exit status 1

no matching function for call to 'ModbusEthernet::readHreg(IPAddress&, const uint16_t&, uint16_t (*)[10], int)'`

I can connect to a PLC and read or write a Holding Register or a Coil, but not multiple. Why? What am I doing wrong? Thanks

electron60 commented 1 year ago

Sorry, I have solved it. Missing (uint16_t*) before REG