forked from miguelasd688/4-legged-robot-model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUDP_socket_s.py
98 lines (69 loc) · 3.54 KB
/
UDP_socket_s.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 16 14:04:07 2022
@author: miguel-asd
"""
import socket
import sys
import time
class socketUDPserver:
def __init__(self, UDP_IP = "192.168.1.22", UDP_PORT = 5005):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Internet & UDP
self.sock.bind((UDP_IP, UDP_PORT))
print('starting up on {} port {}'.format(UDP_IP, UDP_PORT))
def sendRecvOnline(self ,angles, bodyAngle):
print('\nwaiting to receive message')
data, address = self.sock.recvfrom(4096)
print('received {} bytes from {}'.format(len(data), address))
print(data)
sentdata = "<{0}#{1}#{2}#{3}#{4}#{5}#{6}#{7}#{8}#{9}#{10}#{11}#{12}>"
command = sentdata.format(round(angles[0,0],2),round(angles[0,1],2), round(angles[0,2],2),
round(angles[1,0],2),round(angles[1,1],2), round(angles[1,2],2),
round(angles[2,0],2),round(angles[2,1],2), round(angles[2,2],2),
round(angles[3,0],2),round(angles[3,1],2), round(angles[3,2],2),
round(bodyAngle,2))
print(command)
sent = self.sock.sendto(bytes(command , encoding='utf8'), address)
class socketTCPserver:
def __init__(self, TCP_IP = "192.168.1.22", PORT = 5005):
self.buffersize = 8
self.server_address = (TCP_IP, PORT)
self.socket_TCP = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Internet & TCP
self.socket_TCP.bind(self.server_address)
self.socket_TCP.listen(1)
print('starting up on {} port {}'.format(TCP_IP, PORT))
print('\nwaiting to receive message')
try:
self.connection, self.client_address = self.socket_TCP.accept()
print ('connection from', self.client_address)
time.sleep(0.5)
except KeyboardInterrupt:
print("Switching off server...")
self.socket_TCP.shutdown(2)
print("Server closed")
sys.exit()
# except:
# print("Restarting data input")
# pass
def sendRecvOnline(self ,angles, bodyAngle):
# Receive the data in small chunks and retransmit it
data = self.connection.recv(self.buffersize)
print('received {} bytes from {}'.format(len(data), self.client_address))
print(data)
if data:
sentdata = "<{0}#{1}#{2}#{3}#{4}#{5}#{6}#{7}#{8}#{9}#{10}#{11}#{12}>"
command = sentdata.format(round(angles[0,0],2),round(angles[0,1],2), round(angles[0,2],2),
round(angles[1,0],2),round(angles[1,1],2), round(angles[1,2],2),
round(angles[2,0],2),round(angles[2,1],2), round(angles[2,2],2),
round(angles[3,0],2),round(angles[3,1],2), round(angles[3,2],2),
round(bodyAngle,2))
print(command)
print ('sending data back to the client')
self.connection.sendall(bytes(command , encoding='utf8'))
else:
print ('no more data from', self.client_address)
# Clean up the connection
self.connection.close()
print('server closed')
sys.exit()