Vision Code Examples
Short Description
This page provides small Python code examples for communicating with the Vision Starter Kit over TCP/IP.
The examples are intended as starting points for your own applications.
They are not complete projects, but reusable building blocks for connecting to the sensor, receiving data and sending simple commands.
Code Example Overview
| Example | Purpose | Difficulty |
|---|---|---|
| Connect to the Vision Sensor | Establish a TCP connection to the sensor | Basic |
| Receive Sensor Messages | Continuously read incoming messages | Basic |
| Trigger Image Acquisition | Send trigger commands to the sensor | Intermediate |
Before You Start
Make sure the Vision Starter Kit is connected and reachable from your computer.
You may need to adjust the following values in the code examples:
Network settings
The IP address and port may differ depending on your sensor configuration.
Check the network settings of your Vision Starter Kit before running the code.
Example 1: Connect to the Vision Sensor
Purpose
This example shows how to create a TCP connection to the Vision Sensor using Python.
It can be used as a first connection test to check whether your computer can reach the sensor over the configured IP address and port.
Code
import socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_ip = "192.168.0.1"
server_port = 34170
client.connect((server_ip, server_port))
print("Connected to Vision Sensor")
client.close()
Expected Result
If the connection is successful, the terminal should display:
If the connection fails, check:
- sensor IP address
- sensor port
- network connection
- firewall settings
- network adapter configuration
Example 2: Receive Sensor Messages Continuously
Purpose
This example connects to the sensor and continuously receives incoming messages.
It can be used to test whether the sensor sends data to your Python application.
Code
import socket
def run_client():
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_ip = "192.168.0.1"
server_port = 34170
client.connect((server_ip, server_port))
try:
while True:
message = client.recv(1024)
message = message.decode("utf-8")
print("Received:", message)
except KeyboardInterrupt:
print("Stopping client...")
finally:
client.close()
run_client()
Expected Result
The terminal prints incoming messages from the sensor.
Example:
Stop the script with:
Continuous receiving
This example waits for incoming data from the sensor.
If nothing is printed, check whether the sensor is configured to send result data.
Example 3: Trigger Image Acquisition
Purpose
This example sends simple trigger commands to the sensor.
It can be used to select a job, trigger image acquisition and start image recording from a Python script.
Code
import socket
def trigger_image():
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_ip = "192.168.0.1"
server_port = 34170
client.connect((server_ip, server_port))
client.sendall(b"\x02set job 2\x03")
client.sendall(b"\x02trigger\x03")
client.sendall(b"\x02RecordImages\x03")
print("Trigger commands sent")
client.close()
trigger_image()
Expected Result
The sensor receives the trigger commands and starts the configured image acquisition workflow.
The terminal should display:
Job number
The command set job 2 selects job number 2.
Change this value if your relevant job uses a different job number.
Command format
The characters \x02 and \x03 mark the start and end of the command telegram.
Expected Result
The sensor receives the trigger commands and starts the configured image acquisition workflow.
Job number
The command set job 2 selects job number 2.
Change this value if your relevant job uses a different number.
Troubleshooting
Troubleshooting
Connection refused
Check whether the sensor is reachable and whether the correct port is used.
No response from sensor
Check the Ethernet connection and sensor network settings.
Unexpected message format
Make sure the sensor is configured to send the expected response format.
Next Steps
Use these examples as building blocks for your own Vision Starter Kit projects.