rajatdas2005
Hi,
I have a 32 bit embedded device with Linux kernel in it. I want to use OpenPLC runtime from terminal, with .st file as argument and want to communicate with a Modbus TCP device to access GPIO.
 
First I have cross compiled it for 32 bit background_installer.sh , installed
  1. install_py_deps
  2. install_matiec
  3. install_st_optimizer
  4. install_glue_generator
  5. install_libmodbus
  6. install_systemd_service
  7. then hardware layer selected as blank.cpp and blank_program.st
  8. disabled ethercat and opendnp3
Generated ie2c, openplc, st_optimizer, glue_generator all for 32 bit. and also related libraries like libmodbus.so.5, libstdc++.so.6, libm.so.6, libgcc_s.so.1, libc.so.6, ld-linux.so.2, libmodbus.so

Next for a new .st file according to compile_program.sh
  1. ./iec2c -f -l -p -r -R -a ./st_files/"$1"
  2. mv -f POUS.c POUS.h LOCATED_VARIABLES.h VARIABLES.csv Config0.c Config0.h Res0.c ./core/
  3. g++ *.cpp *.o -o openplc -I ./lib -pthread -fpermissive -I /usr/local/include/modbus -L /usr/local/lib -lmodbus -w
all the above 3 steps i have done it according to 32 bit setting. also i have manually written a mbconfig.cfg file.
After all these when i am running

sudo ./openplc
OpenPLC Runtime starting...
Device 0
Name: ModbusTCPDevice
Interactive Server: Listening on port 43628
Interactive Server: waiting for new client...
Protocol: 1
Address: 10.135.114.62
IP Port: 502
Baud rate: 0
Parity:
Data Bits: 0
Stop Bits: 0
DI Start: 0
DI Size: 8
Coils Start: 0
Coils Size: 8
IR Start: 0
IR Size: 0
HR Start: 0
HR Size: 0

Device ModbusTCPDevice is disconnected. Attempting to reconnect...
Persistent Storage is empty
Setting main thread priority to RT
Locking main thread memory
Connected to MB device ModbusTCPDevice
Getting current time
Modbus Read Discrete Input Registers failed on MB device ModbusTCPDevice: Invalid data
Modbus Write Coils failed on MB device ModbusTCPDevice: Bad file descriptor
Device ModbusTCPDevice is disconnected. Attempting to reconnect...
Connected to MB device ModbusTCPDevice

 
But the modbustcp device is not responding according to .st program. The same .st program i have tested using webserver.py and its worked.
fg71
There is rpi-test folder in libmodbus_src you could update that to work with your 32-bit embedded device.
If you get that working, I guess there is other issue that do not make your Modbus to work.
 
At least you use the same test example that use the same libraries as OpenPLC_v3 and better debugging.
Reply 0 0
thiagoralves
The best way to view what is happening perhaps is get a wireshark session between these two devices. It seems that the runtime was able to connect with the slave device, but the slave's response was wrong. Maybe the issue is in your slave, not on the runtime.
Reply 0 0
rajatdas2005
Hi, thanks for your reply, but the same slave device is able to communicate with the openplc runtime via web browser using the same .st file.
Reply 0 0
thiagoralves
Yes, clearly it is not working correctly on your custom device. Wireshark may give you hints on why.
Reply 0 0
rajatdas2005
Thank you for your reply. Just one more query, what is the role of glue_generator in openplc runtime ?
Reply 0 0
fg71
glue_vars is the binding between physical hardware and plc program.
 
%QX0.3 in your location table in OpenPLC Editor is in C variable __QX0_3
The bool_output is array there [0] is index of object containing 8 elements and the [3] is element 4 (start at 0)
So when you configure your Digital Output 7, 8, 12, 13 (Arduino pin number) element 4 become index 3 and that is Arduino pin 13 (start at 0)
 
ex.
 
void glueVars()
{
    bool_output[0][3] = __QX0_3;
}
 
