PWM generate

  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-2025 WPC Systems Ltd. All rights reserved.
 17'''
 18
 19## WPC
 20from wpcsys import pywpc
 21
 22## Python
 23import asyncio
 24import sys
 25sys.path.insert(0, 'src/')
 26
 27
 28async def main():
 29    ## Get Python driver version
 30    print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
 31
 32    ## Create device handle
 33    dev = pywpc.USBDAQF1AOD()
 34
 35    ## Connect to device
 36    try:
 37        dev.connect("default")  ## Depend on your device
 38    except Exception as err:
 39        pywpc.printGenericError(err)
 40        ## Release device handle
 41        dev.close()
 42        return
 43
 44    try:
 45        ## Parameters setting
 46        channel = 0  ## Depend on your device
 47        frequency = 100
 48        duty_cycle = 50
 49
 50        ## Get firmware model & version
 51        driver_info = await dev.Sys_getDriverInfo_async()
 52        print(f"Model name: {driver_info[0]}, Firmware version: {driver_info[-1]} ")
 53
 54        ## Open PWM
 55        err = await dev.PWM_open_async(channel)
 56        print(f"PWM_open_async in channel {channel}, status: {err}")
 57
 58        ## Set frequency
 59        err = await dev.PWM_setFrequency_async(channel, frequency)
 60        print(f"PWM_setFrequency_async in channel {channel}, status: {err}")
 61
 62        ## Set duty cycle
 63        err = await dev.PWM_setDutyCycle_async(channel, duty_cycle)
 64        print(f"PWM_setDutyCycle_async in channel {channel}, status: {err}")
 65
 66        ## Start PWM
 67        err = await dev.PWM_start_async(channel)
 68        print(f"PWM_start_async in channel {channel}, status: {err}")
 69
 70        ## Wait for seconds for generating signal
 71        await asyncio.sleep(5)  ## delay [sec]
 72
 73        ## Stop PWM
 74        err = await dev.PWM_stop_async(channel)
 75        print(f"PWM_stop_async in channel {channel}, status: {err}")
 76
 77        ## Close PWM
 78        err = await dev.PWM_close_async(channel)
 79        print(f"PWM_close_async in channel {channel}, status: {err}")
 80    except Exception as err:
 81        pywpc.printGenericError(err)
 82
 83    finally:
 84        ## Disconnect device
 85        dev.disconnect()
 86
 87        ## Release device handle
 88        dev.close()
 89
 90
 91def main_for_spyder(*args):
 92    if asyncio.get_event_loop().is_running():
 93        return asyncio.create_task(main(*args)).result()
 94    else:
 95        return asyncio.run(main(*args))
 96
 97
 98if __name__ == '__main__':
 99    asyncio.run(main())  ## Use terminal
100    # await main()  ## Use Jupyter or IPython(>=7.0)
101    # main_for_spyder()  ## Use Spyder