By the end of this how to you have learned on how to make a socket connection with a doosan robot and a laptop.
Introduction
There are two types of socket connections: “TCP & UDP” in this how to the TCP connection is explained since that one is the most relaible and is used more often then UDP. The difference between TCP and UDP is:
- TCP: Reliable. TCP ensures that all transmitted data arrives correctly and in the right order. This is achieved through error checking, data recovery, and sequencing.
- UDP: Unreliable. UDP provides no guarantees for delivery, order, or integrity of the data. Packets can be lost, corrupted, or arrive out of order.
Ip adress
A socket connection makes it possible to send messages from your computer to for example a robot or a raspberry pi. the only thing you will need to have is ethernet cable and connect that to your laptop and robot. Then you need to figure out what the ip address of the robot is. This is a Ipv4 address and probably goes something like this: 192.168.0.something.
Port
One other thing you’ll need is the port number. This is a number between 0 and 65535. A port specifies what kind of service u are using.
- The ports between 0-1023 are preserved for HTTP, HTTPS,FTP or ssh.
- The ports between 1024-49151 these ports are preserved for Internet Assigned Numbers Authority (IANA)
- Dynamic or Private Ports (49152-65535) these ports are assigned for temporary connections, Therefore these are the perfect ports for our socket connection.
Software Server side (Laptop)
import socket
HOST = '192.168.0.1'
PORT = 56666
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
server_socket, address = s.accept()
Software Client side (Robot)
socket = get_socket_connection(IP, PORT)
How it works
A socket connection consists out of a server and client.
Software Sever side
socket.AF_INET: Specifies that the socket is using the IPv4 addressing scheme.
socket.SOCK_STREAM: Specifies that the socket is a TCP socket.
s.bind: this binds the port number and ip address
with statement: This ensures that the socket is automatically closed when the with block is exited. This is a more convenient and safer way to manage resources.
Software Client side
In this statement you connect to de server using the same port number and ip address of the server.
Code for reading and writing from the client
from DRCF import *
# read
resp, data = server_socket_read(socket, length=4, timeout=60)
received_number = int.from_bytes(data, 'big')
tp_log(str(received_number))
# write
wait(5)
server_socket_write(socket, 72)
This code shows how to send and recieve data from the client side.
In the first line data is getting recieved which is not bigger than 4 bytes and the reading of the data lasts 1 minute if no data is being red then a timeout is being generated. In the second line converted to a int. Then the recieved is logged.
In the second part of the program shows a way to send data to the server