blewis9991
I am getting the error for Start PLC on the Dashboard
Error: Cannot import name ModbusTcpClient from pymodbus.cline (/home/admin/OpenPLC_v3/.venv/lib/python3.11/site-packages/pymodbus/client/__init__.py)
 
and then failures to connect.
 
Is there an install into a virtual enviroment that must be done? I have limited experience in Linux so I don't know how this is corrected. I have a Pi 5, and bookworm which is all uptodate, and python 3.?
Reply 0 0
blewis9991
I think I resolved this by installing pymod into the virtual environment, but can't tell as the code in the hardware layer is now missing after the session closed. How is the code saved or is it manually entered each time a session is opened in the webserver? Will make a new post on this.
Reply 0 0
fg71
To use ModbusTcpClient you do from pymodbus.client import ModbusTcpClient
Reply 1 0
blewis9991
I
To use ModbusTcpClient you do from pymodbus.client import ModbusTcpClient
Is there a difference between pymodbus.client and the statements in the Hardware Layer code example.? In that example they say
to use psm.set and psm.get; which should I use. I am just trying to read two registers on a modbus device and want to get something working I have no speed or efficiency goals. Thank you
Reply 0 0
blewis9991
I think there is a miss match in installations. I am getting errors on function calls. I removed the {.sync} in the ModbusTcpClient in one of the py modules and it compiles, but now yields these errors on running, mismatched function calls. I can go through and manually edit the functions, but they will be overwritten when regenerated I am guessing. I am using a Pi 5
 
