← Back to Robot Communication series
🤖
Communication
Comm basics · Prereq: lesson 1

03. Ethernet TCP/IP

Connect the robot and a vision (upstream) system with one LAN cable: the robot sends "take a shot" ({TRIG}) and vision replies "here are the coords" ({X,Y,R}). You confirm by hand that HRSS's COPEN/CWRITE/CREAD/CCLEAR map 1:1 to standard PC socket code. You run a vision server on the PC and a robot Client you write, succeeding at real send/receive/parse.

TCP/IPsocketCOPENCWRITECREADvision
Duration
~1–1.5 hours
Level
📊 Beginner
Prerequisite
🎯 Lesson 1
OUTCOME
Understand the same-IP-domain rule, the COPEN/CWRITE/CREAD/CCLEAR flow, and the {}·, packet convention, and talk to the vision server with a Python socket client.

What you'll learn

  • 1Explain the TCP/IP condition (same IP domain, first 3 octets match) and align IPs
  • 2Write the send/receive flow for COPEN(ETH)·CWRITE·CREAD·CCLEAR
  • 3Understand the HRSS packet convention ({ } auto-wrap, , separator) and parse a packet
  • 4Talk to the vision server with a Python socket client (the robot Client)

Introduction

HRSS comms come down to four commands: COPEN/CWRITE/CREAD/CCLEAR. COPEN's first argument is ETH for TCP/IP, SER for RS-232 — one letter changes the channel type entirely. This lesson is all ETH (TCP/IP).

Key concepts

1) The four comm commands

CommandRole (PC equivalent)
COPEN(ETH, HANDLE)open channel (connect)
CWRITE(HANDLE, data)send (sendall)
CREAD(HANDLE, recv)receive (recv)
CCLEAR(HANDLE)clear buffer

2) Same IP domain — must be "in the same neighborhood"

Data only flows within the same IP network domain (first 3 octets match). Robot 192.168.1.10 ↔ vision 192.168.1.20 works (192.168.1 matches), 192.168.2.20 does not. For multiple devices use a HUB; cameras can take power over the LAN via PoE.

3) Packet format — {data} and commas

Braces { } are auto-added at the start/end (sending TRIG → actually {TRIG}), and values are comma-separated. Before testing, check the "Show message" option in the network settings so traffic is visible. Client = robot (connects first); Server = vision (opens a port and waits).

Core example

A robot Client that sends {TRIG} to the vision server and receives/parses {X,Y,R}. Leaving the with block closes the socket (CCLEAR + close).

python
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((host, port))                       # COPEN(ETH, HANDLE)
    s.sendall("{TRIG}".encode())                  # CWRITE(HANDLE, "TRIG")
    data = s.recv(1024).decode()                  # CREAD(HANDLE, ...)
    x, y, r = data.strip().strip("{}").split(",") # strip { } then split on ,
    print(f"X={x}, Y={y}, R={r}")                 # X=123.45, Y=67.89, 30.0
bash
# Terminal A: vision server
python ../../_shared/tcp_echo_server.py --mode vision --port 6000
# Terminal B: robot Client
python src/tcp_vision/main.py    # send {TRIG} → receive {123.45,67.89,30.0}
ℹ️

This Python flow maps 1:1 to HRSS: connect↔COPEN(ETH), sendall↔CWRITE, recv↔CREAD, with-exit↔CCLEAR. HRSS auto-handles the { } wrapping and , separators.

Common mistakes

Q. I get ConnectionRefusedError.

A. The server (Terminal A) isn't up or the port differs. Start the server first and set both --port to 6000.

Q. No communication at all on real hardware.

A. A first-3-octet mismatch is most common (confirm both are 192.168.1.xx). Also check COPEN's first argument is ETH (serial is SER).

Q. It hangs with no reply, or coords always come back 0,0,0.

A. A missing closing } or the server got an unknown request. Confirm you wrapped the send in { ... } and sent exactly TRIG/TRIG2.

Summary

  • Understood the TCP/IP condition (same IP domain, first 3 octets) and topology (HUB·PoE)
  • Learned the four commands COPEN(ETH)/CWRITE/CREAD/CCLEAR and the { }·, convention
  • Succeeded at real send/receive/parse with a robot Client against the PC vision server
  • PC code ↔ HRSS code map 1:1, giving you the skeleton of the real program

Exercises

  1. Send {TRIG2} to receive and parse the negative coords {-45.10,210.00,-15.0}
  2. Drop the closing } from the send to reproduce the hang, and explain why
  3. If the robot IP is 192.168.1.10, write 3 vision IPs that can communicate
Example code / lecture materials

All lecture materials and example code (with simulators, homework, and answers) are openly available on GitHub.

View on GitHub ↗