1'''
2PWM - PWM_generate.py with asynchronous mode.
3
4This example demonstrates how to generate PWM with USBDAQF1AOD.
5
6First, you should set frequency and duty cycle so that it can generate proper signal.
7By the way, if you want to check PWM signal, you could connect DI pin with PWM pin.
8
9-------------------------------------------------------------------------------------
10Please change correct serial number or IP and port number BEFORE you run example code.
11
12For other examples please check:
13 https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
14See README.md file to get detailed usage of this example.
15
16Copyright (c) 2022-2024 WPC Systems Ltd. All rights reserved.
17'''
18
19## Python
20import asyncio
21
22## WPC
23
24from wpcsys import pywpc
25
26async def main():
27 ## Get Python driver version
28 print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
29
30 ## Create device handle
31 dev = pywpc.USBDAQF1AOD()
32
33 ## Connect to device
34 try:
35 dev.connect("default") ## Depend on your device
36 except Exception as err:
37 pywpc.printGenericError(err)
38 ## Release device handle
39 dev.close()
40 return
41
42 try:
43 ## Parameters setting
44 channel = 0 ## Depend on your device
45 frequency = 100
46 duty_cycle = 50
47
48 ## Get firmware model & version
49 driver_info = await dev.Sys_getDriverInfo_async()
50 print("Model name: " + driver_info[0])
51 print("Firmware version: " + driver_info[-1])
52
53 ## Open PWM
54 err = await dev.PWM_open_async(channel)
55 print(f"PWM_open_async in channel {channel}, status: {err}")
56
57 ## Set frequency
58 err = await dev.PWM_setFrequency_async(channel, frequency)
59 print(f"PWM_setFrequency_async in channel {channel}, status: {err}")
60
61 ## Set duty cycle
62 err = await dev.PWM_setDutyCycle_async(channel, duty_cycle)
63 print(f"PWM_setDutyCycle_async in channel {channel}, status: {err}")
64
65 ## Start PWM
66 err = await dev.PWM_start_async(channel)
67 print(f"PWM_start_async in channel {channel}, status: {err}")
68
69 ## Wait for seconds for generating signal
70 await asyncio.sleep(5) ## delay [s]
71
72 ## Stop PWM
73 err = await dev.PWM_stop_async(channel)
74 print(f"PWM_stop_async in channel {channel}, status: {err}")
75
76 ## Close PWM
77 err = await dev.PWM_close_async(channel)
78 print(f"PWM_close_async in channel {channel}, status: {err}")
79 except Exception as err:
80 pywpc.printGenericError(err)
81
82 ## Disconnect device
83 dev.disconnect()
84
85 ## Release device handle
86 dev.close()
87
88 return
89
90def main_for_spyder(*args):
91 if asyncio.get_event_loop().is_running():
92 return asyncio.create_task(main(*args)).result()
93 else:
94 return asyncio.run(main(*args))
95
96if __name__ == '__main__':
97 asyncio.run(main()) ## Use terminal
98 # await main() ## Use Jupyter or IPython(>=7.0)
99 # main_for_spyder() ## Use Spyder