gtbeean
I have done a few fresh installs of Raspbian Bullseye, and consistently see that pymodbus is not available to psm.
Install steps:
1. Raspberry Pi Imager, selecting Raspbian Bullseye Lite x64.
2. sudo apt update, sudo apt full-upgrade, reboot
4. git clone openplc_v3 and run ./ install.sh rpi
5. Start openplc service
6. Login at raspberrypi:8080
7. Change hardware to psm
8. Start PLC
 
 
Logs:
OpenPLC Runtime starting...
Interactive Server: Listening on port 43628
PSM: Starting PSM...
Traceback (most recent call last):
File "/home/openplc/OpenPLC_v3/webserver/./core/psm/main.py", line 27, in <module>
import psm
File "/home/openplc/OpenPLC_v3/webserver/core/psm/psm.py", line 3, in <module>
from pymodbus.server.sync import ModbusTcpServer
ModuleNotFoundError: No module named 'pymodbus'
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 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
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
Skipping configuration of Slave Devices (mbconfig.cfg file not found)
Warning: Persistent Storage file not found
 
 
I had a look within the OpenPLC install script, and it appears that the python dependencies are installed within a venv in the OpenPLC_v3 folder.
 
`pip freeze` after activating the venv shows pymodbus==2.5.3 is installed.
 
 
I had a look within the OpenPLC source code. I don't know c++, but I searched for "python" and found, within psm.cpp the following function.
psm.cpp no venv.png
 
The image shows the error message I see in the log, and also seems to show the psm.cpp isn't using the python executable from the venv, which is where all the dependencies are installed?
 
Please correct me, I very much expect to have missed something important somewhere!
 
Assuming I am wrong about this, please can someone help me fix my problem here?
 
 
Edit:
To be clear, I am using the blank_program and the default psm script.
Also, I have just tested on Raspbian 11 x86 and I get the same issue.
Reply 0 0
thiagoralves
I don’t have a Pi5 to test, but being a regular Debian OS it should work just fine. What is the output of the commands you’re typing? What exactly is not working?
Reply 0 0
richw
when trying  "python3 -m pip install pymodbus==2.5.3:" the following below shows.
 
error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
    python3-xyz, where xyz is the package you are trying to
    install.

    If you wish to install a non-Debian-packaged Python package,
    create a virtual environment using python3 -m venv path/to/venv.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
    sure you have python3-full installed.

    If you wish to install a non-Debian packaged Python application,
    it may be easiest to use pipx install xyz, which will manage a
    virtual environment for you. Make sure you have pipx installed.

    See /usr/share/doc/python3.11/README.venv for more information.

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.
 
when i try to follow the instructions in the error message 
   
    "try apt install
    python3-xyz, where xyz is the package you are trying to
    install."
 
I use "sudo apt install python3-pymodbus==2.5.3"
I get "E: Unable to locate package python3-pymodbus="
 
If i use  "sudo apt install python3-pymodbus"
it installs pymodbus the newest version (3.0.0-7)
 
