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-2025 WPC Systems Ltd. All rights reserved.
18'''
19
20## WPC
21from wpcsys import pywpc
22
23## Python
24import asyncio
25import sys
26sys.path.insert(0, 'src/')
27
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 speed = 0 ## 0: 125 KHz, 1: 250 kHz, 2: 500 kHz, 3: 1 MHz
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 CAN
55 err = await dev.CAN_open_async(port)
56 print(f"CAN_open_async in port {port}, status: {err}")
57
58 ## Set CAN port and set speed to 0
59 err = await dev.CAN_setSpeed_async(port, speed)
60 print(f"CAN_setSpeed_async in port {port}, status: {err}")
61
62 ## Set CAN port and start CAN
63 err = await dev.CAN_start_async(port)
64 print(f"CAN_start_async in port {port}, status: {err}")
65
66 ## CAN_length: True: Extended, False: Standard
67 ## CAN_type: True: Remote, False: Data
68
69 ## ID: 10, data with 8 bytes, Standard & Data
70 err = await dev.CAN_write_async(port, 10, [33, 22, 11, 88, 77, 55, 66, 22], False, False)
71 print(f"CAN_write_async in port {port}, status: {err}")
72
73 ## Wait for 1 sec
74 await asyncio.sleep(1) ## delay [sec]
75
76 ## ID: 20, data less than 8 bytes, Standard & Data
77 err = await dev.CAN_write_async(port, 20, [1, 2, 3], False, False)
78 print(f"CAN_write_async in port {port}, status: {err}")
79
80 ## Wait for 1 sec
81 await asyncio.sleep(1) ## delay [sec]
82
83 ## Set CAN port and stop CAN
84 err = await dev.CAN_stop_async(port)
85 print(f"CAN_stop_async in port {port}, status: {err}")
86
87 ## Close CAN
88 err = await dev.CAN_close_async(port)
89 print(f"CAN_close_async in port {port}, status: {err}")
90 except Exception as err:
91 pywpc.printGenericError(err)
92
93 finally:
94 ## Disconnect device
95 dev.disconnect()
96
97 ## Release device handle
98 dev.close()
99
100
101def main_for_spyder(*args):
102 if asyncio.get_event_loop().is_running():
103 return asyncio.create_task(main(*args)).result()
104 else:
105 return asyncio.run(main(*args))
106
107
108if __name__ == '__main__':
109 asyncio.run(main()) ## Use terminal
110 # await main() ## Use Jupyter or IPython(>=7.0)
111 # main_for_spyder() ## Use Spyder