Time for Connection: Building TCP Clients and Servers
Although RAiV was designed to be a stand alone device, you may want to connect it to other devices. In this post, we will describe how you can make both TCP client and server applications by using qCU_Net Python Module from our Python SDK.
During the implementation of qCU_Net module, we have used python's native socket module. So, you can also implement your own custom communication module without using our Python SDK.
Introduction
qCU_Net Python Module was designed for simplicity and flexibility. It uses message prototype with 4 Byte Header and JSON Payload:
For your custom applications, you can convert your Python data structures (like lists and dictionaries) to json string and set it to JSON Payload.
In our Python code examples, we used the following RAiV wired network and PC network interface configurations:
| IP | 192.168.10.55 |
| Netmask | 255.255.255.0 |
| Gateway | 192.168.10.1 |
| IP | 192.168.10.2 |
| Netmask | 255.255.255.0 |
| Gateway | 192.168.10.1 |
RAiV as TCP Client
Prepare & Upload Your Python Code
By using the qCU_Net module, you can send a TCP message with the following lines of code:
import qCU_Net
def main():
print("**** TCP Client ****")
# Define Payload of the TCP message
payload = {
"text": "Hello Net Comm"
}
# Send the Payload (the message header is computed inside the function)
qCU_Net.send_data_to_server("192.168.10.2", 12345, payload)
if __name__ == "__main__":
main()
Copy the code above and save it to the user_main.py file. Then as upload the file to RAiV as described in our previous post.
For the server side python application, use the sample TCP server implementation from our Github Repository and run it on PC.
Live Action
As soon as the checks of the user_main.py file was completed, the python file is run. You can check the Console Output and/or the PC side sample server.
Warning: If there is no TCP server or the there is no network connection to the server, error message of the python interpreter is displayed on the Console Output.
RAiV as TCP Server
Prepare & Upload Your Python Code
Creating TCP server in RAiV also simple with qCU_Net module. The only addition is your custom json_handler. As qCU_Net messages uses json payload, the server should have a Python function to parse the incoming payload.
If no json handler is supplied to the TCP server, by default qCU_Net module prints the incoming json string.
The complete TCP server code including a sample json handler is given below:
# Module for TCP communication
import qCU_Net
# Sample implementation of a json payload handler
def sample_json_handler(client_socket, json_payload, client_address):
print(f"Custom handler called for client {client_address}")
print(f"Received payload: {json_payload}")
def main():
print("**** TCP Server ****")
# Start the TCP server with sample json handler
qCU_Net.start_tcp_server(host='192.168.10.55', port=12345, json_handler=sample_json_handler)
if __name__ == "__main__":
main()
Copy the code above and save it to the user_main.py file. Then upload the file to RAiV as described in our previous post
For the client side python application, we have used the sample TCP client implementation from our Github Repository and run it on PC.
Live Action
As soon as the checks of the user_main.py file was completed, the python file is run. You can check the Console Output for incoming messages.
What is Next?
To capture your first stereo image:
Take the Shot: Capturing Your First Stereo ImageCheck our Python SDK:
RAiV Python SDKCheck our Github Repository For Sample Codes:
Our Github Repository