How to connect a Kawasaki robot to a Python program over TCP or UDP

Based on contributions by Shiyar.

Kawasaki’s teach pendant environment (KIDE) programs the robot in AS language, not Python. To drive the robot from a Python program on your PC, you open a socket connection between the two: a raw TCP or UDP link, or a higher-level Python library. This article covers both socket methods and points to the alternatives.

What you need

  • A Kawasaki robot controller (D, E, or F series) with KIDE / AS language access
  • The PC and the robot controller on the same network, with both IP addresses known
  • Basic teach pendant setup already done: Kawasaki robot (hands-on)
  • Python 3 (only the built-in socket module is needed)

Steps

1. Choose TCP or UDP

TCP opens a persistent, reliable connection but needs a handshake before data can flow. UDP has no handshake and is simpler and faster, but doesn’t guarantee delivery. Use TCP for a long-lived session where you don’t want to lose commands; use UDP for short, frequent messages where an occasional dropped packet is acceptable.

2. Connect over TCP/IP

Run the Python server first, then start the robot program that connects to it as a client.

Python server (runs on the PC):

import socket
import time

def start_server(host, port):
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind((host, port))
    server_socket.listen(5)
    print(f"Server is listening on {host}:{port}")

    while True:
        client_socket, addr = server_socket.accept()
        print(f"Connected by {addr}")
        while True:
            # To receive data from the robot instead of only sending, use:
            # data = client_socket.recv(1024)
            # if not data:
            #     break
            # print("Received:", data.decode())

            response = '1'
            client_socket.sendall(response.encode())
            time.sleep(2)
            response = '2'
            client_socket.sendall(response.encode())
            time.sleep(3)
        client_socket.close()

if __name__ == '__main__':
    HOST = '192.168.0.2'  # your PC's IP address
    PORT = 10001          # non-privileged port (>1023)
    start_server(HOST, PORT)

This example just alternates sending '1' and '2' for testing; replace the loop body with your own protocol.

Robot program that connects to the PC (AS language, load in KIDE):

.PROGRAM smr2() ; connects to the PC as a TCP client
  tout_open = 60
  ip[1] = 192
  ip[2] = 168
  ip[3] = 0
  ip[4] = 2  ; PC's IP address
  port = 10001
  er_count = 0

connect:
  TIMER (2) = 0
  TCP_CONNECT sock_id1, port, ip[1], tout_open
  IF sock_id1 < 0 THEN
    er_count = er_count + 1
    IF er_count >= 5 THEN
      PRINT "Client Communication with PC has failed"
    ELSE
      PRINT "TCP_CONNECT error id = ", sock_id1, ", error count = ", er_count
      GOTO connect
    END
  ELSE
    PRINT "TCP_CONNECT OK id = ", sock_id1, ", with time elapsed = ", TIMER (2)
  END
.END

Robot program that reacts to received data:

.PROGRAM receive_data() ;
  WHILE TRUE DO
    numbytes = 10
    max_length = 10
    tout_rec = 60
    ret = 0
    TCP_RECV ret, sock_id1, $recv_buf[1], numbytes, tout_rec, max_length
    IF ret < 0 THEN
      PRINT "TCP_RECV error in RECV", ret
      $recv_buf[1] = "000"
    ELSE
      IF numbytes > 0 THEN
        FOR i = 1 TO numbytes
          PRINT "RecBuff[", i, "]= ", $recv_buf[i]
        END
        IF $recv_buf[1] == "1" THEN
          PRINT "IT IS 1"
          SPEED 80 ALWAYS
          JMOVE #[ -100.000,-59.995,9.834,13.745,8.050,50.603]
        END
        IF $recv_buf[1] == "2" THEN
          PRINT "IT IS 2"
          SPEED 80 ALWAYS
          JMOVE #[ -160.000,-59.995,9.834,13.745,8.050,50.603]
        END
      ELSE
        $recv_buf[1] = "000"
        ret = -1
      END
    END
  END
.END

Adjust the joint positions in receive_data to your own program.

3. Or connect over UDP

Robot program (AS language):

.PROGRAM connect_to_pc_by_udp()
  timeout = 120
  answer_timeout = 3
  ip[1] = 192
  ip[2] = 168
  ip[3] = 0
  ip[4] = 10
  port = 10010
  numbytes = 1
  ret = 0
  WHILE TRUE DO
    TWAIT 1
    UDP_RECVFROM ret, port, $cnt[0], numbytes, timeout, ip[1]
    IF ret <> 0 THEN
      PRINT "No data received within timeout period or error code: ", ret
      ; continue to the next iteration without halting
    ELSE
      PRINT "Message: ", $cnt[0]
      ; send a confirmation message back
      $cnt[0] = $ENCODE (/D, numbytes)
      UDP_SENDTO ret, ip[1], port, $cnt[0], 1, answer_timeout
      IF ret <> 0 THEN
        PRINT "Error with the UDP send, code: ", ret
      END
    END
  END
.END

Python side:

import socket
import time

def udp_start(host, port, target_host):
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    server_socket.bind((host, port))
    print("UDP server started.")

    while True:
        input("Press Enter to send data...")
        response = '1'
        print("Sending data")
        server_socket.sendto(response.encode(), target_host)
        time.sleep(2)

if __name__ == '__main__':
    HOST = '192.168.0.2'              # local IP address
    PORT = 10010                      # port to listen on
    TARGET_HOST = ('192.168.0.1', PORT)  # robot IP and port
    udp_start(HOST, PORT, TARGET_HOST)

4. Reference: full manual example

Kawasaki’s own TCP/IP communication manual has worked examples starting around page 46, built from four cooperating programs: open_socket(), send(.ret,.$data), recv(), and close_socket(). The exact calls differ depending on whether the robot acts as server or client. See the Kawasaki TCP/IP Communication Manual for details, and these example programs:

5. Alternative: a bare Python library (experimental)

Instead of hand-rolling the socket protocol, you can use kawapai, an experimental Python library for interfacing directly with Kawasaki D, E, and F series controllers. Its API is still experimental. An improved version of the library (from the Tetrisbot project) is available as tetris bot code incl. improved kawapai library.zip (3.7 MB).

6. Alternative: ROS driver

Kawasaki Heavy Industries publishes an official, unsupported ROS driver. It currently only supports these robot models: duaro1, rs007l, rs007n, rs013n, rs020n, rs025n, rs030n, rs80n.

Troubleshooting

  • TCP_CONNECT keeps failing / “Client Communication with PC has failed”: start the Python server before running the robot’s connect program — the robot is the client here and has nothing to connect to otherwise. Also double check the IP address and port on both sides, and that no firewall is blocking the port.
  • UDP messages seem to go nowhere: UDP does not confirm delivery by itself; make sure both sides use the same port and that the robot program’s UDP_RECVFROM is actually running (loop already started) before you send from Python.

Related


Rewritten and consolidated (Sept 2026) from the original student how-to’s: Connect a Kawasaki robot to a Python program on a computer.