1'''
2CAN - CAN_write.py with asynchronous mode.
3
4This example demonstrates how to write data to another device with CAN interface from USBDAQF1CD.
5
6First, it shows how to open CAN port and configure CAN parameters.
7Second, write bytes to another device.
8Last, stop and close CAN port.
9
10-------------------------------------------------------------------------------------
11Please change correct serial number or IP and port number BEFORE you run example code.
12
13For other examples please check:
14 https://github.com/WPC-Systems-Ltd/WPC_Python_driver_release/tree/main/examples
15See README.md file to get detailed usage of this example.
16
17Copyright (c) 2022-2024 WPC Systems Ltd. All rights reserved.
18'''
19
20## Python
21import asyncio
22
23## WPC
24
25from wpcsys import pywpc
26
27async def main():
28 ## Get python driver version
29 print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
30
31 ## Create device handle
32 dev = pywpc.USBDAQF1CD()
33
34 ## Connect to device
35 try:
36 dev.connect("default") ## Depend on your device
37 except Exception as err:
38 pywpc.printGenericError(err)
39 ## Release device handle
40 dev.close()
41 return
42
43 try:
44 ## Parameters setting
45 port = 1 ## Depend on your device
46 speed = 0 ## 0 = 125 KHz, 1 = 250 kHz, 2 = 500 kHz, 3 = 1 MHz
47
48 ## Get Firmware model & version
49 driver_info = await dev.Sys_getDriverInfo_async()
50 print("Model name: " + driver_info[0])
51 print("Firmware version: " + driver_info[-1])
52
53 ## Open CAN
54 err = await dev.CAN_open_async(port)
55 print(f"CAN_open_async in port {port}, status: {err}")
56
57 ## Set CAN port and set speed to 0
58 err = await dev.CAN_setSpeed_async(port, speed)
59 print(f"CAN_setSpeed_async in port {port}, status: {err}")
60
61 ## Set CAN port and start CAN
62 err = await dev.CAN_start_async(port)
63 print(f"CAN_start_async in port {port}, status: {err}")
64
65 ## CAN_length: True: Extended, False: Standard
66 ## CAN_type: True: Remote, False: Data
67
68 ## ID: 10, data with 8 bytes, Standard & Data
69 err = await dev.CAN_write_async(port, 10, [33, 22, 11, 88, 77, 55, 66, 22], False, False)
70 print(f"CAN_write_async in port {port}, status: {err}")
71
72 ## Wait for 1 sec
73 await asyncio.sleep(1) ## delay [s]
74
75 ## ID: 20, data less than 8 bytes, Standard & Data
76 err = await dev.CAN_write_async(port, 20, [1, 2, 3], False, False)
77 print(f"CAN_write_async in port {port}, status: {err}")
78
79 ## Wait for 1 sec
80 await asyncio.sleep(1) ## delay [s]
81
82 ## Set CAN port and stop CAN
83 err = await dev.CAN_stop_async(port)
84 print(f"CAN_stop_async in port {port}, status: {err}")
85
86 ## Close CAN
87 err = await dev.CAN_close_async(port)
88 print(f"CAN_close_async in port {port}, status: {err}")
89 except Exception as err:
90 pywpc.printGenericError(err)
91
92 ## Disconnect device
93 dev.disconnect()
94
95 ## Release device handle
96 dev.close()
97
98 return
99
100def main_for_spyder(*args):
101 if asyncio.get_event_loop().is_running():
102 return asyncio.create_task(main(*args)).result()
103 else:
104 return asyncio.run(main(*args))
105
106if __name__ == '__main__':
107 asyncio.run(main()) ## Use terminal
108 # await main() ## Use Jupyter or IPython(>=7.0)
109 # main_for_spyder() ## Use Spyder