Motion get logical position

 1'''
 2Motion - Motion_get_logical_position.py with asynchronous mode.
 3
 4-------------------------------------------------------------------------------------
 5Please change correct serial number or IP and port number BEFORE you run example code.
 6
 7For other examples please check:
 8    https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
 9See README.md file to get detailed usage of this example.
10
11Copyright (c) 2022-2025 WPC Systems Ltd. All rights reserved.
12'''
13
14## WPC
15from wpcsys import pywpc
16
17## Python
18import asyncio
19import sys
20sys.path.insert(0, 'src/')
21
22
23async def main():
24    ## Get Python driver version
25    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
26
27    ## Create device handle
28    dev = pywpc.EMotion()
29
30    ## Connect to device
31    try:
32        dev.connect("192.168.1.110")  ## Depend on your device
33    except Exception as err:
34        pywpc.printGenericError(err)
35        ## Release device handle
36        dev.close()
37        return
38
39    try:
40        ## Parameters setting
41        port = 0  ## Depend on your device
42        axis = 0
43        stop_decel = 0
44
45        ## Get firmware model & version
46        driver_info = await dev.Sys_getDriverInfo_async()
47        print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
48
49        ## Motion open
50        err = await dev.Motion_open_async(port)
51        print(f"open_async in port {port}, status: {err}")
52
53        for i in range(100):
54            err = await dev.Motion_setLogicalPosi_async(port, axis, i)
55            if err != 0:
56                print("setLogicalPosi_async ", err)
57            posi = await dev.Motion_getLogicalPosi_async(port, axis)
58            print("getLogicalPosi ", posi)
59
60        ## Motion close
61        err = await dev.Motion_close_async(port)
62        print(f"close_async in port {port}, status: {err}")
63    except Exception as err:
64        pywpc.printGenericError(err)
65
66    finally:
67        ## Disconnect device
68        dev.disconnect()
69
70        ## Release device handle
71        dev.close()
72
73
74def main_for_spyder(*args):
75    if asyncio.get_event_loop().is_running():
76        return asyncio.create_task(main(*args)).result()
77    else:
78        return asyncio.run(main(*args))
79
80
81if __name__ == '__main__':
82    asyncio.run(main())  ## Use terminal
83    # await main()  ## Use Jupyter or IPython(>=7.0)
84    # main_for_spyder()  ## Use Spyder