Is there an update I need to do? Thanks
 
 
OpenPLC Runtime starting...
Interactive Server: Listening on port 43628
PSM: Starting PSM...
Exception in thread Thread-1 (run_server):
Traceback (most recent call last):
File "/usr/lib/python3.11/threading.py", line 1038, in _bootstrap_inner
self.run()
File "/usr/lib/python3.11/threading.py", line 975, in run
self._target(*self._args, **self._kwargs)
File "/home/rwl1/OpenPLC_v3/webserver/core/psm/psm.py", line 128, in run_server
store = ModbusSlaveContext(di=d_inputs, co=d_outputs, ir=a_inputs, hr=a_outputs, zero_mode=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: ModbusSlaveContext.__init__() got an unexpected keyword argument 'zero_mode'
Issued start_modbus() command to start on port: 502
Server: Listening on port 502
Server: waiting for new client...
Issued stop_dnp3() command
Issued start_enip() command to start on port: 44818
Server: Listening on port 44818
Server: waiting for new client...
Issued stop_pstorage() command
PSM: Connection to psm failed
PSM: Error connecting to psm!
PSM: PSM is disabled
Device TUF_BT_temp_1 is disconnected. Attempting to reconnect...
Persistent Storage is empty
Connected to MB device TUF_BT_temp_1
Device TUF_BT_temp_2 is disconnected. Attempting to reconnect...
Connected to MB device TUF_BT_temp_2
Connection to (192.168.1.7, 502) failed: timed out
Modbus Connected
Connection to (192.168.1.7, 502) failed: timed out
Modbus Error: ModbusClientMixin.read_holding_registers() takes 2 positional arguments but 3 positional arguments (and 1 keyword-only argument) were given
Traceback (most recent call last):
File "/home/rwl1/OpenPLC_v3/webserver/./core/psm/main.py", line 67, in <module>
time.sleep(0.1) # Adjust cycle time as needed
^^^^
NameError: name 'time' is not defined
PSM: Error while starting Python interpreter
Reply 0 0
blewis9991
after a lot of searching, there is a mismatch :
ImportError: cannot import name 'ModbusTcpClient' from 'pymodbus.client'
 

Solution:

Update imports in /home/rwl1/OpenPLC_v3/webserver/core/psm/ main.py as follows:
  1. Replace:
     
    python
    from pymodbus.client import ModbusTcpClient
  2. With:
     
    python
    from pymodbus.client.sync import ModbusTcpClient
     
    This is NOT A VALID SOLUTION, when I do this I get further compile errors. Removing the .sync allowed it to compile but then the system fails on incorrect function calls.
     
    Is there an install that updates all the modules to work correctly?  
Reply 0 0
thiagoralves
PSM uses pymodbus under the hood. It expects a specific pymodvus version, since pymodbus API has changed over the last versions. So if you just install the latest pymodbus on the OpenPLC venv, it will crash PSM. If you need to use pymodbus on your PSM module, you’ll have to use the version that comes with it.
Reply 0 0
blewis9991
I have a very limited knowledge of linux and I am not trying to do anything but get two points to read on modbus. I have installed in virtual environments because I was directed to by some system install prompt, that failed, Then I read on this forum to install system wide so I installed or tried to system wide, that failed, I installed python 3.x because I read that had to be the one, that failed, I re-installed 2.5.3 that fails "openplc failiing : OpenPLC Runtime starting... Interactive Server: Listening on port 43628 PSM: Starting PSM... sh: 1: ../.venv/bin/python3: not found PSM: Error while starting Python interpreter". I followed the installs several times and I can't get anything to work with PSM. Can you direct me to or tell me what sequences or files to load to install please? Do I need to wipe the system and start again? I have a pi 5, and bookworm. I have spent two days installing and uninstalling software and don't know what is the correct method. I had expected that the install I downloaded would just work.
Reply 0 0
blewis9991
This is the code I am using:
# To manipulate IOs, just use PSM calls psm.get_var([location name]) to read
# an OpenPLC location and psm.set_var([location name], [value]) to write to
# an OpenPLC location. For example:
# psm.get_var("QX0.0")
# will read the value of %QX0.0. Also:
# psm.set_var("IX0.0", True)
# will set %IX0.0 to true.
import psm
from pymodbus.client import ModbusTcpClient
import struct
# Modbus Configuration
MODBUS_SERVER_IP = '192.168.1.7' # Replace with your Modbus server IP
MODBUS_SERVER_PORT = 502
UNIT_ID = 7
REGISTER_ADDR = 32 # Register 33 in device = Address 32 in 0-based
client = None
def hardware_init():
global client
psm.start()
client = ModbusTcpClient(MODBUS_SERVER_IP, port=MODBUS_SERVER_PORT)
try:
client.connect()
print("Modbus Connected")
except Exception as e:
print(f"Modbus Connection Failed: {e}")
def update_inputs():
global client
if not client or not client.connected:
try:
client.connect()
except Exception as e:
print(f"Reconnect failed: {e}")
return

try:
# Read 2 registers starting at address 32 (Registers 33 & 34)
response = client.read_holding_registers(REGISTER_ADDR, 2, slave=UNIT_ID)
if response.isError():
print(f"Modbus Read Error: {response}")
return

# Get register values
reg33 = response.registers[0]
reg34 = response.registers[1]
# Update OpenPLC inputs using PSM API
psm.set_var("IW100", reg33) # %IW100 = Register 33
psm.set_var("IW101", reg34) # %IW101 = Register 34
except Exception as e:
print(f"Modbus Error: {e}")
client.close()
def update_outputs():
pass # No outputs needed for this device
if __name__ == "__main__":
hardware_init()
while not psm.should_quit():
update_inputs()
update_outputs()
time.sleep(0.1) # Adjust cycle time as needed
client.close()
psm.stop()
Reply 0 0
blewis9991
here is the dashboard variables
[203128%2F9245391%2Fmceclip0]
 
This is the error in the window
OpenPLC Runtime starting...
Interactive Server: Listening on port 43628
PSM: Starting PSM...
sh: 1: ../.venv/bin/python3: not found
PSM: Error while starting Python interpreter
Issued start_modbus() command to start on port: 502
Server: Listening on port 502
Server: waiting for new client...
Issued stop_dnp3() command
Issued start_enip() command to start on port: 44818
Server: Listening on port 44818
Server: waiting for new client...
Issued stop_pstorage() command
Issued start_snap7() command
PSM: Connection to psm failed
PSM: Error connecting to psm!
PSM: PSM is disabled
Device TUF_BT_temp_1 is disconnected. Attempting to reconnect...
Persistent Storage is empty
Connected to MB device TUF_BT_temp_1
Modbus Read Input Registers failed on MB device TUF_BT_temp_1: Illegal function
Modbus Read Holding Registers failed on MB device TUF_BT_temp_1: Bad file descriptor
Device TUF_BT_temp_1 is disconnected. Attempting to reconnect...
Connected to MB device TUF_BT_temp_1
Modbus Read Input Registers failed on MB device TUF_BT_temp_1: Illegal function
Modbus Read Holding Registers failed on MB device TUF_BT_temp_1: Bad file descriptor
Device TUF_BT_temp_1 is disconnected. Attempting to reconnect...
Connected to MB device TUF_BT_temp_1
Modbus Read Input Registers failed on MB device TUF_BT_temp_1: Illegal function
Modbus Read Holding Registers failed on MB device TUF_BT_temp_1: Bad file descriptor
Device TUF_BT_temp_1 is disconnected. Attempting to reconnect...
 
Thank you for replying.
Reply 0 0
blewis9991
This is the start_openplc.sh file, I modified it to try to remove the virtual folder reference, but doesn't seem to matter, still fails

if [ -d "/docker_persistent" ]; then
mkdir -p /docker_persistent/st_files
cp -n /workdir/webserver/dnp3_default.cfg /docker_persistent/dnp3.cfg
cp -n /workdir/webserver/openplc_default.db /docker_persistent/openplc.db
cp -n /workdir/webserver/active_program_default /docker_persistent/active_program
cp -n /workdir/webserver/st_files_default/* /docker_persistent/st_files/
cp -n /dev/null /docker_persistent/persistent.file
cp -n /dev/null /docker_persistent/mbconfig.cfg
fi
cd "/home/rwl1/OpenPLC_v3/webserver"
python3 webserver.py
Reply 0 0
thiagoralves
Who told you that you need to use PSM to read Modbus registers from an external device? You definitely don’t need PSM for that. PSM is a hardware driver to access things that OpenPLC does not bring out of the box. I have a feeling that you have messed up your installation with all those changes and edits. Wipe everything, install OpenPLC runtime and that’s it. To read from an external Modbus device all you have to do is go to Slave Devices and add your device accordingly. The registers of your Modbus device will be mapped to OpenPLC variables at the 100 range (like %QW100, %IW100, etc). You don’t need to change any file, or edit any code, or type anything on terminal, except the 3 commands to install OpenPLC runtime.
Reply 0 0
blewis9991
Where I went wrong if I can even remember is pigpio on pi 5 yielded errors. Another major mistake I made was trying to change the startup to try to startup openplc on boot. This was probably the start of all my grief because it led to to installs that broke things unknowingly. In hindsight I should have just manually started the system after reboot. There was also a problem with port 8080 coming up as in use. Why I do not know that led to further changes. Now embarassing ly, because my background is in control systems but not Linux I used searches with Chatgpt, deepseek, perplexity. There is unfortunately no response on most questions on forums so then problems self induced or not leaves a lot of room for misinformation.

Then because I don't understand the differences of the install modules I was directed by searches (mostly chatgpt) to install a module that yielded an OS response to install into a virtual environment. A rabbit hole that led to even deeper rabbit holes. There were past posts about versions of which python to use and on and on. I watched every video I could find.

Should I manually remove python and PSM or will the install do this? Or do I reinstall Bookworm and start there? Thanks for the help.
Reply 0 0
thiagoralves
You can just remove the OpenPLC_v3 folder and install it again (git clone, etc). This should fix everything. Now, if you want to use the Pi5 GPIO pins, then you'll have to wait a little bit. OpenPLC is not compatible with that yet. There are workarounds posted by users in the forum, but this is a bit convoluted process for someone who doesn't have any Linux experience. Once the support is all baked in, it will be just a matter of installing it and be good to go.
 
If you can live with the Blank hardware with no GPIO support for now, then all you have to do is install the runtime as the instruction says. The installer even puts up a systemd service for you, so next time you reboot the Pi, OpenPLC runtime will start automatically without you having to do anything. And as I said on my previous post, reading/writing to an external Modbus device does not require any coding or fiddling with the runtime at all. You just need to log in to the web portal and add a Slave Device
Reply 0 0
blewis9991
Yes, you were completely right! I see the source of all my trouble and grief. Here it is for someone else's benefit.
 
I unraveled this and it is obvious now. Before I installed OpenPLC the first time, I installed a remote desktop TigerVNC. This installed into a virtual folder. Then when OpenPLC was installed this caused conflicts because of the virtual account TigerVPN had me create and that I was using and it all went down hill from there.
 
I did a complete re-install of Bookworm onto an NVMe and then did a new install of OpenPLC runtime onto the pi 5. It worked exactly as you said. I can read the modbus variables and they are updating.
 
There is one error that is being reported. It seems to be coming from the python code so I will list it here for you, but the variables in Monitoriing are updating.
OpenPLC Runtime starting...
Interactive Server: Listening on port 43628
PSM: Starting PSM...
Traceback (most recent call last):
File "/home/admin/OpenPLC_v3/webserver/./core/psm/main.py", line 53, in <module>
update_inputs()
File "/home/admin/OpenPLC_v3/webserver/./core/psm/main.py", line 42, in update_inputs
TUF_BT_temp_1 = psm.get_var("IW100")
^^^^^^^^^^^^^^^^^^^^
File "/home/admin/OpenPLC_v3/webserver/core/psm/psm.py", line 90, in get_var
return a_inputs.getValues(address)[0]
~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^
IndexError: list index out of range
 
My code is:

Issued start_modbus() command to start on port: 502
Server: Listening on port 502
Server: waiting for new client...
Issued stop_dnp3() command
PSM: Connected to PSM
Device TUF_BT_temp_1 is disconnected. Attempting to reconnect...
Persistent Storage is empty
Connected to MB device TUF_BT_temp_1
Issued start_modbus() command to start on port: 502
Modbus server already active. Restarting on port: 502
Server: Error accepting client!
 
~~~~~~~~~~~~~~~~~~~~~~~
 

#import all your libraries here
import psm
import time
#global variables
counter = 0
var_state = False
def hardware_init():
#Insert your hardware initialization code in here
psm.start()
def update_inputs():
#place here your code to update inputs
global counter
global var_state
TUF_BT_temp_1 = psm.get_var("IW100")
counter += 1
if (counter == 10):
counter = 0
var_state = not var_state

if __name__ == "__main__":
hardware_init()
while (not psm.should_quit()):
update_inputs()
# update_outputs()
time.sleep(0.1) #You can adjust the psm cycle time here
psm.stop()
 
[203128%2F9246521%2Fmceclip0]
~~~~~~~~~~~~~
[203128%2F9246509%2Fmceclip0]
Reply 0 0
Reply