def test_file_recive(address, timeout): """test if loading a file from the controls works""" lsv2 = pyLSV2.LSV2(address, port=19000, timeout=timeout, safe_mode=True) lsv2.connect() if lsv2.is_itnc(): mdi_path = 'TNC:/$MDI.H' tool_t_path = 'TNC:/TOOL.T' elif lsv2.is_pilot(): mdi_path = 'TNC:/nc_prog/ncps/PGM01.nc' tool_t_path = 'TNC:/table/toolturn.htt' else: mdi_path = 'TNC:/nc_prog/$mdi.h' tool_t_path = 'TNC:/table/tool.t' with tempfile.TemporaryDirectory(suffix=None, prefix='pyLSV2_') as tmp_dir_name: local_mdi_path = Path(tmp_dir_name).joinpath('mdi.h') assert lsv2.recive_file(local_path=str(local_mdi_path), remote_path=mdi_path, binary_mode=False) is True local_tool_table_path = Path(tmp_dir_name).joinpath('tool.t') assert lsv2.recive_file(local_path=str(local_tool_table_path), remote_path=tool_t_path) is True lsv2.disconnect()
def test_login(): lsv2 = pyLSV2.LSV2('192.168.56.101', port=19000, safe_mode=True) lsv2.connect() assert lsv2.login(login=pyLSV2.LSV2.LOGIN_INSPECT) == True assert lsv2.login(login=pyLSV2.LSV2.LOGIN_FILETRANSFER) == True assert lsv2.login(login=pyLSV2.LSV2.LOGIN_DNC) == False lsv2.disconnect()
def test_read_tool_information(address, timeout): """test if reading tool information on iTNC works""" lsv2 = pyLSV2.LSV2(address, port=19000, timeout=timeout, safe_mode=False) lsv2.connect() if lsv2.is_itnc(): assert lsv2.get_spindle_tool_status() is not False lsv2.disconnect()
def test_remote_file_functions(): lsv2 = pyLSV2.LSV2('192.168.56.101', port=19000, safe_mode=True) lsv2.connect() assert lsv2.make_directory('TNC:/nc_prog/pyLSV2_test') == True assert lsv2.change_directory('TNC:/nc_prog/pyLSV2_test') == True assert lsv2.copy_local_file(source_path='TNC:/nc_prog/$mdi.h', target_path='TNC:/nc_prog/pyLSV2_test/') == True assert lsv2.get_file_info('TNC:/nc_prog/pyLSV2_test/$mdi.h') != False assert lsv2.copy_local_file(source_path='TNC:/nc_prog/$mdi.h', target_path='TNC:/nc_prog/pyLSV2_test/aa.h') == True assert lsv2.get_file_info('TNC:/nc_prog/pyLSV2_test/aa.h') != False assert lsv2.copy_local_file(source_path='TNC:/nc_prog/$mdi.h', target_path='mdi.b') == True assert lsv2.get_file_info('TNC:/nc_prog/mdi.b') != False assert lsv2.change_directory(remote_directory='TNC:/nc_prog') == True assert lsv2.copy_local_file(source_path='$mdi.h', target_path='TNC:/nc_prog/pyLSV2_test/bb.h') == True assert lsv2.get_file_info('TNC:/nc_prog/pyLSV2_test/bb.h') != False assert lsv2.move_local_file(source_path='TNC:/nc_prog/pyLSV2_test/bb.h', target_path='TNC:/nc_prog/pyLSV2_test/cc.h') == True assert lsv2.get_file_info('TNC:/nc_prog/pyLSV2_test/cc.h') != False assert lsv2.delete_file('TNC:/nc_prog/pyLSV2_test/cc.h') == True assert lsv2.get_file_info('TNC:/nc_prog/pyLSV2_test/cc.h') == False assert lsv2.delete_file('TNC:/nc_prog/pyLSV2_test/aa.h') == True assert lsv2.delete_file('TNC:/nc_prog/pyLSV2_test/$mdi.h') == True assert lsv2.delete_file('TNC:/nc_prog/mdi.b') == True assert lsv2.change_directory('TNC:/nc_prog') == True assert lsv2.delete_empty_directory('TNC:/nc_prog/pyLSV2_test') == True lsv2.disconnect()
def test_read_error_messages(address, timeout): """test if reading error messages on iTNC works""" lsv2 = pyLSV2.LSV2(address, port=19000, timeout=timeout, safe_mode=False) lsv2.connect() if lsv2.is_itnc(): assert lsv2.get_error_messages() is not False lsv2.disconnect()
def test_login(address, timeout): """test to see if login and safe mode works""" lsv2 = pyLSV2.LSV2(address, port=19000, timeout=timeout, safe_mode=True) lsv2.connect() assert lsv2.login(login=pyLSV2.Login.INSPECT) is True assert lsv2.login(login=pyLSV2.Login.FILETRANSFER) is True assert lsv2.login(login=pyLSV2.Login.DNC) is False lsv2.disconnect()
def test_read_info(): lsv2 = pyLSV2.LSV2('192.168.56.101', port=19000, safe_mode=True) lsv2.connect() assert lsv2.change_directory(remote_directory='TNC:/nc_prog') == True assert lsv2.get_directory_info() != False assert lsv2.get_file_info(remote_file_path='TNC:/nc_prog/$mdi.h') != False assert lsv2.get_directory_content() != False assert lsv2.get_drive_info() != False lsv2.disconnect()
def test_remote_file_functions(address, timeout): """test if functions for manipulating the remote file system work""" lsv2 = pyLSV2.LSV2(address, port=19000, timeout=timeout, safe_mode=True) lsv2.connect() test_dir = 'TNC:/nc_prog/pyLSV2_test/' if lsv2.is_itnc(): mdi_dir = 'TNC:/' mdi_name = '$MDI.H' elif lsv2.is_pilot(): mdi_dir = 'TNC:/nc_prog/ncps/' mdi_name = 'PGM01.nc' else: mdi_dir = 'TNC:/nc_prog/' mdi_name = '$mdi.h' test_file_1 = test_dir + 'aa.h' test_file_2 = test_dir + 'bb.h' test_file_3 = test_dir + 'cc.h' assert lsv2.make_directory(test_dir) is True assert lsv2.change_directory(test_dir) is True if lsv2.is_tnc(): #only test for tnc controls assert lsv2.copy_local_file( source_path=mdi_dir + mdi_name, target_path=test_dir) is True assert lsv2.get_file_info(test_dir + mdi_name) is not False assert lsv2.delete_file(test_dir + mdi_name) is True assert lsv2.copy_local_file( source_path=mdi_dir + mdi_name, target_path=test_file_1) is True assert lsv2.get_file_info(test_file_1) is not False assert lsv2.change_directory(remote_directory=mdi_dir) is True assert lsv2.copy_local_file( source_path=mdi_name, target_path=test_file_2) is True assert lsv2.get_file_info(test_file_2) is not False assert lsv2.move_local_file(source_path=test_file_2, target_path=test_file_3) is True assert lsv2.get_file_info(test_file_3) is not False assert lsv2.delete_file(test_file_3) is True assert lsv2.get_file_info(test_file_3) is False assert lsv2.delete_file(test_file_1) is True assert lsv2.change_directory(mdi_dir) is True assert lsv2.delete_empty_directory(test_dir) is True lsv2.disconnect()
def test_plc_read(address, timeout): """test to see if reading of plc data works""" lsv2 = pyLSV2.LSV2(address, port=19000, timeout=timeout, safe_mode=False) lsv2.connect() assert lsv2.read_plc_memory(address=0, mem_type=pyLSV2.MemoryType.MARKER, count=1) is not False assert lsv2.read_plc_memory(address=0, mem_type=pyLSV2.MemoryType.WORD, count=1) is not False assert lsv2.read_plc_memory(address=0, mem_type=pyLSV2.MemoryType.DWORD, count=1) is not False assert lsv2.read_plc_memory(address=0, mem_type=pyLSV2.MemoryType.STRING, count=1) is not False assert lsv2.read_plc_memory(address=0, mem_type=pyLSV2.MemoryType.INPUT, count=1) is not False assert lsv2.read_plc_memory(address=0, mem_type=pyLSV2.MemoryType.OUTPUT_WORD, count=1) is not False lsv2.disconnect()
def test_read_machine_parameter(address, timeout): """test to see if reading of machine parameters works""" lsv2 = pyLSV2.LSV2(address, port=19000, timeout=timeout, safe_mode=False) lsv2.connect() if lsv2.is_itnc(): # old style assert lsv2.get_machine_parameter('7230.0') is not False else: # new stype assert lsv2.get_machine_parameter( 'CfgDisplayLanguage.ncLanguage') is not False lsv2.disconnect()
def test_folder_functions(): lsv2 = pyLSV2.LSV2('192.168.56.101', port=19000, safe_mode=True) lsv2.connect() assert lsv2.change_directory('TNC:/nc_prog') == True assert lsv2.make_directory('TNC:/nc_prog/pyLSV2_test/T1/T2/T3') == True assert lsv2.delete_empty_directory('TNC:/nc_prog/pyLSV2_test/T1/T2/T3') == True assert lsv2.change_directory('TNC:/nc_prog/pyLSV2_test/T1/T2/T3') == False assert lsv2.change_directory('TNC:/nc_prog/pyLSV2_test/T1/T2') == True assert lsv2.delete_empty_directory('TNC:/nc_prog/pyLSV2_test/T1/T2') == False assert lsv2.change_directory('TNC:/nc_prog') == True assert lsv2.delete_empty_directory('TNC:/nc_prog/pyLSV2_test/T1/T2') == True assert lsv2.delete_empty_directory('TNC:/nc_prog/pyLSV2_test/T1') == True assert lsv2.delete_empty_directory('TNC:/nc_prog/pyLSV2_test') == True lsv2.disconnect()
def test_file_recive(): lsv2 = pyLSV2.LSV2('192.168.56.101', port=19000, safe_mode=True) lsv2.connect() with tempfile.TemporaryDirectory(suffix=None, prefix='pyLSV2_') as tmp_dir_name: local_mdi_path = Path(tmp_dir_name).joinpath('mdi.h') assert lsv2.recive_file(local_path=str(local_mdi_path), remote_path='TNC:/nc_prog/$mdi.h', binary_mode=False) == True local_tool_table_path = Path(tmp_dir_name).joinpath('tool.t') assert lsv2.recive_file(local_path=str(local_tool_table_path), remote_path='TNC:/table/tool.t') == True lsv2.disconnect()
def test_key_press_sim(address, timeout): """test to see if reading of machine parameters works""" lsv2 = pyLSV2.LSV2(address, port=19000, timeout=timeout, safe_mode=False) lsv2.connect() lsv2.login(pyLSV2.Login.MONITOR) assert lsv2.set_keyboard_access(False) is True assert lsv2.send_key_code(pyLSV2.KeyCode.MODE_MANUAL) is True assert lsv2.send_key_code(pyLSV2.KeyCode.MODE_PGM_EDIT) is True assert lsv2.set_keyboard_access(True) is True lsv2.logout(pyLSV2.Login.MONITOR) lsv2.disconnect()
def test_rw_machine_parameter(address, timeout): """test to see if reading and writing of machine parameters works""" lsv2 = pyLSV2.LSV2(address, port=19000, timeout=timeout, safe_mode=False) lsv2.connect() if lsv2.is_itnc(): # old style parameter_name = '7230.0' else: # new stype parameter_name = 'CfgDisplayLanguage.ncLanguage' lsv2.login(pyLSV2.Login.PLCDEBUG) current_value = lsv2.get_machine_parameter(parameter_name) assert lsv2.set_machine_parameter(parameter_name, current_value, safe_to_disk=False) is not False lsv2.logout(pyLSV2.Login.PLCDEBUG) lsv2.disconnect()
def test_data_path_read(address, timeout): """test to see if reading via data path works. run only on iTNC""" lsv2 = pyLSV2.LSV2(address, port=19000, timeout=timeout, safe_mode=False) lsv2.connect() if lsv2.is_itnc(): assert lsv2.read_data_path('/PLC/memory/K/1') is not None assert lsv2.read_data_path('/PLC/memory/M/1') is not None assert lsv2.read_data_path('/PLC/memory/B/1') is not None assert lsv2.read_data_path('/PLC/memory/W/2') is not None assert lsv2.read_data_path('/PLC/memory/D/4') is not None assert lsv2.read_data_path('/PLC/memory/I/1') is not None assert lsv2.read_data_path('/PLC/memory/S/1') is not None assert lsv2.read_data_path('/TABLE/TOOL/T/1/L') is not None # These probably only work on a programming station assert lsv2.read_data_path('/PLC/program/symbol/global/MG_BA_Automatik') is not None assert lsv2.read_data_path('/PLC/program/symbol/module/"SPINDEL.SRC"/KL_100_PROZENT') is not None assert lsv2.read_data_path('/PLC/program/symbol/global/STG_WZM[0].WL_WZM_SIMULATION_ZAEHLE') is not None assert lsv2.read_data_path('/PLC/program/symbol/global/+STG_WZM[1]') is not None lsv2.disconnect()
def test_read_info(address, timeout): """test if reading of file information works""" lsv2 = pyLSV2.LSV2(address, port=19000, timeout=timeout, safe_mode=True) lsv2.connect() if lsv2.is_itnc(): mdi_path = 'TNC:/$MDI.H' elif lsv2.is_pilot(): mdi_path = 'TNC:/nc_prog/ncps/PGM01.nc' else: mdi_path = 'TNC:/nc_prog/$mdi.h' assert lsv2.change_directory(remote_directory='TNC:/nc_prog') is True assert lsv2.get_directory_info() is not False assert lsv2.get_file_info(remote_file_path=mdi_path) is not False assert lsv2.get_directory_content() is not False assert lsv2.get_drive_info() is not False result1 = lsv2.get_file_list(pyLSV2.DriveName.TNC, descend=False) result2 = lsv2.get_file_list(pyLSV2.DriveName.TNC, descend=True) assert (len(result2) > len(result1)) is True lsv2.disconnect()
def test_file_transfer_binary(): lsv2 = pyLSV2.LSV2('192.168.56.101', port=19000, safe_mode=True) lsv2.connect() with tempfile.TemporaryDirectory(suffix=None, prefix='pyLSV2_') as tmp_dir_name: local_send_path = Path('./data/testdata.bmp') local_recive_path = Path(tmp_dir_name).joinpath('test.bmp') remote_path = 'TNC:/' + local_send_path.name assert lsv2.get_file_info(remote_path) == False assert lsv2.send_file(local_path=local_send_path, remote_path=remote_path, override_file=True, binary_mode=True) == True assert lsv2.recive_file(local_path=str(local_recive_path), remote_path=remote_path, override_file=True, binary_mode=True) == True assert lsv2.delete_file(remote_path) == True digests = [] for filename in [local_send_path, local_recive_path]: hasher = hashlib.md5() with open(filename, 'rb') as f: buf = f.read() hasher.update(buf) a = hasher.hexdigest() digests.append(a) print(a) assert digests[0] == digests[1] lsv2.disconnect()
def test_file_transfer_binary(address, timeout): """test if transferring a file in binary mode works""" lsv2 = pyLSV2.LSV2(address, port=19000, timeout=timeout, safe_mode=True) lsv2.connect() with tempfile.TemporaryDirectory(suffix=None, prefix='pyLSV2_') as tmp_dir_name: local_send_path = Path('./data/testdata.bmp') local_recive_path = Path(tmp_dir_name).joinpath('test.bmp') remote_path = pyLSV2.DriveName.TNC + '/' + local_send_path.name assert lsv2.get_file_info(remote_path) is False assert lsv2.send_file(local_path=local_send_path, remote_path=remote_path, override_file=True, binary_mode=True) is True assert lsv2.recive_file(local_path=str(local_recive_path), remote_path=remote_path, override_file=True, binary_mode=True) is True assert lsv2.delete_file(remote_path) is True digests = [] for filename in [local_send_path, local_recive_path]: hasher = hashlib.md5() with open(filename, 'rb') as f_p: buf = f_p.read() hasher.update(buf) h_d = hasher.hexdigest() digests.append(h_d) assert (digests[0] == digests[1]) is True lsv2.disconnect()
def test_directory_functions(address, timeout): """test if functions to change, create and delete directories work""" lsv2 = pyLSV2.LSV2(address, port=19000, timeout=timeout, safe_mode=True) test_dir = 'TNC:/nc_prog/pyLSV2_test/' lsv2.connect() assert lsv2.change_directory('TNC:/nc_prog') is True assert lsv2.make_directory(test_dir + 'T1/T2/T3') is True assert lsv2.delete_empty_directory(test_dir + 'T1/T2/T3') is True assert lsv2.change_directory(test_dir + 'T1/T2/T3') is False assert lsv2.change_directory(test_dir + 'T1/T2') is True if lsv2.is_itnc(): assert lsv2.delete_empty_directory(test_dir + 'T1/T2') is True assert lsv2.change_directory('TNC:/nc_prog') is True else: assert lsv2.delete_empty_directory(test_dir + 'T1/T2') is False assert lsv2.change_directory('TNC:/nc_prog') is True assert lsv2.delete_empty_directory(test_dir + 'T1/T2') is True assert lsv2.delete_empty_directory(test_dir + 'T1') is True assert lsv2.delete_empty_directory(test_dir) is True lsv2.disconnect()
def test_read_versions(): lsv2 = pyLSV2.LSV2('192.168.56.101', port=19000, safe_mode=True) lsv2.connect() assert lsv2.get_versions() != False lsv2.disconnect()
if __name__ == "__main__": address = '192.168.56.101' user_name = 'user' private_key_file = '<path to private key file>' lsv2_port = 19000 print('Connecting to {:s}@{:s}:{:d} via ssh tunnel'.format( user_name, address, lsv2_port)) ssh_forwarder = SSHTunnelForwarder(address, ssh_username=user_name, ssh_pkey=private_key_file, remote_bind_address=('127.0.0.1', lsv2_port)) ssh_forwarder.start() print('SSH tunnel established. local port is {}'.format( ssh_forwarder.local_bind_port)) print('Establish regular LSV2 connection via local port') lsv2 = pyLSV2.LSV2('127.0.0.1', port=ssh_forwarder.local_bind_port, timeout=5, safe_mode=False) lsv2.connect() print('Connected to "{Control}" with NC Software "{NC_Version}"'.format( **lsv2.get_versions())) print('Close Connection') lsv2.disconnect() print('Close SSH tunnel') ssh_forwarder.stop()
from pathlib import Path import pyLSV2 from pyLSV2.const import MemoryType logging.basicConfig(level=logging.WARNING) if __name__ == "__main__": address = '192.168.56.102' if len(sys.argv) > 1: address = sys.argv[1] print('Connecting to {}'.format(address)) con = pyLSV2.LSV2(address, port=19000, timeout=5, safe_mode=False) con.connect() print('Connected to "{Control}" with NC Software "{NC_Version}"'.format(**con.get_versions())) print('Running LSV2 Version {LSV2_Version} with Flags {LSV2_Version_Flags}'.format(**con.get_system_parameter())) print('Drive Info: {}'.format(con.get_drive_info())) print('Current Folder {Path} Free Space: {Free Size} Attrib: {Dir_Attributs}'.format(**con.get_directory_info())) print('PLC Marker: {}'.format(con.read_plc_memory(address=0, mem_type=MemoryType.MARKER, count=15))) print('PLC Word: {}'.format(con.read_plc_memory(address=6, mem_type=MemoryType.WORD, count=10))) print('PLC Double Word: {}'.format(con.read_plc_memory(address=0, mem_type=MemoryType.DWORD, count=10))) print('PLC String: {}'.format(con.read_plc_memory(address=2, mem_type=MemoryType.STRING, count=2))) print('PLC Input: {}'.format(con.read_plc_memory(address=0, mem_type=MemoryType.INPUT, count=5))) print('PLC Word Output: {}'.format(con.read_plc_memory(address=10, mem_type=MemoryType.OUTPUT_WORD, count=5)))
def test_read_execution_state(address, timeout): """test if reading the executions state works""" lsv2 = pyLSV2.LSV2(address, port=19000, timeout=timeout, safe_mode=False) lsv2.connect() assert lsv2.get_execution_status() in [0, 1, 2, 3, 4, 5] lsv2.disconnect()
def test_read_override_information(address, timeout): """test if reading override values works""" lsv2 = pyLSV2.LSV2(address, port=19000, timeout=timeout, safe_mode=False) lsv2.connect() assert lsv2.get_override_info() is not False lsv2.disconnect()
def test_read_pgm_status(address, timeout): """test if reading the status of the selected program works""" lsv2 = pyLSV2.LSV2(address, port=19000, timeout=timeout, safe_mode=False) lsv2.connect() assert lsv2.get_program_status() is not False lsv2.disconnect()
else: dest_path = Path(args.destination) logging.info('Destination path %s is local', dest_path.resolve()) if use_ssh: import sshtunnel ssh_forwarder = sshtunnel.SSHTunnelForwarder( host_machine, remote_bind_address=("127.0.0.1", host_port)) ssh_forwarder.start() host_machine = "127.0.0.1" host_port = ssh_forwarder.local_bind_port logging.info('SSH tunnel established. local port is %d', host_port) try: con = pyLSV2.LSV2(hostname=host_machine, port=host_port, timeout=args.timeout) con.connect() except socket.gaierror as ex: logging.error('An Exception occurred: "%s"', ex) logging.error('Could not resove host information: "%s"', host_machine) sys.exit(-2) if source_is_remote: file_info = con.get_file_info(remote_file_path=source_path) if not file_info: logging.error('source file dose not exist on remote: "%s"', source_path) sys.exit(-3) elif file_info['is_directory']: logging.error('source on remote is not file but directory: "%s"',
byte_1, byte_2, = struct.unpack('!BB', content) error_text = lsv2.get_error_text(byte_1, byte_2) else: error_text = 'Unknown Error Number {}'.format(content) else: error_text = 'NONE' return 'sent {} payload {} received {} message_length {} content {} error text : {}'.format( command_string, payload, response, response_length, content, error_text) if __name__ == '__main__': logging.basicConfig(format='%(levelname)s : %(message)s', level=logging.ERROR) lsv2 = pyLSV2.LSV2('192.168.56.103', safe_mode=False) lsv2.connect() lsv2.login(login=pyLSV2.LOGIN_INSPECT) lsv2.login(login=pyLSV2.LOGIN_DNC) lsv2.login(login=pyLSV2.LOGIN_INSPECT) lsv2.login(login=pyLSV2.LOGIN_DIAG) lsv2.login(login=pyLSV2.LOGIN_PLCDEBUG) lsv2.login(login=pyLSV2.LOGIN_FILETRANSFER) lsv2.login(login=pyLSV2.LOGIN_MONITOR) lsv2.login(login=pyLSV2.LOGIN_DSP) lsv2.login(login=pyLSV2.LOGIN_DNC) lsv2.login(login=pyLSV2.LOGIN_SCOPE) lsv2.login(login=pyLSV2.LOGIN_FILEPLC, password='******') # base_string = 'A_'
def test_read_versions(address, timeout): """test if login and basic query works""" lsv2 = pyLSV2.LSV2(address, port=19000, timeout=timeout, safe_mode=True) lsv2.connect() assert lsv2.get_versions() is not False lsv2.disconnect()