Reply 0 0
rajatdas2005
Thank you.
This is my mbconfig.cfg file,
Num_Devices = "1"
Polling_Period = "100"
Timeout = "1000"
# ------------
# DEVICE 0
# ------------
device0.name = "Test_1"
device0.slave_id = "1"
device0.protocol = "TCP"
device0.address = "10.135.114.62"
device0.IP_Port = "502"
device0.RTU_Baud_Rate = "115200"
device0.RTU_Parity = "None"
device0.RTU_Data_Bits = "8"
device0.RTU_Stop_Bits = "1"
device0.RTU_TX_Pause = "0"
device0.Discrete_Inputs_Start = "0"
device0.Discrete_Inputs_Size = "8"
device0.Coils_Start = "0"
device0.Coils_Size = "8"
device0.Input_Registers_Start = "0"
device0.Input_Registers_Size = "0"
device0.Holding_Registers_Read_Start = "0"
device0.Holding_Registers_Read_Size = "0"
device0.Holding_Registers_Start = "0"
device0.Holding_Registers_Size = "0"

-------------------------
and in openplc runtime i have selected hardware layer as blank and in slave section i have selected a modbus tcp device (10.135.114.62). In this devices i am running a modbus server code:
# Set up the GPIO pins
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(27, GPIO.IN)
# Initialize the Modbus data store
store = ModbusSlaveContext(
di=ModbusSequentialDataBlock(0, [0]*100),
co=ModbusSequentialDataBlock(0, [0]*100),
hr=ModbusSequentialDataBlock(0, [0]*100),
ir=ModbusSequentialDataBlock(0, [0]*100))
context = ModbusServerContext(slaves=store, single=True)
def run_server():
log.info("Starting Modbus server...")
StartTcpServer(context, identity=identity, address=("0.0.0.0", 502))
def update_gpio():
previous_coil_value = None # Store the last known state of the coil for GPIO 17
previous_gpio_27_state = None # Store the last known state of GPIO 27 input
while True:
# Read coil value at address 0 (controls GPIO 17)
coil = context[0].getValues(5, 0, 1)
if coil[0] != previous_coil_value: # Log and update only if coil state changes
log.info(f"GPIO 17 state changed to: {coil[0]}")
GPIO.output(17, coil[0])
previous_coil_value = coil[0]
# Read the state of GPIO 27 (input pin)
gpio_27_state = GPIO.input(27)
if gpio_27_state != previous_gpio_27_state: # Log only if input state changes
log.info(f"GPIO 27 input state changed to: {gpio_27_state}")
previous_gpio_27_state = gpio_27_state

# Update the Modbus data store with the state of GPIO 27
context[0].setValues(2, 0, [gpio_27_state])
# Add a short delay to reduce CPU usage
time.sleep(0.1)
-----------------------------------------
So where my openplc runtime IX100 and QX100 address is getting mapped with the modbus server code ?
Reply 0 0
thiagoralves
It gets mapped on the fly when the runtime parses the mbconfig file. The code for that is at modbus_master.cpp
Reply 0 0
rajatdas2005
Thank you once again
Reply 0 0
rajatdas2005
Now, openplc runtime is running and it able to connect with a modbus server device. Earlier I have not executed glue_generator.

Next I have a another question during cross compilation of openplc runtime I have also generated libmodbus.so, and all relevant libraries and all are kept in a local "dependencies" folder.
However when I am running openplc runtime from terminal (../dependencies/ld-linux.so.2 --library-path ../dependencies ./openplc) the modbus server is not starting, so how to enable or disable modbus server of openplc runitime ? I am not running openplc from via webserver.
Reply 0 0
thiagoralves
To start Modbus (or any other server) you will have to connect to the localhost interactive server at port 43628 and then issue commands to start the servers you want. See https://github.com/thiagoralves/OpenPLC_v3/blob/master/webserver/core/interactive_server.cpp
Reply 0 0
Reply