1"""! This module defines functions to communicate with target hardware (i.e micro-controller board, SBC etc.) that is programmed with CASP GPIO protocol. Both serial and ethernet communication interfaces are supported. All functions in this module uses global object \ref _casp_process.casp_gpio_proc from \ref _casp_process.py module.
5from _casp_process
import casp_gpio_proc
6import casp_utils
as utils
9 """! Initializes CASP GPIO subprocess. This is the first function to be called before using any other function in this module.
14 """! Terminates CASP GPIO subprocess and releases the resources.
16 casp_gpio_proc.close()
18def ConnectSerial(serial_port: str=
"COM3", enable_dtr: bool=
True, send_recv_delay: int=1, baud: int=115200) -> bool:
19 """! Initializes CASP GPIO connection process with the target over serial port. Performs initial protocol hand shake and keep things ready for data read/write operations.
21 User can use \ref SerialPorts() to know available serial ports on the device.
25 serial_port: Set serial port to which device is connected.
27 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).
29 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.
31 baud: Set baud rate in bits per second. Default value is 115200 bps.
35 Returns True if connection is successful. Else returns False.
37 print(f
"CASP: Connecting to serial device...")
38 response = casp_gpio_proc.write(
"casp.connect.serial", [serial_port, str(baud), str(int(enable_dtr)), str(send_recv_delay),
"0",
"0"])
39 if len(response) == 0:
40 response =
"CASP: Empty response."
41 strs = utils.SplitString(response.strip())
42 print(f
"CASP: {strs[0]}")
48def ConnectEth(ip: str, port: int=42001, tcp: bool=
False, send_recv_delay: int=1) -> bool:
49 """! Initializes CASP GPIO connection process with the target over ethernet. Performs initial protocol hand shake and keep things ready for data read/write operations.
53 ip: Set IP address of the target device. Both IP4 and IP6 are supported.
55 port: Set port number of the target device. Mostly, better leave it to default.
57 tcp: Set to True when TCP protocol is to be used else UDP protocol will be used during communication.
59 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.
63 Returns True if connection is successful. Else returns False.
65 print(f
"CASP: Connecting to ethernet device...")
66 response = casp_gpio_proc.write(
"casp.connect.eth", [ip, str(port), str(int(tcp)), str(send_recv_delay),
"0",
"0"])
67 if len(response) == 0:
68 response =
"CASP: Empty response."
69 strs = utils.SplitString(response.strip())
70 print(f
"CASP: {strs[0]}")
77 """! Displays available serial ports on the host. User can use this function before using the \ref ConnectSerial() function.
79 response = casp_gpio_proc.write(
"casp.serial.ports")
80 if len(response) == 0:
81 response =
"Empty response."
82 print(f
"CASP: {response.strip()}")
85 """! Opens 'CASP User Data' directory from user's Documents folder.
87 response = casp_gpio_proc.write(
"casp.dir.user_data")
88 print(f
"CASP: {response.strip()}")
91 """! Opens CASP tutorials directory.
93 response = casp_gpio_proc.write(
"casp.dir.tutorials")
94 print(f
"CASP: {response.strip()}")
97 """! Opens CASP examples directory.
99 response = casp_gpio_proc.write(
"casp.dir.examples")
100 print(f
"CASP: {response.strip()}")
103 """! Opens CASP Python Libraries documentation.
105 response = casp_proc.write(
"casp.help.python")
108 """! Reads target GPIO raw pin value and returns the value as an integer.
112 pin_name: Pin number/name from the selected module in 'casp_gpio_pins' folder from which the value is to be read.
116 Returns the pin value received from the target as an integer.
118 response = casp_gpio_proc.write(
"casp.read", [pin_name])
119 if len(response) == 0:
120 response =
"Empty response."
121 strs = utils.SplitString(response.strip())
128def Write(pin_name: str, value: int) -> bool:
129 """! Writes value to the target's GPIO pin and returns whether the write operation is successful or not.
133 pin_name: Pin number/name from the selected module in 'casp_gpio_pins' folder to which the value is written.
135 value: Value to write.
139 Returns whether the write operation is successful or not.
141 response = casp_gpio_proc.write(
"casp.write", [pin_name, str(value)])
142 if len(response) == 0:
143 response =
"Empty response."
144 strs = utils.SplitString(response.strip())
FolderExamples()
Opens CASP examples directory.
FolderUserData()
Opens 'CASP User Data' directory from user's Documents folder.
SerialPorts()
Displays available serial ports on the host.
HelpPython()
Opens CASP Python Libraries documentation.
FolderTutorials()
Opens CASP tutorials directory.
bool ConnectSerial(str serial_port="COM3", bool enable_dtr=True, int send_recv_delay=1, int baud=115200)
Initializes CASP GPIO connection process with the target over serial port.
bool Write(str pin_name, int value)
Writes value to the target's GPIO pin and returns whether the write operation is successful or not.
Init()
Initializes CASP GPIO subprocess.
Close()
Terminates CASP GPIO subprocess and releases the resources.
int Read(str pin_name)
Reads target GPIO raw pin value and returns the value as an integer.
bool ConnectEth(str ip, int port=42001, bool tcp=False, int send_recv_delay=1)
Initializes CASP GPIO connection process with the target over ethernet.