blewis9991
The values are updating, but I have the following error. Can anyone tell me how or where correct this please?
 
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
[203128%2F9250067%2Fblobid0]Junior Member21 posts
 
Write a reply...
thiagoralves
PSM does not have access to address 100 and over, as that address range is reserved for slave devices connected over Modbus.
Reply 1 0
blewis9991
Ok, but I am using modbus devices assigned by openplc who is assigning the devices at 100 - 103, How can I address these devices since openplc runtime/slave is assigning them. I have no option for the address.
Reply 0 0
blewis9991
PSM does not have access to address 100 and over, as that address range is reserved for slave devices connected over Modbus.
Ok, but I am using modbus, and the devices assigned are by openplc who is assigning the devices at 100 - 103, How can I address these devices since openplc runtime/slave is assigning them. I have no option for the address.
Reply 0 0
thiagoralves
You can access them from your PLC program, not PSM. It makes no sense for PSM, being a driver, to access the address space of another driver - the Modbus master in this case. This would be a fight conflict game.
Reply 0 0
thiagoralves
Just to expand on that for a bit, PSM is not the place for you to write logic. The purpose of PSM is to allow OpenPLC to interface hardware it does not have drivers for. For example, one-wire sensors are great candidates for PSM, since it is very easy to interface them from Python. The PSM code should only be responsible for reading the sensors data and make that available in the located buffers (%IX, %IW, etc). If you’re trying to use PSM to write logic, or conditions for your process, then you’re doing it wrong. These things should reside in your PLC programs.
Reply 0 0
blewis9991
You can access them from your PLC program, not PSM. It makes no sense for PSM, being a driver, to access the address space of another driver - the Modbus master in this case. This would be a fight conflict game.
 
You can access them from your PLC program, not PSM. It makes no sense for PSM, being a driver, to access the address space of another driver - the Modbus master in this case. This would be a fight conflict game.
I am confused,
TUF_BT_temp_1 = psm.get_var("IW100")
Is giving the error,
 
What I am trying to do is to retrieve the value and then use it in a python program. I do not want to write logic in the driver. I thought I could convert the value to real but if this is wrong, I will not. How do I retrieve the modbus register value and make it available? Is this done by a global variable or something else? Is there an example you could direct me to? Thank you
Reply 0 0
thiagoralves
If you just want to use this value in a Python program then you don’t need OpenPLC at all. Simply use PyModbus library (or any other Modbus library for Python you may like) to retrieve the registers directly from the slave to your Python program and you’re good to go.
Reply 1 0
blewis9991
If you just want to use this value in a Python program then you don’t need OpenPLC at all. Simply use PyModbus library (or any other Modbus library for Python you may like) to retrieve the registers directly from the slave to your Python program and you’re good to go.
Having pursued OpenPLC thus far I'd rather not start with another system, and I appreciate the whole openplc system as an excellent framework. Plus I want to possiblely use function blocks and custom blocks.
 
The reason I wanted python was because I had a lot of difficulty in type conversions with the ST and FB's. But after I get a working reference point which is reading and displaying a REAL value from the modbus temperature sensor I can figure out which methodology is best.
 
To get the driver value and use it in ST  or elsewhere and avoid the error is what I am trying to do. Can you tell me how to do that please.
Reply 0 0
thiagoralves
Ah, now I see the bigger picture. Ideally you should try to do the conversion in ST, but if that gets too hard, maybe a better approach would be to use PSM instead of the Modbus Slave device driver. Using PSM you can retrieve the value from the slave device using PyModbus in Python. Just beware that PSM uses an older version of PyModbus (2.5.3) that has a different API when compared to the newer one. So just take that in consideration when you're looking for examples and documentation. If fact, here is the documentation specifically for the 2.5.3 version: https://pymodbus-n.readthedocs.io/en/v2.5.3/source/library/pymodbus.html
 
Using PyModbus and PSM you can retrieve the value from your slave device in PSM and then convert that to a REAL value. Since PSM cannot access REAL locations, you can do a trick that makes it easier to pass along to the PLC side. Get the integer part and place it in one location, like %IW0, and then get the decimal part and place it at %IW1 (decimal part multiplied by 1000 so it becomes a regular integer). Then, on the PLC side you can simply retrieve %IW0 and IW1, convert them both to REAL, divide %IW1 by 1000, and finally sum them both together. This is a longer process than just using ST to convert the WORDS to REAL, but is definitely simpler to understand.
 
Perhaps in the end I should look to add a WORDS_TO_REAL conversion block on OpenPLC as many users need that and get confused on how to manually make it happen.
Reply 1 0
blewis9991
Thank you, I will do as you suggested
Reply 0 0
Reply