Package: pyLSV2
pyLSV2This library is an attempt to implement the LSV2 communication protocol used by certain
CNC controls. It's main goal is to transfer file between the application and the control as well
as collect information about said files. Over time more and more functions where added which
support gathering information from the control.Most of this library is based on the work of tfischer73 and his Eclipse plugin found athis GitHub page. Since there is no free documentation beside the plugin, some parts are based purely on reverse engineering and might therefore be not correct.As long as no encrypted communication is necessary and you are using Python 3.5 or newer, no additional librarys are necessary.Please consider the dangers of using this library on a production machine! This library is by no means complete and could damage the control or cause injuries! Everything beyond simple file manipulation is blocked by a lockout parameter. Use at your own risk!LicenseMIT LicenseCopyright (c) 2020 - 2023 drunsinnPermission 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.News and Releasescheckthe github release pagefor information on the latest updatesContributorsIn chronological order:tfischer73drunsinnWouterElfrinkkekec14Michal NavrátilPandaRoux8sazimamanusolveNeeXooBaptou88Md-aliy7npalmerDNXAndreas-strgUsageSeelsv2_demo.pyfor a demonstration of some of the functions.Since the whole protocol isn't documented there can always be problems with certain corner cases. Especially during file transfer a lot of stuff can go wrong.
In case the control doesn't accept a command it returns an error. Some of these errors are checked internally but not everything is covered as of now. It is therefore
best to check the last error withcon.last_error. Every error consists of type and a status code. The enumLSV2StatusCodecontains all known status codes.Notes for upgrade to v1.xxNotice: The change from 0.xx to 1.xx brought some major incompatible changes in regards to the API:raise the minimal required python version to 3.4 (forIronPython 3.4), future releases (1.1.x) will target 3.7 or higher!change of function names and parameters to better reflect their usechange of return types from dict to data classes to reduce the dependency on magic strings
These changes where made intentionally to make further development easier. See the demo script for an overview of the new API.exemplary overview of the changes from v0.x to v1.xFunctionalityVersion 0.xVersion 1.xread nc software versioncon.get_versions()["NC_Version"]con.versions.nc_swcheck if control is a iTNCcon.is_itnc()con.version.is_itnc()get execution status viacon.get_execution_status()returnsintvaluecon.execution_status()returns enumExecStateread override values viacon.get_override_info()returnsdictorFalsecon.override_info()returnsOverrideStateorNoneread axes position viacon.get_axes_location()returnsdictorFalsecon.axes_location()returnsdictorNonemove a file on the controlcon.move_local_file()con.move_file()Basic example without context managerimport pyLSV2
 con = pyLSV2.LSV2("192.168.56.101")
 con.connect()
 print(con.versions.control)
 con.disconnect()Basic example with context managerimport pyLSV2
 with pyLSV2.LSV2("192.168.56.101") as con:
 ... con.connect()
 ... print(con.versions.control)Accessing PLC dataTo read values from the PLC memory you need to know the memory area/type and the memory address. There are two ways to read these values.Reading via memory addressThe following command reads 15 marker (bits) starting at address 32. This returns a list with 15 boolean values.con.read_plc_memory(32, pyLSV2.MemoryType.MARKER, 15)Seelsv2_demo.pyfor more examples.The available memory areas and their python data typeMemory TypePython TypePLC_MEM_TYPE_MARKERboolPLC_MEM_TYPE_INPUTboolPLC_MEM_TYPE_OUTPUTboolPLC_MEM_TYPE_COUNTERboolPLC_MEM_TYPE_TIMERboolPLC_MEM_TYPE_BYTEintegerPLC_MEM_TYPE_WORDintegerPLC_MEM_TYPE_DWORDintegerPLC_MEM_TYPE_STRINGstrPLC_MEM_TYPE_INPUT_WORDintegerPLC_MEM_TYPE_OUTPUT_WORDintegerReading the values from the memory address takes the size of each memory type into account.Reading via Data PathThe following command reads values from the control not via a memory address but via supplying a data access path. This will only work on iTNC controls!
The advantage is that it also allows you to access tables like the tool table without reading the complete file.con.read_data_path('/PLC/memory/K/1')
 con.read_data_path('/TABLE/TOOL/T/1/DOC')Seelsv2_demo.pyfor more examples.Note that reading values from memory does not take into account the actual size in the control memory. This leads to an offset between the values read withread_data_pathandread_plc_memory. As a workaround you have to multiply the address value with the number of bytes the data type requires. The following example tries to show how this can be accomplished:for mem_address in [0, 1, 2, 4, 8, 12, 68, 69, 151, 300, 368]:
    v1 = lsv2.read_plc_memory(mem_address, pyLSV2.MemoryType.DWORD, 1)[0]
    v2 = lsv2.read_data_path("/PLC/memory/D/%d" % (mem_address * 4))
    assert v1 == v2SSH TunnelNewer controls allow the use of ssh to encrypt the communication via LSV2.
Seessh_tunnel_demo.pyfor an example on
how to use the python librarysshtunnelto achieve a secure connection.CompatibilitySince there are a lot of different software versions and machine configurations out there
it is hard to say if this library is compatible with all of them. Most testing has been done
on programming stations but also with real hardware. Here is a list of versions that have
been tested:Programming StationsControlSoftwareTNC640340595 08 SP1TNC640340595 10 SP2TNC640340595 11 SP1TNC640340595 11 SP4iTNC530606425 04 SP20iTNC530340494 08 SP2CNCpilot6401230521 03 SP1TNC7817625 17MachinesControlSoftwareTNC620817605 04 SP1TNC640340595 08 SP1iTNC530340480 14 SP4iTNC530606420 02 SP14iTNC530606420 02 SP3If you have tested it on one of your machines with a different software version, please let us know!TablesIncluded in this library is also functionality to work with Tables used by different NC Controls. This includes for example TNC controls as well as Anilam 6000i CNC. As these controls and there software versions use different table formats, it is also possible to dreive the format form an existing table and export the format to a json file.Seetab2csv.pyfor a demonstration on how to read a table and convert it to a csv file.This script can also be used as a command line toolusage: tab2csv.py [-h] [--decimal_char DECIMAL_CHAR] [-d | -v] source
 
 command line script parsing table files
 
 positional arguments:
   source                table file to parse
 
 options:
   -h, --help            show this help message and exit
   --decimal_char DECIMAL_CHAR
                         override local decimal char
   -d, --debug           enable log level DEBUG
   -v, --verbose         enable log level INFOTestingTo run the test you either need a machine or a programming station. The control has to be on and the
PLC program has to be running. The IP address and timeout are set via command line parameters.pytest --address=192.168.56.103 --timeout=5Minimum required Python versionThe minimum required python version was checked withvermin.vermin --no-parse-comments .The results indicate that pyLSV2 should work with python 3.5 and even with 3.4 if you install
the packported modules argparse, enum and typing. While argpares is only used in the demo script
the other two are necessary. Therefore it should be possible to use pyLSV2 with the current version
ofIronPythonif you install these two modules.Resourcesprotocol.rsthttps://www.inventcom.net/support/heidenhain/read-tnc-plc-datahttps://www.inventcom.net/s1/_pdf/Heidenhain_TNC_Machine_Data.pdfhttps://www.yumpu.com/en/document/read/18882603/-f-heidenhain