CASP Python Libraries
This documentation primarily focuses on the usage of functions from 'casp', 'casp_be', 'casp_gpio' and 'casp_udio' namespaces.
Loading...
Searching...
No Matches
casp_udio.py
1"""! This module defines functions to communicate with target hardware (i.e micro-controller board, SBC etc.) that is programmed with either:
2
3- CASP model consisting of communication blocks such as Serial, WiFi or Ethernet blocks that are configured to communicate with host using CASP UDIO protocol
4- (or) User C/C++ code that can communicate with host using CASP UDIO protocol through Serial, WiFi or Ethernet communication.
5
6All functions in this module uses global object \ref _casp_process.casp_gpio_proc from \ref _casp_process.py module.
7
8<b>About CASP UDIO:</b>
9
10CASP UDIO (User Data Input Output) is a simple communication protocol to exchange data with remote device or computer. The data exchange format is basically divided into header and payload. The format of header (4 Byte long) is as follows:
11
12- Byte index 0: Start byte with value 0x74
13- Byte indexes 1 and 2: Payload length (2 byte value)
14- Byte index 3: Checksum of payload bytes (sum of all payload bytes)
15- Byte index 4 and above: Payload bytes. Payload length should be in multiples of 4.
16
17The payload bytes in general can be anything that is understandable between sender and receiver. However, the payload bytes are accessed (read and write) as 4 byte (or 32-bit) integers or floats. As such payload length should be in multiples of 4. This protocol is used mostly to transfer data as a contiguous sequence of 32-bit integers and floats irrespective of whether the underlying data is 32-bit integer or float. i.e. the actual data can of two 16-bit integers or a 64-bit integer or float split into two 32-bit integers at raw byte level.
18
19This protocol is generally used within CASP domain to communicate between host and targets with minimum overheard.
20
21In Python UDI and UDO is always accessed as 32-bit integer or 32-bit float. Even UDI and UDO indexes that are specified in functions such as ReadI(), ReadF(), WriteI() and WriteF() indicate UDI/UDO index and not byte index.
22"""
23
24import importlib
25from _casp_process import casp_gpio_proc
26import casp_utils as utils
27
28def Init():
29 """! Initializes CASP UDIO subprocess. This is the first function to be called before using any other function in this module.
30 """
31 casp_gpio_proc.init()
32
34 """! Opens CASP Python Libraries documentation.
35 """
36 response = casp_proc.write("casp.help.python")
37
38def Close():
39 """! Terminates CASP UDIO subprocess and releases the resources.
40 """
41 casp_gpio_proc.close()
42
43def ConnectSerial(serial_port: str="COM3", udi_bytes: int=16, udo_bytes: int=16, enable_dtr: bool=True, send_recv_delay: int=1, baud: int=115200) -> bool:
44 """! Initializes CASP UDIO connection process with the target over serial port. Performs initial protocol hand shake and keep things ready for data read/write operations.
45
46 User can use \ref SerialPorts() to know available serial ports on the device.
47
48 Arguments:
49
50 serial_port: Set serial port to which device is connected.
51
52 udi_bytes: Set 'User Data Input (UDI)' bytes for receiving data from the target. Default value is 16 bytes.
53
54 udo_bytes: Set 'User Data Output (UDO)' bytes for sending data from the target. Default value is 16 bytes.
55
56 enable_dtr: Set to True when serial data terminal ready (DTR) control signal is to be used. Default value is True. Set to False if device is not detectable (generally on old hardware).
57
58 send_recv_delay: Set send receive time delay in milli seconds. Generally set between 1 to 5 msec. Smaller values reduces latency at the cost of increased CPU utilization and vice versa. Default value is 1 msec.
59
60 baud: Set baud rate in bits per second. Default value is 115200 bps.
61
62 Return Value:
63
64 Returns True if connection is successful. Else returns False.
65 """
66 print(f"CASP: Connecting to serial device...")
67 response = casp_gpio_proc.write("casp.connect.serial", [serial_port, str(baud), str(int(enable_dtr)), str(send_recv_delay), str(udi_bytes), str(udo_bytes)])
68 if len(response) == 0:
69 response = "CASP: Empty response."
70 strs = utils.SplitString(response.strip())
71 print(f"CASP: {strs[0]}")
72 ack:bool = False
73 if len(strs) > 1:
74 ack = bool(strs[1])
75 return ack
76
77def ConnectEth(ip: str, port: int=42001, udi_bytes: int=16, udo_bytes: int=16, tcp: bool=False, send_recv_delay: int=1) -> bool:
78 """! Initializes CASP UDIO connection process with the target over ethernet. Performs initial protocol hand shake and keep things ready for data read/write operations.
79
80 Arguments:
81
82 ip: Set IP address of the target device. Both IP4 and IP6 are supported.
83
84 port: Set port number of the target device. Mostly, better leave it to default.
85
86 udi_bytes: Set 'User Data Input (UDI)' bytes for receiving data from the target. Default value is 16 bytes.
87
88 udo_bytes: Set 'User Data Output (UDO)' bytes for sending data from the target. Default value is 16 bytes.
89
90 tcp: Set to True when TCP protocol is to be used else UDP protocol will be used during communication.
91
92 send_recv_delay: Set send receive time delay in milli seconds. Generally set between 1 to 5 msec. Smaller values reduces latency at the cost of increased CPU utilization and vice versa. Default value is 1 msec.
93
94 Return Value:
95
96 Returns True if connection is successful. Else returns False.
97 """
98 print(f"CASP: Connecting to ethernet device...")
99 response = casp_gpio_proc.write("casp.connect.eth", [ip, str(port), str(int(tcp)), str(send_recv_delay), str(udi_bytes), str(udo_bytes)])
100 if len(response) == 0:
101 response = "CASP: Empty response."
102 strs = utils.SplitString(response.strip())
103 print(f"CASP: {strs[0]}")
104 ack:bool = False
105 if len(strs) > 1:
106 ack = bool(strs[1])
107 return ack
108
110 """! Displays available serial ports on the host. User can use this function before using the \ref ConnectSerial() function.
111 """
112 response = casp_gpio_proc.write("casp.serial.ports")
113 if len(response) == 0:
114 response = "Empty response."
115 print(f"CASP: {response.strip()}")
116
117def ReadI(udi_idx: int) -> int:
118 """! Reads User Data Input (UDI) value of type 32-bit integer from the target and returns the value as an integer.
119
120 Arguments:
121
122 udi_idx: UDI index from which the value is to be read. It should be greater than -1 and less than 'udi_bytes' specified in ConnectSerial() or ConnectEth() functions divided by 4.
123
124 Return Value:
125
126 Returns the UDI value received from the target as an integer.
127 """
128 response = casp_gpio_proc.write("casp.udi_read.i", [str(udi_idx)])
129 if len(response) == 0:
130 response = "Empty response."
131 strs = utils.SplitString(response.strip())
132 if "Error" in strs[0]:
133 print(f"CASP: ReadI({udi_idx}) Failed: {strs[0]}")
134 val: int = 0
135 if len(strs) > 1:
136 val = int(strs[1])
137 return val
138
139def ReadF(udi_idx: int) -> float:
140 """! Reads User Data Input (UDI) value of type 32-bit float from the target and returns the value as an float.
141
142 Arguments:
143
144 udi_idx: UDI index from which the value is to be read. It should be greater than -1 and less than 'udi_bytes' specified in ConnectSerial() or ConnectEth() functions divided by 4.
145
146 Return Value:
147
148 Returns the UDI value received from the target as an float.
149 """
150 response = casp_gpio_proc.write("casp.udi_read.f", [str(udi_idx)])
151 if len(response) == 0:
152 response = "Empty response."
153 strs = utils.SplitString(response.strip())
154 if "Error" in strs[0]:
155 print(f"CASP: ReadF({udi_idx}) Failed: {strs[0]}")
156 val: float = 0
157 if len(strs) > 1:
158 val = float(strs[1])
159 return val
160
161def WriteI(udo_idx: int, value: int) -> bool:
162 """! Writes User Data Output (UDO) value of type 32-bit integer to the target and returns the status of the write operation.
163
164 Arguments:
165
166 udo_idx: UDO index to which the value is to be written. It should be greater than -1 and less than 'udo_bytes' specified in ConnectSerial() or ConnectEth() functions divided by 4.
167
168 value: Value to write.
169
170 Return Value:
171
172 Returns True if write operation is successful else returns False.
173 """
174 response = casp_gpio_proc.write("casp.udo_write.i", [str(udo_idx), str(value)])
175 if len(response) == 0:
176 response = "Empty response."
177 strs = utils.SplitString(response.strip())
178 ack:bool = False
179 if len(strs) > 1:
180 ack = bool(strs[1])
181 if ack == False:
182 print(f"CASP: WriteI({udo_idx}) Failed: {strs[0]}")
183 return ack
184
185def WriteF(udo_idx: int, value: float) -> bool:
186 """! Writes User Data Output (UDO) value of type 32-bit float to the target and returns the status of the write operation.
187
188 Arguments:
189
190 udo_idx: UDO index to which the value is to be written. It should be greater than -1 and less than 'udo_bytes' specified in ConnectSerial() or ConnectEth() functions divided by 4.
191
192 value: Value to write.
193
194 Return Value:
195
196 Returns True if write operation is successful else returns False.
197 """
198 response = casp_gpio_proc.write("casp.udo_write.f", [str(udo_idx), str(value)])
199 if len(response) == 0:
200 response = "Empty response."
201 strs = utils.SplitString(response.strip())
202 ack:bool = False
203 if len(strs) > 1:
204 ack = bool(strs[1])
205 if ack == False:
206 print(f"CASP: WriteF({udo_idx}) Failed: {strs[0]}")
207 return ack
208
int ReadI(int udi_idx)
Reads User Data Input (UDI) value of type 32-bit integer from the target and returns the value as an ...
Definition casp_udio.py:117
SerialPorts()
Displays available serial ports on the host.
Definition casp_udio.py:109
Init()
Initializes CASP UDIO subprocess.
Definition casp_udio.py:28
bool ConnectEth(str ip, int port=42001, int udi_bytes=16, int udo_bytes=16, bool tcp=False, int send_recv_delay=1)
Initializes CASP UDIO connection process with the target over ethernet.
Definition casp_udio.py:77
bool WriteF(int udo_idx, float value)
Writes User Data Output (UDO) value of type 32-bit float to the target and returns the status of the ...
Definition casp_udio.py:185
HelpPython()
Opens CASP Python Libraries documentation.
Definition casp_udio.py:33
float ReadF(int udi_idx)
Reads User Data Input (UDI) value of type 32-bit float from the target and returns the value as an fl...
Definition casp_udio.py:139
bool WriteI(int udo_idx, int value)
Writes User Data Output (UDO) value of type 32-bit integer to the target and returns the status of th...
Definition casp_udio.py:161
bool ConnectSerial(str serial_port="COM3", int udi_bytes=16, int udo_bytes=16, bool enable_dtr=True, int send_recv_delay=1, int baud=115200)
Initializes CASP UDIO connection process with the target over serial port.
Definition casp_udio.py:43
Close()
Terminates CASP UDIO subprocess and releases the resources.
Definition casp_udio.py:38