Logopaede
Hello,

I'm doing my first steps with Modbus-RTU. A Raspberry Pi4 with OpenPLC is connected with an SDM120M energy counter over an USB-to-RS485 adapter.

I can read two Input Registers with node-red at the moment (voltage and current), so OpenPLC works generally. Great!

But to my surprise I start to get connection timeouts to my modbus slave once I configure more than a size of around 12 Input registers in the Edit slave device window.

OpenPLC Runtime Logs
Modbus Read Input Registers failed on MB device SDM120M: Connection timed out
Modbus Read Input Registers failed on MB device SDM120M: Connection timed out
Modbus Read Input Registers failed on MB device SDM120M: Connection timed out
[...]


That is a problem, because the higher registers are unreachable then. For example, these are the addresses of the (16-bit) input registers:

30001 Voltage
30007 Current
30013 Active Power
30019 Apparent Power
30025 Reactive Power
[and so on]

To reach all parameters of the SDM120M I would need a size of more than 300 input registers in OpenPLC!

I don't understand at the moment, what limits the size of usable registers to just around 12.

The SDM120M has 2400 baud (8N1) per default. The cable-length is 2 m, 2 wires, shielded (but not connected to a common ground).

The program running on the OpenPLC at the same time is fetching temperatures from two ESP8266 slaves per Modbus-TCP and some GPIOs on the RPi4.
The task cycle is T#250ms for the temperatures and T#50ms for the GPIOs.

How often are the input registers of the energy counter read then? Each 50 ms or 250 ms?
Is this limiting at 2400 baud and can cause connection timeouts?



Thank you
Reply 0 0
NWT.Stuff
Interesting Application. I need to have a go at Modbus RTU someday 🙂

2400 baud 2400bps sounds really really slow by todays standards. 

What is the application ? Domestic Energy Monitoring ?

What is the maximum baud rate on this Energy Meter ?

And also why did you select this product ? 

Regards, Kevin
Reply 0 0
Logopaede
Hi,

2400 baud is the default (9600 the max), but it should be enough to read a handful registers requested by OpenPLC in 500 ms polling period in my opinion (or not?). It can be set to other baudrates by writing special registers, but I don't want to brick the device before I have better information about the issue.

OpenPLC is a proof-of-concept for me at the moment. When basic features work to my liking, I make plans for real applications in the future. The SDM120M was cheap, well documented and I had no access to any other Modbus-RTU slave, that's why I selected it to be my dummy.
Reply 0 0
NWT.Stuff
Ok understood 🙂

I agree messing with baud rate is unlikely to be the solution.

I don’t have any experience in this field but if I were to attempt RS485 I wouldn’t go through a USB adaptor; I would go with a converter that goes directly to RPI GPIO.

Just thinking aloud not a recommendation as not done any of this before 🙂
Reply 0 0
thiagoralves

My first bet would be to increase the polling period to something ridiculous like 5 seconds or so. This should be more than enough to read as many registers as you want. If that works then you can try adjusting the polling accordingly. 


If the polling is still too slow for your needs, another option would be to have a intermediary process capturing the required registers from your device and feeding OpenPLC. The PSM driver seems perfect for this task. Search this forum for PSM to learn more about it.

Reply 0 0
Logopaede
Ok, the polling period is not the limiting factor, I tried 5 seconds but only up to 10 - 12 registers size the polling is stable, above time outs occure and even voltage and current are not refreshed any longer.

Before I read your hints about PSM - thank you! - I have just one question for basic understanding:

Does "polling the modbus slave" mean, that the full size of registers (for example 12 registers) is requested from the slave and stored and mapped to OpenPLC variables (for example %IW102, 103, etc.) inside OpenPLC each polling period?

Just a short yes/no from anybody who knows would be welcome.
Reply 0 0
thiagoralves
Yes. The polling cycle polls all registers from all slave modbus devices, all at once. It uses modbus read multiple registers command to do that. It is possible that your device is having issues with reading multiple registers on the same command. There is no way to easily change that without changing OpenPLC code
Reply 0 0
Logopaede
Thanks for the clarification!
Today I removed my OpenPLC_v3 folder and did a fresh install (git clone ...).
Lost all my settings, but I'm having a backup of the old folder. Hopefully I can recover everything if needed.

On this tabula rasa I configured just one single modbus-RTU device, the SDM120M.
To exclude further influence I stopped the node-red server, that normally polls my OpenPLC variables. The issue remains.



I guess this fits to your idea, that the SDM120M could have a problem with the "modbus read multiple registers command". On the other hand, it is not possible to read the parameter-registers selectively with OpenPLC, but a solution could be your new OpenPLC Python SubModule (PSM). I see this new option in the Hardware menue. But to select it, I have to deselect the Raspberry Pi Hardware support. I have to read more now, what is the idea behind PSM. Having no Python skills, it will be probably not a solution for me, but for programmers it could be a mega progress. Kind regards!
Reply 0 0
NWT.Stuff
@thiagoralves can explain better than me but I have been using Python on RPI for many years, Open PLC for less.  

