Sunny
Hello,
I'm trying to call Windows API in generated C Code. But it is wrong to compile because some data types conflict with that in windows. e.g: INT, BOOL and so on.
[203128%2F8298516%2Fmceclip0]
 
The situation is like this: I have one process which operates hardware and exposes APIs. The simulator of OpenPLC editor is another process which executes the compiled code.(I'm new to OpenPLC, so I'm not sure if my understanding is right.) So if I can call provided APIs from another process via IPC, I can control my hardware with OpenPLC Editor.
 
So how can I solve this problem or is there another better solution for this situation?
 
A greeting and thanks.
Reply 0 0
thiagoralves
Instead of modifying the editor’s simulator to generate the code you want, you should modify the runtime that actually executes the code generated by the editor. The OpenPLC runtime is multi platform. You can get the windows installer from here:  https://openplcproject.com/docs/installing-openplc-runtime-on-windows/
 
This installer basically installs Cygwin and then clones the OpenPLC runtime repository from https://github.com/thiagoralves/OpenPLC_v3.git
 
On the OpenPLC runtime there is something called PSM (Python SubModule). You can enable it on the Hardware tab of the runtime’s webserver. PSM allows you to write OpenPLC drivers using Python. That’s the easiest way to integrate something else with OpenPLC. If Python is not fast enough for you, you can at least prototype it in Python, and then if it works, port it over to C and write a “real” OpenPLC driver.
Reply 0 0
Sunny
OK, I will try this method. Thanks so much for your reply.
Reply 0 0
Sunny
Hello,
I wrote a simple demo project. My PSM code is like this:
#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()
    f = open('GPIO.txt', 'w')
    f.write('0')
    f.close()
    f = open('Axis.txt', 'w')
    f.write('0\n0')
        
def execute_cmd(cmd: int):
    print("call success")
    if(cmd == 0):
        print("call properly")
        f = open('GPIO.txt', 'r')
        values = f.readline()
        port = psm.get_var("IW2")
        value = psm.get_var("IX0.0")
        if(value == False):
            mask = -1 - (1 << port)
            values = int(values) & mask
            print("False")
            print(values)
        else:
            mask = 1 << port
            values = int(values) | mask
            print("True");
            print(values);
        f.close()
        f.open('GPIO.txt', 'w')
        f.write(values)
        f.close()
        
        
if __name__ == "__main__":
    hardware_init()
    print(psm.get_var("IW0"))
    while (not psm.should_quit()):
        print("psm looping")
        while(psm.get_var("IW0") != 1):
            time.sleep(1)
            print("psm waiting")
        cmd = psm.get_var("IW1")
        execute_cmd(cmd)
        time.sleep(1) #You can adjust the psm cycle time here
    psm.stop()
 
PSM server will wait until the "IW0" is set to 1. Then it uses file operations to simulate IO control. My PLC program is like this:
[203128%2F8303431%2Fmceclip0] 
(“本地” means "local")
But When I uploaded this program to OpenPLC Runtime, it didn't work as I expected. PSM server just waited and did nothing. In Monitor Tag, the result is like this:
[203128%2F8303428%2Fmceclip0]
Only the variable state switched back and forth between zero and one. the function "execute_cmd" was not called properly.
 
I have no idea about how to solve this problem. Thanks for any help.
Reply 0 0
thiagoralves
This statement
while(psm.get_var("IW0") != 1):
 
is hanging PSM, so it is never updating its values and this statement never becomes false.
 
Keep in mind that PLCs are state machines. You can’t have them waiting on signals on a while loop, especially a PLC driver. It must execute and exit as fast as possible.
 
You should keep PSM main function untouched from the base example, and then work in the update input and output functions with “if” statements instead of “while”
Reply 0 0
Sunny

After reviewing the source code of OpenPLC, I think I have roughly understood the role of PSM. It is a Modbus Server which serves as a hardware driver connecting OpenPlc_Runtime with hardware. So it is only responsible for the communication, but does not deal with complex logical things.

[203128%2F8311615%2Fmceclip0]

What I want to realize is that when PLC modifies a variable located at %IW0, %IX0_0, etc, OpenPLC_Runtime can do some more complicated things, like sending a Http Request. 

I contrive another plan to achieve my goal. Instead of putting these logic in PSM, I decide to add a new hardware layer just like PSM by adding a new .cpp file under core/hardware_layers.(or modifying psm.cpp) Also, When I was studying the source code, I found that there exists a file called 'custom_layer.cpp'. I haven't quite figured out how to use this file yet. I feel that it may help me to fulfill my purpose, but I'm not sure.

Could you please tell me about whether this solution could possibly solve my problem? And If it won't work, what should I do? I will appreciate if you could give me some advices. Thank you very much.

Reply 0 0
thiagoralves
Do you want to setup a call to get this cleared up? I think it will be a lot easier to explain all that to you in a 15 min call than going over forum posts. Let me know your availability. I’m at EDT time zone
Reply 0 0
Sunny
That is very nice of you. I'm at CST time zone, which is 12 hours earlier than EDT. Do you have time during 8:00-11:00 am, August 13 (EDT), which is 8:00-11:00 pm, August 13 (CST) here. You can arrange the time at your convenience and email me the online meeting link (frostmourne@sjtu.edu.cn) or reply me directly here. Thank you again for your generous helping.
Reply 0 0
thiagoralves
I’m available on Aug 14th 8am EDT. Does that work for you?
Reply 0 0
Sunny
Yes, I will be available at that time.
Reply 0 0
thiagoralves
Just sent you an invite for tomorrow 8:30am
Reply 0 0
Sunny
Hello sir.
I want to ask a question about OpenPLC Editor. If I want to add a new Function Block to the library in which I have to define a local variable binded to a position like "%IW0", what should I write in my .xml file?
 
Thanks for any help!
Reply 0 0
thiagoralves
You can't have Function Blocks binded to located variables. Functions and Function Blocks are generic structures. The user should be responsible for binding the right variables to its inputs and outputs.
Reply 0 0
Sunny
Okay, I see. We will adjust our design.
Reply 0 0
Reply