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