martin010

Not really know if this is an issue or something I've done wrong, but I am trying a ESP8266 (a Wemos Mini) to run as a modbus slave. The ESP runs with the following library;

#include <ModbusSlaveTCP.h>

and when the module is polled by openPLC, it does respond to:
slave.cbVector[CB_READ_COILS] = readDigitalIn;
...
void readDigitalIn(uint8_t fc, uint16_t address, uint16_t length) {
  // read digital input
  for (int i = 0; i < length; i++) {
     slave.writeCoilToBuffer(i, digitalRead(address + i));
  }
}

ut the OpenPLC outputs an error as below:

Connected to MB device
Modbus Write Coils failed on MB device : Connection timed out
Modbus Read Input Registers failed on MB device : Bad file descriptor
Modbus Write Holding Registers failed on MB device : Bad file descriptor
Device v is disconnected. Attempting to reconnect...
Connected to MB device

Whenever I use ModbusTcpClient in python with the following simple program:

client = ModbusTcpClient('xxx.xxx.xxx.xxx')
result = client.read_coils(1,0)
print(result.bits[0])
client.close()

it returns no error, so assuming the ESP modbus does respond ok?

Any tips?

Quote 0 0
thiagoralves
Can you post a screenshot of the configuration you did on the slave devices page?
Quote 0 0
martin010
I used the standard ESP8266 from the device settings. Can’t change any settings on that one. 
Quote 0 0
martin010
I have tried your Sonoff code, and that seems to work ok. I think it is related to time-outs. 
Since my sketch involves much more then only a modbus, it needs to do a lot more work.
What is the time-out on the modbus polling? would there be anyway it could be implemented in an existing sketch that does other tasks as well?
Quote 0 0
thiagoralves
Why don't you use the ESP8266 firmware provided in the OpenPLC website? If you're writing your own firmware you probably won't be able to use the ESP8266 settings from the Slave Devices page, unless you implement exactly the same configuration settings from the OpenPLC firmware in the website (again, why not just use the provided firmware then). I bet the problem you're having is due to configurations on your custom firmware (like number of registers, slave id, etc...)
Quote 0 0
martin010
I have a ESP that works like a sensor / themorstat. I would like to add the modbus protocol use some inputs on the ESP in combination with Open PLC. 
I can therefor not use the ESP dedicated for openPLC. I was hoping it could be combined.  
Quote 0 0
thiagoralves
It probably can be "combined" if you start from the OpenPLC firmware and then add the stuff you need. You just need to be wise not to mess up with the configuration of registers, otherwise the communication will not work.
Quote 0 0
martin010
I understand that, but I think timing could be an issue? ie. when the ESP does other tasks, if can briefly not communicate on the modbus, so hence my first question, is there a certain time out, or a polling/timeout interval that can be set/changed? I noticed that openPLC query's multiple time per second to the ESP. Maybe if there is some info about this I could see if it could be combined?
Quote 0 0
thiagoralves
The timeout is probably defined somewhere on the libmodbus library (the library OpenPLC uses to communicate with Modbus slave devices. See here: https://libmodbus.org/docs/v3.0.6/modbus_set_response_timeout.html ). I'm more inclined to believe that the issue is more related to configuration than timeout. The ESP chip is pretty fast, which means that you would have to do a lot of computation in parallel for the modbus message to timeout.

The queries that OpenPLC do are based on the scan cycle of the program it is running. Every PLC program has a cyclic task that defines the interval for which the scan cycle will run. You can check this on PLCOpen Editor, left sidebar, expand Config0, double-click on Res0. Under "Tasks" you can see the configuration of tasks for your program. If you change the interval for the scan cycle, it will change the pooling interval for Modbus Slave Devices as well.
Quote 0 0
martin010
ok, thanks for the pointers, will have a look at things. 
Quote 0 0
martin010
I Did load your ESP firmware on a Wemos Mini Pro for some testing, but still got some connection errors
Here is the Serial log from the Wemos, and below it the OpenPLC Log.
This is with running the blank program, so nothing is supposed to happen?

--- Wemos Log ---
WiFi connected
Server started
My IP: 192.168.0.197
new client!
new client!
new client!
new client!

--- OpenPLC Log
openPLC Runtime starting...
Interactive Server: Listening on port 43628
Device Wemos D1 Pro is disconnected. Attempting to reconnect...
Connected to MB device Wemos D1 Pro
Issued start_modbus() command to start on port: 502
Modbus Server: Listening on port 502
Modbus Server: waiting for new client...
Issued start_dnp3() command to start on port: 20000
DNP3 ID manager: Starting thread (0)
DNP3 ID DNP3_Server: Listening on: 0.0.0.0:20000
Modbus Read Discrete Input Registers failed on MB device Wemos D1 Pro: Connection timed out
Modbus Write Coils failed on MB device Wemos D1 Pro: Bad file descriptor
Modbus Read Input Registers failed on MB device Wemos D1 Pro: Bad file descriptor
Modbus Write Holding Registers failed on MB device Wemos D1 Pro: Bad file descriptor
Device Wemos D1 Pro is disconnected. Attempting to reconnect...
Connected to MB device Wemos D1 Pro
Modbus Write Coils failed on MB device Wemos D1 Pro: Connection timed out
Modbus Read Input Registers failed on MB device Wemos D1 Pro: Bad file descriptor
Modbus Write Holding Registers failed on MB device Wemos D1 Pro: Bad file descriptor
Device Wemos D1 Pro is disconnected. Attempting to reconnect...
Connected to MB device Wemos D1 Pro
Modbus Write Holding Registers failed on MB device Wemos D1 Pro: Connection timed out
Device Wemos D1 Pro is disconnected. Attempting to reconnect...
Connected to MB device Wemos D1 Pro
Modbus Read Discrete Input Registers failed on MB device Wemos D1 Pro: Connection timed out
Modbus Write Coils failed on MB device Wemos D1 Pro: Bad file descriptor
Modbus Read Input Registers failed on MB device Wemos D1 Pro: Bad file descriptor
Modbus Write Holding Registers failed on MB device Wemos D1 Pro: Bad file descriptor
Device Wemos D1 Pro is disconnected. Attempting to reconnect...
Connected to MB device Wemos D1 Pro
Quote 0 0
thiagoralves
Hmm..this is weird. I will check the firmware to see what is causing these disconnections. How often do you get these errors (every hour, minute or second)?
Quote 0 0
martin010
This was just in a time span of a few seconds. I stopped the server straight away. When I run it again, it takes about 1 to 2 min. and then it starts to dis/re-connect at intervals.
Quote 0 0
martin010
Think I might have found the issue. The raspberry Pi got two IP Addresses. One was configured as static, but for some reason dhcpd assigned a DHCP address as well. Got that fixed now, and it seems to run fine now for at least the last half an hour. I will leave it for a while to see if the messages return. To be clear this was with your ESP8266 firmware. nothing modified. 
Quote 0 0
thiagoralves
Good to hear that. Keep me updated if you see disconnection errors again
Quote 0 0