modbus-esp8266 Libr...
 
Notifications
Clear all

modbus-esp8266 Library Problems

2 Posts
2 Users
0 Reactions
191 Views
(@hometownjamie)
Member
Joined: 3 weeks ago
Posts: 4
Topic starter  

I am trying to get a modbus server using the modbus-esp8266 library running on an esp32 and need help.  I am using callback functions and have both set and get registers (Function type 4 calls) working as expected.  I am applying the same logic to set and get coils (Function type 1 calls) and have no success at all. The get coil status always returns 0 to the client, regardless of what the callback function returns.  There's clearly something about this I don't understand.  Thanks to anyone who can set me straight.

I have made a small test sketch to illustrate my problem:

#include <WiFi.h>
#include <ModbusIP_ESP8266.h>
#include "secrets.h"

IPAddress local_ip(192, 168, 0, 40);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);

ModbusIP mbs; //ModbusIP server object

#define NOCOILS 500
#define NOREGS 620

uint16_t regs[NOREGS];

uint16_t onHregRead(TRegister* reg, uint16_t val) {
Serial.printf("Requesting value for address %d\n", reg->address.address);
return(regs[reg->address.address+1]);
}

uint16_t onHregWrite(TRegister* reg, uint16_t val) {
Serial.printf("Requesting to set value of %d for register %d\n", val, reg->address.address);
regs[reg->address.address+1] progam = val;
return(1);
}

uint16_t onCoilRead(TRegister* reg, uint16_t val) {
Serial.printf("Requesting value for coil %d\n", reg->address.address);
return(1); // return 1 to indicate coil is on
}

void setup() {
Serial.begin(115200);

// connect to WIFI
WiFi.config(local_ip, gateway, subnet);
Serial.println("Connecting to WIFI");
WiFi.begin(SSID, PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
}
Serial.println("WiFi Connected");

mbs.server();

mbs.addHreg(0, 0, NOREGS); // parameters: offset, initial_value, num_regs
mbs.onGetHreg(0, onHregRead, NOREGS); // parameters: offset, callback, # of entries
mbs.onSetHreg(0, onHregWrite, NOREGS); // parameters: offset, callback, # of entries

mbs.addCoil(0, 0, NOCOILS); // offset, initial value, quantity to add
mbs.onGetCoil(0, onCoilRead, NOCOILS);

Serial.println("Running...");
}

void loop(void) {
mbs.task();
}

This topic was modified 2 weeks ago by hometownjamie

   
Quote
Topic Tags
TFMcCarthy
(@tfmccarthy)
Member
Joined: 1 year ago
Posts: 435
 

Any progress on this?

I've been reluctant to comment on this because of my lack of knowledge of the Modbus protocol. I've done a bit of research on it (with Mazurel/Modbus: Modbus library for modern C++, emelianov/modbus-esp8266, and Simply Modbus Software FAQ) and I think I have enough information for intelligent comment.

Are you basing this on a DBWS article? If so, which one?

You give a sketch of a Modbus server but not the client, so that's only half the picture. You should post the code of complete example you're using. otherwise, I can't replicate the results. Speaking of which, you should also post the results of the program output. Again, for comparison.

As a guideline, via a quote:

...frothy eloquence neither convinces nor satisfies me.
I am from Missouri. You have got to show me.
- U.S. Congressman Willard Duncan Vandiver

So, for example,

I am using callback functions and have both set and get registers (Function type 4 calls) working as expected.

You should show all the code that demonstrates this and the output to prove it.

My suspicion is this is a communication, not a protocol problem. Still, my mental picture of the protocol isn't perfectly clear, and I need to do more experimenting with it.

(Sorry if this sounds pompous. I don't mean it to.)


This post was modified 5 days ago 2 times by TFMcCarthy

The one who has the most fun, wins!


   
ReplyQuote