1'''
2I2C - I2C_write_read.py with asynchronous mode.
3
4This example demonstrates how to communicate with USBDAQF1CD. (master) and 24C08C (slave) with I2C interface.
5
6First, it shows how to open I2C port and configure I2C parameters.
7Second, write some bytes with address into EEPROM (24C08C). We have to make sure that bytes written in address is correct however read address from EEPROM (24C08C).
8Last, close I2C port
9
10The sensor used in this example is a 24C08C expecially for Two-wore Serial EEPROM.
11
12-------------------------------------------------------------------------------------
13Please change correct serial number or IP and port number BEFORE you run example code.
14
15For other examples please check:
16 https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
17See README.md file to get detailed usage of this example.
18
19Copyright (c) 2022-2024 WPC Systems Ltd. All rights reserved.
20'''
21
22## Python
23import asyncio
24
25## WPC
26
27from wpcsys import pywpc
28
29async def main():
30 ## Get Python driver version
31 print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
32
33 ## Create device handle
34 dev = pywpc.USBDAQF1CD()
35
36 ## Connect to device
37 try:
38 dev.connect("default") ## Depend on your device
39 except Exception as err:
40 pywpc.printGenericError(err)
41 ## Release device handle
42 dev.close()
43 return
44
45 try:
46 ## Parameters setting
47 port = 1 ## Depend on your device
48 mode = 0
49 device_address = 0x50 ## 01010000
50
51 ## Generate random data
52 import numpy as np
53 word_address = np.random.randint(8) ## Generate a random address
54 value = np.random.randint(256) ## Generate a random value
55
56 '''
57 Take 24C08C for example
58 '''
59
60 ## Get firmware model & version
61 driver_info = await dev.Sys_getDriverInfo_async()
62 print("Model name: " + driver_info[0])
63 print("Firmware version: " + driver_info[-1])
64
65 '''
66 Open I2C port
67 '''
68
69 ## Open I2C
70 err = await dev.I2C_open_async(port)
71 print(f"I2C_open_async in port {port}, status: {err}")
72
73 '''
74 Set I2C parameter
75 '''
76
77 ## Set I2C port and set clock rate to standard mode
78 err = await dev.I2C_setClockRate_async(port, mode)
79 print(f"I2C_setClockRate_async in port {port}, status: {err}")
80
81 '''
82 Write data via I2C
83 '''
84
85 ## Write WREN byte
86 err = await dev.I2C_write_async(port, device_address, [word_address, value])
87 print(f"I2C_write_async in port {port}, status: {err}")
88 print(f"write data: 0x{value:02X}")
89
90 '''
91 Read data via I2C
92 '''
93
94 err = await dev.I2C_write_async(port, device_address, [word_address])
95 print(f"I2C_write_async in port {port}, status: {err}")
96
97 data = await dev.I2C_read_async(port, device_address, 1)
98 print(f"read data: 0x{data[0]:02X}")
99
100 '''
101 Close I2C port
102 '''
103
104 ## Close I2C
105 err = await dev.I2C_close_async(port)
106 print(f"I2C_close_async in port {port}, status: {err}")
107 except Exception as err:
108 pywpc.printGenericError(err)
109
110 ## Disconnect device
111 dev.disconnect()
112
113 ## Release device handle
114 dev.close()
115
116 return
117
118def main_for_spyder(*args):
119 if asyncio.get_event_loop().is_running():
120 return asyncio.create_task(main(*args)).result()
121 else:
122 return asyncio.run(main(*args))
123
124if __name__ == '__main__':
125 asyncio.run(main()) ## Use terminal
126 # await main() ## Use Jupyter or IPython(>=7.0)
127 # main_for_spyder() ## Use Spyder