1'''
2I2C - I2C_write_read.py with synchronous mode.
3
4This example demonstrates how to communicate with USBDAQF1RD. (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 time
24
25## WPC
26
27from wpcsys import pywpc
28
29def main():
30 ## Get Python driver version
31 print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
32
33 ## Create device handle
34 dev = pywpc.USBDAQF1RD()
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 timeout = 3 ## second
51
52 ## Generate random data
53 import numpy as np
54 word_address = np.random.randint(8) ## Generate a random address
55 value = np.random.randint(256) ## Generate a random value
56
57 '''
58 Take 24C08C for example
59 '''
60
61 ## Get firmware model & version
62 driver_info = dev.Sys_getDriverInfo(timeout)
63 print("Model name: " + driver_info[0])
64 print("Firmware version: " + driver_info[-1])
65
66 '''
67 Open I2C port
68 '''
69
70 ## Open I2C
71 err = dev.I2C_open(port, timeout)
72 print(f"I2C_open in port {port}, status: {err}")
73
74 '''
75 Set I2C parameter
76 '''
77
78 ## Set I2C port and set clock rate to standard mode
79 err = dev.I2C_setClockRate(port, mode, timeout)
80 print(f"I2C_setClockRate in port {port}, status: {err}")
81
82 '''
83 Write data via I2C
84 '''
85
86 ## Write data to device
87 err = dev.I2C_write(port, device_address, [word_address, value], timeout)
88 print(f"I2C_write in port {port}, status: {err}")
89 print(f"write data: 0x{value:02X}")
90 time.sleep(0.05) ## delay [s]
91
92 '''
93 Read data via I2C
94 '''
95
96 ## Write to set pointer for reading
97 err = dev.I2C_write(port, device_address, [word_address], timeout)
98 print(f"I2C_write in port {port}, status: {err}")
99
100 ## Read data from device
101 data = dev.I2C_read(port, device_address, 1, timeout)
102 print(f"read data: 0x{data[0]:02X}")
103
104 '''
105 Close I2C port
106 '''
107
108 ## Close I2C
109 err = dev.I2C_close(port, timeout)
110 print(f"I2C_close in port {port}, status: {err}")
111 except Exception as err:
112 pywpc.printGenericError(err)
113
114 ## Disconnect device
115 dev.disconnect()
116
117 ## Release device handle
118 dev.close()
119
120 return
121
122if __name__ == '__main__':
123 main()