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-2024 WPC Systems Ltd. All rights reserved.
13'''
14
15## Python
16import asyncio
17
18## WPC
19
20from wpcsys import pywpc
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.EthanP()
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        channel = 0
43        resistance_ratio = 0.5
44
45        ## Get firmware model & version
46        driver_info = await dev.Sys_getDriverInfo_async()
47        print("Model name: " + driver_info[0])
48        print("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    ## Disconnect device
66    dev.disconnect()
67
68    ## Release device handle
69    dev.close()
70
71    return
72def main_for_spyder(*args):
73    if asyncio.get_event_loop().is_running():
74        return asyncio.create_task(main(*args)).result()
75    else:
76        return asyncio.run(main(*args))
77if __name__ == '__main__':
78    asyncio.run(main()) ## Use terminal
79    # await main() ## Use Jupyter or IPython(>=7.0)
80    # main_for_spyder() ## Use Spyder