edouard@3543: #!/bin/bash edouard@3543: edouard@3549: rm -f ./SRVOK ./PLCOK edouard@3543: edouard@3549: # Run server edouard@3549: $BEREMIZPYTHONPATH - > >( edouard@3549: echo "Start SRV loop" edouard@3549: while read line; do edouard@3549: # Wait for server to print modified value edouard@3549: echo "SRV>> $line" edouard@3549: if [[ "$line" == 3.4 ]]; then edouard@3549: echo "PLC could write value" edouard@3549: touch ./SRVOK edouard@3549: fi edouard@3549: done edouard@3549: echo "End SRV loop" edouard@3549: ) << EOF & edouard@3543: edouard@3549: import sys edouard@3718: import os edouard@3549: import time edouard@3820: import asyncio edouard@3549: edouard@3820: from asyncua import Server edouard@3549: edouard@3820: async def main(): edouard@3820: server = Server() edouard@3820: host = os.environ.get("OPCUA_DEFAULT_HOST", "127.0.0.1") edouard@3820: endpoint = "opc.tcp://"+host+":4840/freeopcua/server/" edouard@3820: await server.init() edouard@3820: server.set_endpoint(endpoint) edouard@3549: edouard@3820: uri = "http://beremiz.github.io" edouard@3820: idx = await server.register_namespace(uri) edouard@3549: edouard@3820: objects = server.get_objects_node() edouard@3549: edouard@3820: testobj = await objects.add_object(idx, "TestObject") edouard@3820: testvarout = await testobj.add_variable(idx, "TestOut", 1.2) edouard@3820: testvar = await testobj.add_variable(idx, "TestIn", 5.6) edouard@3820: await testvar.set_writable() edouard@3549: edouard@3820: await server.start() edouard@3820: try: edouard@3820: while True: edouard@3820: await asyncio.sleep(1) edouard@3820: print(await testvar.get_value()) edouard@3820: sys.stdout.flush() edouard@3820: finally: edouard@3820: await server.stop() edouard@3549: edouard@3820: asyncio.run(main()) edouard@3820: edouard@3549: EOF edouard@3549: SERVER_PID=$! edouard@3549: edouard@3549: # Start PLC with opcua test edouard@3549: setsid $BEREMIZPYTHONPATH $BEREMIZPATH/Beremiz_cli.py -k \ edouard@3549: --project-home $BEREMIZPATH/tests/projects/opcua_client build transfer run > >( edouard@3549: echo "Start PLC loop" edouard@3549: while read line; do edouard@3549: # Wait for PLC runtime to output expected value on stdout edouard@3549: echo "PLC>> $line" edouard@3549: if [[ "$line" == 1.2 ]]; then edouard@3549: echo "PLC could read value" edouard@3549: touch ./PLCOK edouard@3549: fi edouard@3549: done edouard@3549: echo "End PLC loop" edouard@3549: ) & edouard@3549: PLC_PID=$! edouard@3549: edouard@3549: echo all subprocess started, start polling results edouard@3549: res=110 # default to ETIMEDOUT edouard@3549: c=30 edouard@3549: while ((c--)); do edouard@3549: if [[ -a ./SRVOK && -a ./PLCOK ]]; then edouard@3549: echo got results. edouard@3549: res=0 # OK success edouard@3549: break edouard@3549: else edouard@3549: echo waiting.... $c edouard@3549: sleep 1 edouard@3543: fi edouard@3543: done edouard@3543: edouard@3549: # Kill PLC and subprocess edouard@3549: echo will kill PLC:$PLC_PID and SERVER:$SERVER_PID edouard@3549: pkill -s $PLC_PID edouard@3549: kill $SERVER_PID edouard@3549: edouard@3549: exit $res