From a6a0e2d66ddabb6c901ca3860409849d8c34c5d6 Mon Sep 17 00:00:00 2001 From: Lea Tonejca Date: Thu, 27 Jul 2023 11:33:06 +0200 Subject: [PATCH 1/9] add method to read macro variables --- fwlib.i | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/fwlib.i b/fwlib.i index 27273c9..1bf0083 100644 --- a/fwlib.i +++ b/fwlib.i @@ -313,6 +313,25 @@ void deinit() $result = SWIG_Python_AppendOutput($result, o); %} +%typemap(in,numinputs=0) (unsigned long *num, double *data) %{ + unsigned long temp2 = 1; + $1 = &temp2; + double temp3 = 0; + $2 = &temp3; +%} +%typemap(argout) (unsigned long *num, double *data) %{ +PyObject *o = PyTuple_New(2); +PyObject *oo; +int num2 = *$1; +float num3 = *$2; + +oo = PyLong_FromLong(num2); +PyTuple_SetItem(o, 0, oo); +oo = PyLong_FromLong(num3); +PyTuple_SetItem(o, 1, oo); + +$result = SWIG_Python_AppendOutput($result, o); +%} %typemap(in, numinputs=0) (ODBEXEPRG *exeprg) %{ ODBEXEPRG_T temp; @@ -420,3 +439,4 @@ short cnc_exeprgname2(unsigned short, char *path_name); short cnc_rdopmsg(unsigned short, short type, short length, OPMSG *opmsg); short cnc_statinfo(unsigned short, ODBST *statinfo); short cnc_freelibhndl(unsigned short libh); +short cnc_rdmacror2(unsigned short, unsigned long s_no, unsigned long *num, double *data); \ No newline at end of file From 33b5909a6b28ca07144c4df1681f6809a5b8d9fe Mon Sep 17 00:00:00 2001 From: Thomas Trautner Date: Mon, 31 Jul 2023 16:36:40 +0200 Subject: [PATCH 2/9] create package and add requirements --- LICENSE | 21 ++++++++++++ README.md | 6 ++++ __init__.py | 0 main.py | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++ setup.py | 6 ++-- 5 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 LICENSE create mode 100644 __init__.py create mode 100644 main.py diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..6c29b3f --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 tonejca + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md index 70bdebf..94a6f2b 100644 --- a/README.md +++ b/README.md @@ -31,3 +31,9 @@ ln -s extern/fwlib/libfwlib32-linux-x64.so.1.0.5 libfwlib32.so gcc -fPIC -shared fwlib_wrap.c -o _fwlib.so -L. -lpthread -lm -lfwlib32 -I/usr/local/include/python3.9 # compile python module LD_LIBRARY_PATH=. python3.9 test.py # verify module works ``` + +## build package +(extended by tonejca) +``` +python3 setup.py bdist_wheel sdist +``` \ No newline at end of file diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/main.py b/main.py new file mode 100644 index 0000000..17b6899 --- /dev/null +++ b/main.py @@ -0,0 +1,97 @@ +# -- Imports ------------------------------------------------------------------ +import fwlib # namespace +from types import TracebackType +from typing import Optional, Type + + +class FocasException(Exception): + pass + + +class UnableToReadMacro(FocasException): + """Raised when a macro could not be read""" + + +class UnableToReadAxis(FocasException): + """Raised when an axis could not be read""" + + +class FocasController: + def __init__(self, ip: str, port: int = 8193, sample_rate: int = 10): + self.ip = ip + self.port = port + self.sample_rate = sample_rate + self.library_handle = None + + def __enter__(self): # -> Focas: + """Initialize the connector class""" + + self.connect() + return self + + def __exit__( + self, + exception_type: Optional[Type[BaseException]], + exception_value: Optional[BaseException], + traceback: Optional[TracebackType], + ) -> None: + """Clean up the resources used by the storage class + + Parameters + ---------- + + exception_type: + The type of the exception in case of an exception + + exception_value: + The value of the exception in case of an exception + + traceback: + The traceback in case of an exception + + """ + + self.disconnect() + + def connect(self): + ret, self.libh = fwlib.cnc_allclibhndl3(self.ip, self.port, self.sample_rate) + if ret != 0: + raise ConnectionError( + f"Unable to connect to Focas control (IP: {self.ip}, Port:" + f" {self.port})" + ) + + self.library_handle = self.libh + assert self.library_handle is not None, "Library handle not initialized" + + def disconnect(self): + fwlib.cnc_freelibhndl(self.library_handle) + + def read_macro(self, register: int) -> int: + ret, (_, dataFeature) = fwlib.cnc_rdmacror2(self.library_handle, register) + if ret != 0: + raise UnableToReadMacro(f"Unable to read macro “{register}”") + return dataFeature + + def read_axis(self, category: int, index: int) -> int: + ret, axisdata = fwlib.cnc_rdaxisdata(self.library_handle, category, [index]) + if ret != 0: + raise UnableToReadAxis(f"Unable to read axis data “{category}”, “{index}”") + return axisdata + + +if __name__ == "__main__": + nc_data = [] + focas_controller = FocasController("192.168.1.10") + with focas_controller: + try: + axis_tuples = [[1, 0], [2, 0], [3, 0], [5, 0], [5, 1]] + for tuple in axis_tuples: + axisdata = focas_controller.read_axis(tuple[0], tuple[1]) + for axis in axisdata: + nc_data.append(axis[1]) + print(nc_data) + except (ConnectionError, UnableToReadMacro, UnableToReadAxis) as error: + print(error) + focas_controller.disconnect() + print(nc_data) diff --git a/setup.py b/setup.py index 63e4199..10c794a 100644 --- a/setup.py +++ b/setup.py @@ -17,12 +17,13 @@ else: pass fname = f"libfwlib32-{plat}-{arch}.so.{version}" - print(f"{fname=}", f"{libpath=}") + print(fname) + # print(f"{fname=}", f"{libpath=}") os.symlink(fname, os.path.join(fwlib_dir, "libfwlib32.so")) os.symlink(fname, os.path.join(fwlib_dir, "libfwlib32.so.1")) setup( - name="fwlib", + name="pyfwlib", version="0.1", description="", ext_modules=[ @@ -39,6 +40,7 @@ runtime_library_dirs=[fwlib_dir], ) ], + install_requires=["swig==4.1.1", "wheel==0.41.0", "setuptools==68.0.0",], # used to locate c header files include_dirs=[fwlib_dir], package_data={ From 643016ada39ac6ca5254acaa69f777751a55e11d Mon Sep 17 00:00:00 2001 From: Thomas Trautner Date: Mon, 31 Jul 2023 16:38:43 +0200 Subject: [PATCH 3/9] create package and add requirements --- pyfwlib.egg-info/PKG-INFO | 4 ++++ pyfwlib.egg-info/SOURCES.txt | 9 +++++++++ pyfwlib.egg-info/dependency_links.txt | 1 + pyfwlib.egg-info/requires.txt | 3 +++ pyfwlib.egg-info/top_level.txt | 1 + 5 files changed, 18 insertions(+) create mode 100644 pyfwlib.egg-info/PKG-INFO create mode 100644 pyfwlib.egg-info/SOURCES.txt create mode 100644 pyfwlib.egg-info/dependency_links.txt create mode 100644 pyfwlib.egg-info/requires.txt create mode 100644 pyfwlib.egg-info/top_level.txt diff --git a/pyfwlib.egg-info/PKG-INFO b/pyfwlib.egg-info/PKG-INFO new file mode 100644 index 0000000..63350ff --- /dev/null +++ b/pyfwlib.egg-info/PKG-INFO @@ -0,0 +1,4 @@ +Metadata-Version: 2.1 +Name: pyfwlib +Version: 0.1 +License-File: LICENSE diff --git a/pyfwlib.egg-info/SOURCES.txt b/pyfwlib.egg-info/SOURCES.txt new file mode 100644 index 0000000..694828c --- /dev/null +++ b/pyfwlib.egg-info/SOURCES.txt @@ -0,0 +1,9 @@ +LICENSE +README.md +fwlib.i +setup.py +pyfwlib.egg-info/PKG-INFO +pyfwlib.egg-info/SOURCES.txt +pyfwlib.egg-info/dependency_links.txt +pyfwlib.egg-info/requires.txt +pyfwlib.egg-info/top_level.txt \ No newline at end of file diff --git a/pyfwlib.egg-info/dependency_links.txt b/pyfwlib.egg-info/dependency_links.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/pyfwlib.egg-info/dependency_links.txt @@ -0,0 +1 @@ + diff --git a/pyfwlib.egg-info/requires.txt b/pyfwlib.egg-info/requires.txt new file mode 100644 index 0000000..63e31bf --- /dev/null +++ b/pyfwlib.egg-info/requires.txt @@ -0,0 +1,3 @@ +swig==4.1.1 +wheel==0.41.0 +setuptools==68.0.0 diff --git a/pyfwlib.egg-info/top_level.txt b/pyfwlib.egg-info/top_level.txt new file mode 100644 index 0000000..9327967 --- /dev/null +++ b/pyfwlib.egg-info/top_level.txt @@ -0,0 +1 @@ +_fwlib From 802acf4a1c4228d013b63452b804203750e54039 Mon Sep 17 00:00:00 2001 From: Lea Tonejca Date: Wed, 2 Aug 2023 11:31:53 +0200 Subject: [PATCH 4/9] name module pyfwlib --- fwlib.i | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fwlib.i b/fwlib.i index 1bf0083..1213e62 100644 --- a/fwlib.i +++ b/fwlib.i @@ -1,4 +1,4 @@ -%module fwlib +%module pyfwlib %{ #include "./extern/fwlib/fwlib32.h" typedef struct odbaxdt_t { From 084b59f0122a32ba921c5091af9ec174fbfebedb Mon Sep 17 00:00:00 2001 From: Lea Tonejca Date: Thu, 3 Aug 2023 11:35:50 +0200 Subject: [PATCH 5/9] add macro variable function --- fwlib.i | 2 +- setup.py | 4 ++-- src/fwlib | 1 + 3 files changed, 4 insertions(+), 3 deletions(-) create mode 160000 src/fwlib diff --git a/fwlib.i b/fwlib.i index 1bf0083..1213e62 100644 --- a/fwlib.i +++ b/fwlib.i @@ -1,4 +1,4 @@ -%module fwlib +%module pyfwlib %{ #include "./extern/fwlib/fwlib32.h" typedef struct odbaxdt_t { diff --git a/setup.py b/setup.py index 63e4199..5eba353 100644 --- a/setup.py +++ b/setup.py @@ -17,12 +17,12 @@ else: pass fname = f"libfwlib32-{plat}-{arch}.so.{version}" - print(f"{fname=}", f"{libpath=}") + # print(f"{fname=}", f"{libpath=}") os.symlink(fname, os.path.join(fwlib_dir, "libfwlib32.so")) os.symlink(fname, os.path.join(fwlib_dir, "libfwlib32.so.1")) setup( - name="fwlib", + name="pyfwlib", version="0.1", description="", ext_modules=[ diff --git a/src/fwlib b/src/fwlib new file mode 160000 index 0000000..c1837e3 --- /dev/null +++ b/src/fwlib @@ -0,0 +1 @@ +Subproject commit c1837e3b494b6bca08324f20d051a9d89b58de79 From 1af81b23ab21477f90562207b779515dcfae7b20 Mon Sep 17 00:00:00 2001 From: Lea Tonejca Date: Thu, 3 Aug 2023 11:47:30 +0200 Subject: [PATCH 6/9] keep module name: fwlib --- README.md | 5 +++++ fwlib.i | 2 +- setup.py | 2 +- src/fwlib | 1 - 4 files changed, 7 insertions(+), 3 deletions(-) delete mode 160000 src/fwlib diff --git a/README.md b/README.md index 70bdebf..d607c43 100644 --- a/README.md +++ b/README.md @@ -31,3 +31,8 @@ ln -s extern/fwlib/libfwlib32-linux-x64.so.1.0.5 libfwlib32.so gcc -fPIC -shared fwlib_wrap.c -o _fwlib.so -L. -lpthread -lm -lfwlib32 -I/usr/local/include/python3.9 # compile python module LD_LIBRARY_PATH=. python3.9 test.py # verify module works ``` + +## install apckage +``` +pip3 install -e git+https://github.com/tonejca/pyfwlib.git@pyfanucable#egg=fwlib +``` \ No newline at end of file diff --git a/fwlib.i b/fwlib.i index 1213e62..1bf0083 100644 --- a/fwlib.i +++ b/fwlib.i @@ -1,4 +1,4 @@ -%module pyfwlib +%module fwlib %{ #include "./extern/fwlib/fwlib32.h" typedef struct odbaxdt_t { diff --git a/setup.py b/setup.py index 5eba353..37a2421 100644 --- a/setup.py +++ b/setup.py @@ -22,7 +22,7 @@ os.symlink(fname, os.path.join(fwlib_dir, "libfwlib32.so.1")) setup( - name="pyfwlib", + name="fwlib", version="0.1", description="", ext_modules=[ diff --git a/src/fwlib b/src/fwlib deleted file mode 160000 index c1837e3..0000000 --- a/src/fwlib +++ /dev/null @@ -1 +0,0 @@ -Subproject commit c1837e3b494b6bca08324f20d051a9d89b58de79 From e81e7b2ed2320d45fa0eeae4019fe558774a5490 Mon Sep 17 00:00:00 2001 From: Lea Tonejca Date: Wed, 23 Aug 2023 15:25:21 +0200 Subject: [PATCH 7/9] add automatic package installation to readme --- README.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index d607c43..0d2dd9b 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,13 @@ # fwlib & swig # Instructions -## Dependencies + +## I - Install package automatically +``` +pip3 install -e git+https://github.com/tonejca/pyfwlib.git@pyfanucable#egg=fwlib +``` + +## II - Install library semi-automated get fwlib ``` git submodule update --init --recursive @@ -31,8 +37,3 @@ ln -s extern/fwlib/libfwlib32-linux-x64.so.1.0.5 libfwlib32.so gcc -fPIC -shared fwlib_wrap.c -o _fwlib.so -L. -lpthread -lm -lfwlib32 -I/usr/local/include/python3.9 # compile python module LD_LIBRARY_PATH=. python3.9 test.py # verify module works ``` - -## install apckage -``` -pip3 install -e git+https://github.com/tonejca/pyfwlib.git@pyfanucable#egg=fwlib -``` \ No newline at end of file From b9985e8ce04fe14714271b5b15a3cdba905740b7 Mon Sep 17 00:00:00 2001 From: Lea Tonejca Date: Wed, 23 Aug 2023 15:37:04 +0200 Subject: [PATCH 8/9] WIP: debugging setup.py --- README.md | 2 +- setup.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0d2dd9b..200f7b2 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ ## I - Install package automatically ``` -pip3 install -e git+https://github.com/tonejca/pyfwlib.git@pyfanucable#egg=fwlib +pip3 install -e git+https://github.com/tonejca/pyfwlib.git@swig#egg=fwlib ``` ## II - Install library semi-automated diff --git a/setup.py b/setup.py index 070a67c..61cf9cb 100644 --- a/setup.py +++ b/setup.py @@ -7,6 +7,8 @@ if not os.path.isfile(libpath): plat = "linux" machine = platform.machine() + print("machine") + print(machine) version = "1.0.5" if machine == "x86_64": arch = "x64" From 8c369d3540094de93f928659b93e9237bd935057 Mon Sep 17 00:00:00 2001 From: Lea Tonejca Date: Wed, 23 Aug 2023 15:37:04 +0200 Subject: [PATCH 9/9] WIP: debugging setup.py --- README.md | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0d2dd9b..200f7b2 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ ## I - Install package automatically ``` -pip3 install -e git+https://github.com/tonejca/pyfwlib.git@pyfanucable#egg=fwlib +pip3 install -e git+https://github.com/tonejca/pyfwlib.git@swig#egg=fwlib ``` ## II - Install library semi-automated diff --git a/setup.py b/setup.py index 070a67c..95df744 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ plat = "linux" machine = platform.machine() version = "1.0.5" - if machine == "x86_64": + if machine == "x86_64" or "AMD64": arch = "x64" elif machine == "i386": arch = "x86"