In my case previous to PSM I have a Python Module running and I access Digital IO via Modbus TCP/IP.  So the Python is running in parallel with OpenPLC and communicating over Modbus. With PSM I can directly read and write to PLC Registers with in built code in PLC Web Server.  This also has the added flexibility of configuring the RPI GPIO for each Application (PLC Program).  It sits better and integrates seamlessly.

I would say even if your don't have confidence in your Python skills you will surely have the transferrable coding skills to have a go.  It could be as simple as working through a Python Project that someone has posted on line for Modbus RTU and pasting it onto the PSM window and working your way through it 🙂

Regards, Kevin 
Reply 0 0
Logopaede
Yes, the new flexibility is a great plus. Good explanation!
On the other hand I'm losing the already working abstraction layer:
OpenPLC's Modbus-TCP for the esp8266 (nodemcu1+2), the Pi's local GPIOs and the (partial) Modbus-RTU support of my energy counter.

If I understand it correctly, for PSM I have to find / to code new Python3 "drivers":
RPi GPIO
Modbus-TCP
Modbus-RTU
Reply 0 0
NWT.Stuff
Quote:
If I understand it correctly, for PSM I have to find / to code new Python3 "drivers":


RPI GPIO
is preinstalled on PI-OS 

For PSM I have posted an example of usage towards the bottom of this topic
https://openplc.discussion.community/post/megaioind-industrial-building-automation-for-raspberrypi-9832564?pid=1325494776

Modbus-TCP
I have tried 2 modbus TCP clients. the easiest and the best I have found is :-
https://pypi.org/project/pyModbusTCP/

PSM has actually eliminated the need for this in one of my applications with digital IO.  I need to have a think about if I will still use it in the future.

I will dig out a suitable Python 3 example I use and post it later.

Modbus-RTU
Not tried that yet 🙂

Reply 0 0
Logopaede
Good news from me too!
The limiting factor is my USB-to-RS485 converter of this type:



With another converter of another manufacturer I can read multiple registers without problems. Tested it with a commandline tool on a laptop. For example 24 registers read from the SDM120M:

$ mbpoll -a 1 -b 2400 -P none -t 3 -r 1 -c 24 /dev/ttyUSB0
mbpoll 1.0-0 - FieldTalk(tm) Modbus(R) Master Simulator
Copyright © 2015-2019 Pascal JEAN, https://github.com/epsilonrt/mbpoll
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions; type 'mbpoll -w' for details.

Protocol configuration: Modbus RTU
Slave configuration...: address = [1]
start reference = 1, count = 24
Communication.........: /dev/ttyUSB0, 2400-8N1
t/o 1.00 s, poll rate 1000 ms
Data type.............: 16-bit register, input register table

-- Polling slave 1... Ctrl-C to stop)
[1]: 17259
[2]: 58982 (-6554)
[3]: 0
[4]: 0
[5]: 0
[6]: 0
[7]: 0
[8]: 0
[9]: 0
[10]: 0
[11]: 0
[12]: 0
[13]: 0
[14]: 0
[15]: 0
[16]: 0
[17]: 0
[18]: 0
[19]: 0
[20]: 0
[21]: 0
[22]: 0
[23]: 0
[24]: 0


The same command with my other RS485 converter always ends with connection timeout (only with less than ~10-12 registers it works with mbpoll).

I'm very confident that the new converter will work now in the scenario of post #1.
(have to recover it now). I will report.

Anyway I'm now very interested in the possibilities of PSM. I'm thinking about migrating to it when the classic setup works. Great, that you already pasted helpful examples, Kevin. Thank you very much!
Reply 1 0
thiagoralves
Just to clarify on the PSM questions:
Logopaede wrote:

If I understand it correctly, for PSM I have to find / to code new Python3 "drivers":
RPi GPIO
Modbus-TCP
Modbus-RTU


RPi GPIO -> PSM replaces the Raspberry Pi driver, so, yes, you will need to write Python code to interface the Pi's GPIOs. However, this should be extremely straightforward given the flexibility and ease of use of Python. NWT has a working code on his thread post

Modbus-TCP -> You don't need to replace that if you use PSM. The Slave devices module continue working normally independently of the driver you choose on the hardware tab

Modbus-RTU -> Same as Modbus-TCP

So, to summarize, if you use PSM, you would only have to replace the RPi GPIO commands, and then work on whatever else you need for your hardware to run as you want.
Reply 1 0
Logopaede
Good that you explained it, Thiago. Indeed I had a misunderstanding at this point.
Reply 0 0
NWT.Stuff
Quote:
Good news from me too!
The limiting factor is my USB-to-RS485 converter of this type:

Glad you have found the problem and can proceed as you had originally planned 🙂 It's always easier than starting again.

I want to look at Energy Meters at some stage too.  The "Smart Meters" installed in UK are very crap and not very smart !!
Reply 0 0
Reply