PWM generate

 1'''
 2PWM - PWM_generate.py with synchronous mode.
 3
 4This example demonstrates how to generate PWM with USBDAQF1RD.
 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 time
21
22## WPC
23
24from wpcsys import pywpc
25
26def main():
27    ## Get Python driver version
28    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
29
30    ## Create device handle
31    dev = pywpc.USBDAQF1RD()
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        timeout = 3 ## second
48
49        ## Get firmware model & version
50        driver_info = dev.Sys_getDriverInfo(timeout)
51        print("Model name: " + driver_info[0])
52        print("Firmware version: " + driver_info[-1])
53
54        ## Open PWM
55        err = dev.PWM_open(channel, timeout)
56        print(f"PWM_open in channel {channel}, status: {err}")
57
58        ## Set frequency
59        err = dev.PWM_setFrequency(channel, frequency, timeout)
60        print(f"PWM_setFrequency in channel {channel}, status: {err}")
61
62        ## Set duty cycle
63        err = dev.PWM_setDutyCycle(channel, duty_cycle, timeout)
64        print(f"PWM_setDutyCycle in channel {channel}, status: {err}")
65
66        ## Start PWM
67        err = dev.PWM_start(channel, timeout)
68        print(f"PWM_start in channel {channel}, status: {err}")
69
70        ## Wait for seconds for generating signal
71        time.sleep(5) ## delay [s]
72
73        ## Stop PWM
74        err = dev.PWM_stop(channel, timeout)
75        print(f"PWM_stop in channel {channel}, status: {err}")
76
77        ## Close PWM
78        err = dev.PWM_close(channel, timeout)
79        print(f"PWM_close in channel {channel}, status: {err}")
80    except Exception as err:
81        pywpc.printGenericError(err)
82
83    ## Disconnect device
84    dev.disconnect()
85
86    ## Release device handle
87    dev.close()
88
89    return
90
91if __name__ == '__main__':
92    main()