DPOT write by channel

 1'''
 2DPOT - DPOT_writeByChannel.py with asynchronous mode.
 3
 4This example demonstrates how to write digital potentiometer resistance to the specefic 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-2025 WPC Systems Ltd. All rights reserved.
13'''
14
15## WPC
16from wpcsys import pywpc
17
18## Python
19import asyncio
20import sys
21sys.path.insert(0, 'src/')
22
23
24async def main():
25    ## Get Python driver version
26    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
27
28    ## Create device handle
29    dev = pywpc.EthanP()
30
31    ## Connect to device
32    try:
33        dev.connect("192.168.1.110")  ## Depend on your device
34    except Exception as err:
35        pywpc.printGenericError(err)
36        ## Release device handle
37        dev.close()
38        return
39
40    try:
41        ## Parameters setting
42        port = 0  ## Depend on your device
43        channel = 0
44        resistance_ratio = 0.5
45
46        ## Get firmware model & version
47        driver_info = await dev.Sys_getDriverInfo_async()
48        print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
49
50        ## Open DPOT
51        err = await dev.DPOT_open_async(port)
52        print(f"DPOT_open_async in port {port}, status: {err}")
53
54        ## Write on the channel
55        err = await dev.DPOT_writeByChannel_async(port, channel, resistance_ratio)
56        print(f"DPOT_writeByChannel_async in port {port} channel {channel}, status: {err}")
57
58        ## Close DPOT
59        err = await dev.DPOT_close_async(port)
60        print(f"DPOT_close_async in port {port}, status: {err}")
61
62    except Exception as err:
63        pywpc.printGenericError(err)
64
65    finally:
66        ## Disconnect device
67        dev.disconnect()
68
69        ## Release device handle
70        dev.close()
71
72
73def main_for_spyder(*args):
74    if asyncio.get_event_loop().is_running():
75        return asyncio.create_task(main(*args)).result()
76    else:
77        return asyncio.run(main(*args))
78
79
80if __name__ == '__main__':
81    asyncio.run(main())  ## Use terminal
82    # await main()  ## Use Jupyter or IPython(>=7.0)
83    # main_for_spyder()  ## Use Spyder