1"""! This module defines functions to communicate with target hardware (i.e micro-controller board, SBC etc.) that is programmed with either:
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.
6All functions in this module uses global object \ref _casp_process.casp_gpio_proc from \ref _casp_process.py module.
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:
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.
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.
19This protocol is generally used within CASP domain to communicate between host and targets with minimum overheard.
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.
25from _casp_process
import casp_gpio_proc
26import casp_utils
as utils
29 """! Initializes CASP UDIO subprocess. This is the first function to be called before using any other function in this module.
34 """! Opens CASP Python Libraries documentation.
36 response = casp_proc.write(
"casp.help.python")
39 """! Terminates CASP UDIO subprocess and releases the resources.
41 casp_gpio_proc.close()
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.
46 User can use \ref SerialPorts() to know available serial ports on the device.
50 serial_port: Set serial port to which device is connected.
52 udi_bytes: Set 'User Data Input (UDI)' bytes for receiving data from the target. Default value is 16 bytes.
54 udo_bytes: Set 'User Data Output (UDO)' bytes for sending data from the target. Default value is 16 bytes.
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).
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.
60 baud: Set baud rate in bits per second. Default value is 115200 bps.
64 Returns True if connection is successful. Else returns False.
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]}")
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.
82 ip: Set IP address of the target device. Both IP4 and IP6 are supported.
84 port: Set port number of the target device. Mostly, better leave it to default.
86 udi_bytes: Set 'User Data Input (UDI)' bytes for receiving data from the target. Default value is 16 bytes.
88 udo_bytes: Set 'User Data Output (UDO)' bytes for sending data from the target. Default value is 16 bytes.
90 tcp: Set to True when TCP protocol is to be used else UDP protocol will be used during communication.
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.
96 Returns True if connection is successful. Else returns False.
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]}")
110 """! Displays available serial ports on the host. User can use this function before using the \ref ConnectSerial() function.
112 response = casp_gpio_proc.write(
"casp.serial.ports")
113 if len(response) == 0:
114 response =
"Empty response."
115 print(f
"CASP: {response.strip()}")
118 """! Reads User Data Input (UDI) value of type 32-bit integer from the target and returns the value as an integer.
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.
126 Returns the UDI value received from the target as an integer.
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]}")
140 """! Reads User Data Input (UDI) value of type 32-bit float from the target and returns the value as an float.
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.
148 Returns the UDI value received from the target as an float.
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]}")
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.
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.
168 value: Value to write.
172 Returns True if write operation is successful else returns False.
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())
182 print(f
"CASP: WriteI({udo_idx}) Failed: {strs[0]}")
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.
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.
192 value: Value to write.
196 Returns True if write operation is successful else returns False.
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())
206 print(f
"CASP: WriteF({udo_idx}) Failed: {strs[0]}")
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 ...
SerialPorts()
Displays available serial ports on the host.
Init()
Initializes CASP UDIO subprocess.
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.
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 ...
HelpPython()
Opens CASP Python Libraries documentation.
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...
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...
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.
Close()
Terminates CASP UDIO subprocess and releases the resources.