But I still get the same error when I try to run PSM in OpenPLC
Reply 0 0
thiagoralves
Got it. In that case it seems that raspbian is forcing the usage of venv everywhere. OpenPLC already uses venv, but PSM uses system Python as it is easier to install user libraries that may be used with PSM. I’ll change PSM to use OpenPLC’s venv instead and it will fix this
Reply 0 0
richw
Just wondering what the time frame would be for this change to  be incorporated.
Reply 0 0
thiagoralves
It is literally a one line change. On file psm.cpp ( https://github.com/thiagoralves/OpenPLC_v3/blob/master/webserver/core/hardware_layers/psm.cpp ) change line 77 to:
char *cmd = "../.venv/bin/python3 -u ./core/psm/main.py 2>&1";
Since I don't have a Raspberry Pi 5 to test I don't know if it works or not. Make the change in your installation, reapply the PSM driver and let us know if it worked please.
Reply 0 0
richw
After  making the change and reapplying the PSM driver I receive the errors below.
 
Optimizing ST program...
Generating C files...
POUS.c
POUS.h
LOCATED_VARIABLES.h
VARIABLES.csv
Config0.c
Config0.h
Res0.c
Moving Files...
Compiling for Linux
Generating object files...
Generating glueVars...
Compiling main program...
hardware_layer.cpp:77:5: error: extended character   is not valid in an identifier
77 | char *cmd = "../.venv/bin/python3 -u ./core/psm/ main.py  2>&1";
| ^
hardware_layer.cpp:77:11: error: extended character   is not valid in an identifier
77 | char *cmd = "../.venv/bin/python3 -u ./core/psm/ main.py  2>&1";
| ^
hardware_layer.cpp:77:16: error: extended character   is not valid in an identifier
77 | char *cmd = "../.venv/bin/python3 -u ./core/psm/ main.py  2>&1";
| ^
hardware_layer.cpp: In function ‘void* start_psm()’:
hardware_layer.cpp:77:5: error: ‘char ’ was not declared in this scope; did you mean ‘char’?
77 | char *cmd = "../.venv/bin/python3 -u ./core/psm/ main.py  2>&1";
| ^~~~~
| char
hardware_layer.cpp:77:11: error: ‘cmd ’ was not declared in this scope
77 | char *cmd = "../.venv/bin/python3 -u ./core/psm/ main.py  2>&1";
| ^~~~
hardware_layer.cpp:77:16: error: ‘ ’ was not declared in this scope
77 | char *cmd = "../.venv/bin/python3 -u ./core/psm/ main.py  2>&1";
| ^
hardware_layer.cpp:82:27: error: ‘cmd’ was not declared in this scope
82 | if ((psm_proc = popen(cmd, "r")) == NULL)
| ^~~
Error compiling C files
Compilation finished with errors!
Reply 0 0
thiagoralves
It seems that you added extra characters (77 | ) to the line I sent you. Make sure you only type what I sent.
Reply 0 0
richw
I did not type it, I used the copy function to the right side of the line but I will try again.
Reply 0 0
richw
Everything seems to be working correctly now.
 
OpenPLC Runtime starting...
Interactive Server: Listening on port 43628
PSM: Starting PSM...
Issued start_modbus() command to start on port: 502
Server: Listening on port 502
Server: waiting for new client...
Issued stop_dnp3() command
Issued stop_enip() command
Issued stop_pstorage() command
PSM: Connected to PSM
Warning: Persistent Storage file not found
Reply 1 0
richw
Where is the python virtual environment located at for OpenPLC. If I understand correctly I need to know this to install a python module.
Reply 0 0
thiagoralves
OpenPLC_v3/.venv/bin/python3
 
You can call that binary with -m pip install to install modules
Reply 0 0
richw
installed the python module using "OpenPLC_v3/.venv/bin/python3 -m pip install rpi-lgpio" and it installed just fine, but when I import the module in PSM and start OpenPLC  I get the error below. I can go to  "OpenPLC_v3/.venv/bin" directory run Python3 and inport lgpio moudle with no errors. Is there something else I need to do, or am I dooing something wrong?
 
PSM: Starting PSM...
Traceback (most recent call last):
File "/home/openplc/OpenPLC_v3/webserver/./core/psm/main.py", line 29, in <module>
import lgpio
ModuleNotFoundError: No module named 'lgpio'
PSM: Error while starting Python interpreter
PSM: Connection to psm failed
PSM: Error connecting to psm!
PSM: PSM is disabled 
Reply 0 0
thiagoralves
That’s odd. If the modification you did at psm.cpp is working and calling the venv Python, then there is no reason for it not to find the module. Are you sure you installed it correctly? Can you post here your pip command and the output you get when you try to install it?
Reply 0 0
richw
The pip command I used is   "OpenPLC_v3/.venv/bin/python3 -m pip install rpi-lgpio"
since its already installed the reply I get is   "Requirement already satisfied: rpi-lgpio in ./OpenPLC_v3/.venv/lib/python3.11/site-packages (0.4)"
 
If I remove the import it works
Reply 0 0
richw
just wondering if there is any suggestions on how to fix this problem with PSM. I am able to run scripts with python and import LGPIO when logged in with SSH but not using PSM with OpenPLC runtime. I can't even use the RPI driver because the new Linux kernel doesn't use rpi.gpio any more so if you have an RPI5 or upgrade your older RPI to the newer RPI OS you are dead in the water and unable to use any GPIO.
Reply 0 0
Reply