SD write

 1'''
 2SD - SD_write.py with asynchronous mode.
 3
 4This example demonstrates how to write a message in SD card from WifiDAQE3AOD.
 5
 6-------------------------------------------------------------------------------------
 7Please change correct serial number or IP and port number BEFORE you run example code.
 8
 9For other examples please check:
10    https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
11See README.md file to get detailed usage of this example.
12
13Copyright (c) 2022-2024 WPC Systems Ltd. All rights reserved.
14'''
15
16## Python
17import asyncio
18
19## WPC
20
21from wpcsys import pywpc
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.WifiDAQE3AOD()
29
30    ## Connect to device
31    try:
32        dev.connect("192.168.5.38") ## 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        filename = "WPC_test.txt"
42        write_data = "WPC Systems Ltd"
43        mode = 0 ## 0 : write, 1 : read
44
45        ## Open file in sdcard
46        err = await dev.SD_openFile_async(filename, mode)
47        print(f"SD_openFile_async, status: {err}")
48
49        ## Get sdcard storage
50        storage = await dev.SD_getStorage_async()
51        print(f"SD_getStorage_async: {storage}")
52
53        ## Write data in sdcard
54        err = await dev.SD_writeFile_async(write_data)
55        print(f"SD_writeFile_async, status: {err}")
56
57        ## Close file in sdcard
58        err = await dev.SD_closeFile_async()
59        print(f"SD_closeFile_async, status: {err}")
60    except Exception as err:
61        pywpc.printGenericError(err)
62
63    ## Disconnect network device
64    dev.disconnect()
65
66    ## Release device handle
67    dev.close()
68
69    return
70
71def main_for_spyder(*args):
72    if asyncio.get_event_loop().is_running():
73        return asyncio.create_task(main(*args)).result()
74    else:
75        return asyncio.run(main(*args))
76
77if __name__ == '__main__':
78    asyncio.run(main()) ## Use terminal
79    # await main() ## Use Jupyter or IPython(>=7.0)
80    # main_for_spyder() ## Use Spyder