1'''
2CAN - CAN_write.py with synchronous 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 time
22
23## WPC
24
25from wpcsys import pywpc
26
27def 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 timeout = 3 ## second
48
49 ## Get Firmware model & version
50 driver_info = dev.Sys_getDriverInfo(timeout)
51 print("Model name: " + driver_info[0])
52 print("Firmware version: " + driver_info[-1])
53
54 ## Open CAN
55 err = dev.CAN_open(port, timeout)
56 print(f"CAN_open in port {port}, status: {err}")
57
58 ## Set CAN port and set speed to 0
59 err = dev.CAN_setSpeed(port, speed, timeout)
60 print(f"CAN_setSpeed in port {port}, status: {err}")
61
62 ## Set CAN port and start CAN
63 err = dev.CAN_start(port, timeout)
64 print(f"CAN_start 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 = dev.CAN_write(port, 10, [33, 22, 11, 88, 77, 55, 66, 22], False, False, timeout)
71 print(f"CAN_write in port {port}, status: {err}")
72
73 ## Wait for 1 sec
74 time.sleep(1) ## delay [s]
75
76 ## ID: 20, data less than 8 bytes, Standard & Data
77 err = dev.CAN_write(port, 20, [1, 2, 3], False, False, timeout)
78 print(f"CAN_write in port {port}, status: {err}")
79
80 ## Wait for 1 sec
81 time.sleep(1) ## delay [s]
82
83 ## Set CAN port and stop CAN
84 err = dev.CAN_stop(port, timeout)
85 print(f"CAN_stop in port {port}, status: {err}")
86
87 ## Close CAN
88 err = dev.CAN_close(port, timeout)
89 print(f"CAN_close in port {port}, status: {err}")
90 except Exception as err:
91 pywpc.printGenericError(err)
92
93 ## Disconnect device
94 dev.disconnect()
95
96 ## Release device handle
97 dev.close()
98
99 return
100
101if __name__ == '__main__':
102 main()