How to adress/change values of data stored ( Mb.Data) using MgsModbus (TCP IP) ?

Hello, I am from Argentina. I am working on my final project for an engineer carrer. I am trying to make the DUE a device that can communicate through RS485-Ethernet-Lora Wifi. For the RS485 and Ethernet I use USB RS485 and mini ethernet W5100. I am trying with modbus RTU via RS485 and MODBUS TCP/IP. With Lora Wifi i use the sx1278 433MHz. I didn´t have any problem.

I could run and test communications without any problems, but i dont understand how to give values to holding registers,coils,discret inputs and input registers when i am trying to communicate with modbus tcp ip using mgsmodbus.

It´s not a library, you just need to include the .h and .cpp

I have read a lot of things here in the forum but i couldn´t find what i am looking for and in the page where you can find mgsmodbus, the creator says : " The user must assign the correct Modbus address to the data". I just want to know how is that adressing method.

http://myarduinoprojects.com/home.html

My DUE is just a slave, and the problem is that if the master write a 1 to the coil at adress 3, it will be for hold reg, disc input and inp reg.

Here is the .ino slave example

#include <SPI.h>
#include <Ethernet.h>
#include “MgsModbus.h”

MgsModbus Mb;
int inByte = 0; // incoming serial byte

// Ethernet settings (depending on MAC and Local network)
byte mac = {0x90, 0xA2, 0xDA, 0x0E, 0x94, 0xB5 };
IPAddress ip(192, 168, 0, 10);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);

void setup()
{
// serial setup
Serial.begin(9600);
Serial.println(“Serial interface started”);

// initialize the ethernet device
Ethernet.begin(mac, ip, gateway, subnet); // start etehrnet interface
Serial.println(“Ethernet interface started”);

// print your local IP address:
Serial.print(“My IP address: “);
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(”.”);
}
Serial.println();

// Fill MbData
// Mb.SetBit(0,false);
Mb.MbData[0] = 0;
Mb.MbData[1] = 0;
Mb.MbData[2] = 0;
Mb.MbData[3] = 0;
Mb.MbData[4] = 0;
Mb.MbData[5] = 0;
Mb.MbData[6] = 0;
Mb.MbData[7] = 0;
Mb.MbData[8] = 0;
Mb.MbData[9] = 0;
Mb.MbData[10] = 0;
Mb.MbData[11] = 0;

// print MbData
for (int i=0;i<12;i++) {
Serial.print("address: "); Serial.print(i); Serial.print("Data: "); Serial.println(Mb.MbData*);*

  • }*
  • // print menu*
  • Serial.println(“0 - print the first 12 words of the MbData space”);*
  • Serial.println(“1 - fill MbData with 0x0000 hex”);*
  • Serial.println(“2 - fill MbData with 0xFFFF hex”);*
  • Serial.println(“3 - fill MbData with 0x5555 hex”);*
  • Serial.println(“4 - fill MbData with 0xAAAA hex”);*
    }
    void loop()
    {
  • if (Serial.available() > 0) {*
  • // get incoming byte:*
  • inByte = Serial.read();*
  • if (inByte == ‘0’) { // print MbData*
  • for (int i=0;i<12;i++) {*
    _ Serial.print("address: "); Serial.print(i); Serial.print("Data: "); Serial.println(Mb.MbData*);_
    _
    }_
    _
    } _
    _ if (inByte == ‘1’) {for (int i=0;i<12;i++) {Mb.MbData = 0x0000;}}
    if (inByte == ‘2’) {for (int i=0;i<12;i++) {Mb.MbData = 0xFFFF;}}
    if (inByte == ‘3’) {for (int i=0;i<12;i++) {Mb.MbData = 0x5555;}}
    if (inByte == ‘4’) {for (int i=0;i<12;i++) {Mb.MbData = 0xAAAA;}}
    }

    // Mb.MbmRun();

    Mb.MbsRun();

    }

    I have read the .cpp and i don´t understand how to use the MbData to for example just change the value of a hold register at adress 5 (40001-49999)
    Thanks , i hope u can help me !
    MgsModbus.cpp (10.9 KB)
    MgsModbus.h (3.42 KB)_

    MgsModbus_test_SlaveSinLostConection.ino (2.43 KB)*