runtime/xenomai.py
author Edouard Tisserant <edouard@beremiz.fr>
Thu, 05 Dec 2024 13:56:59 +0100
changeset 4060 d2f5eb3c7d6e
parent 3750 f62625418bff
permissions -rw-r--r--
py_ext: fix CSV Writer

fix POU logic :
- SAVE is a BOOL
- invocation of py_eval on rising edge of SAVE
- remove save python argument

fix python:
- use no encoding for file open (python2)
- re-use detected dialect if any
- use no "rt+" and truncate since no need to re-sniff dialect for output file
- return "OK" instead of "#SUCCESS", preventing POU logic to ACK result
- support creating new line if writing just after last line
- support appending data on short rows

fix example:
- use a HMI:Button to trigger CSV write instead of HMI:Input +1
- reload CSVs on on each new CSV opened in file browser
- add display of CSV write output
2015
4eeefc6a13fd Fix codestyle
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2000
diff changeset
     1
#!/usr/bin/env python
4eeefc6a13fd Fix codestyle
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2000
diff changeset
     2
# -*- coding: utf-8 -*-
4eeefc6a13fd Fix codestyle
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2000
diff changeset
     3
#
4eeefc6a13fd Fix codestyle
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2000
diff changeset
     4
# See COPYING.Runtime file for copyrights details.
4eeefc6a13fd Fix codestyle
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2000
diff changeset
     5
#
4eeefc6a13fd Fix codestyle
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2000
diff changeset
     6
3750
f62625418bff automated conversion using 2to3-3.9 tool
GP Orcullo <kinsamanka@gmail.com>
parents: 2015
diff changeset
     7
2000
9fa2f8ede5d6 Fixed random segfault happening when loading new PLC in runtime, when using Xenonai.
Edouard Tisserant
parents:
diff changeset
     8
from ctypes import CDLL, RTLD_GLOBAL, pointer, c_int, POINTER, c_char, create_string_buffer
2015
4eeefc6a13fd Fix codestyle
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2000
diff changeset
     9
4eeefc6a13fd Fix codestyle
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2000
diff changeset
    10
2000
9fa2f8ede5d6 Fixed random segfault happening when loading new PLC in runtime, when using Xenonai.
Edouard Tisserant
parents:
diff changeset
    11
def TryPreloadXenomai():
9fa2f8ede5d6 Fixed random segfault happening when loading new PLC in runtime, when using Xenonai.
Edouard Tisserant
parents:
diff changeset
    12
    """
9fa2f8ede5d6 Fixed random segfault happening when loading new PLC in runtime, when using Xenonai.
Edouard Tisserant
parents:
diff changeset
    13
    Xenomai 3 (at least for version <= 3.0.6) do not handle properly dlclose
9fa2f8ede5d6 Fixed random segfault happening when loading new PLC in runtime, when using Xenonai.
Edouard Tisserant
parents:
diff changeset
    14
    of shared objects whose dlopen did trigger xenomai_init.
2015
4eeefc6a13fd Fix codestyle
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2000
diff changeset
    15
    As a workaround, this pre-loads xenomai libraries that need to be
2000
9fa2f8ede5d6 Fixed random segfault happening when loading new PLC in runtime, when using Xenonai.
Edouard Tisserant
parents:
diff changeset
    16
    initialized and call xenomai_init once for all.
2015
4eeefc6a13fd Fix codestyle
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2000
diff changeset
    17
2000
9fa2f8ede5d6 Fixed random segfault happening when loading new PLC in runtime, when using Xenonai.
Edouard Tisserant
parents:
diff changeset
    18
    Xenomai auto init of libs MUST be disabled (see --auto-init-solib in xeno-config)
9fa2f8ede5d6 Fixed random segfault happening when loading new PLC in runtime, when using Xenonai.
Edouard Tisserant
parents:
diff changeset
    19
    """
9fa2f8ede5d6 Fixed random segfault happening when loading new PLC in runtime, when using Xenonai.
Edouard Tisserant
parents:
diff changeset
    20
    try:
9fa2f8ede5d6 Fixed random segfault happening when loading new PLC in runtime, when using Xenonai.
Edouard Tisserant
parents:
diff changeset
    21
        for name in ["cobalt", "modechk", "copperplate", "alchemy"]:
9fa2f8ede5d6 Fixed random segfault happening when loading new PLC in runtime, when using Xenonai.
Edouard Tisserant
parents:
diff changeset
    22
            globals()[name] = CDLL("lib"+name+".so", mode=RTLD_GLOBAL)
2015
4eeefc6a13fd Fix codestyle
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2000
diff changeset
    23
        cobalt.xenomai_init(pointer(c_int(0)), pointer((POINTER(c_char)*2)(create_string_buffer("prog_name"), None)))
4eeefc6a13fd Fix codestyle
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2000
diff changeset
    24
    except Exception:
2000
9fa2f8ede5d6 Fixed random segfault happening when loading new PLC in runtime, when using Xenonai.
Edouard Tisserant
parents:
diff changeset
    25
        pass