-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUDP_conns.py
165 lines (145 loc) · 4.68 KB
/
UDP_conns.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import socket
import threading
import json
# default configs
UDP_IP = "127.0.0.1"
CONN_PORT = 62011
CONN_TO_PORT = 62012
MODE_PORT = 62013
MODE_TO_PORT = 62014
M_DATA_PORT = 62015
M_DATA_TO_PORT = 62016
CALI_PORT = 62017
CALI_TO_PORT = 62018
# M_VAL_PORT = 62019
# M_VAL_TO_PORT = 62020
# SAFE_PORT = 62021
# SAFE_TO_PORT = 62022
CURR_MODE = 0
# _LOCK_CURR_MODE = threading.Lock()
def connection(
my_port,
conn_port,
my_ip=UDP_IP,
conn_ip=UDP_IP
):
global CURR_MODE
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((my_ip, my_port))
# release right after program finishes
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print("Starting -", threading.currentThread().getName())
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print("[{}]".format(threading.currentThread().getName()), "R", data.decode())
msg = str(float(CURR_MODE))
sock.sendto(msg.encode(), (conn_ip, conn_port))
print("[{}]".format(threading.currentThread().getName()), "S", msg)
def mode(
my_port,
conn_port,
my_ip=UDP_IP,
conn_ip=UDP_IP
):
global CURR_MODE
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((my_ip, my_port))
# release right after program finishes
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print("Starting -", threading.currentThread().getName())
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print("[{}]".format(threading.currentThread().getName()), "R", data.decode())
# _LOCK_CURR_MODE.acquire()
CURR_MODE = float(data)
msg = str(float(CURR_MODE))
sock.sendto(msg.encode(), (conn_ip, conn_port))
# _LOCK_CURR_MODE.release()
print("[{}]".format(threading.currentThread().getName()), "S", msg)
# def input_val(
# my_port,
# conn_port,
# my_ip=UDP_IP,
# conn_ip=UDP_IP
# ):
# sock = socket.socket(socket.AF_INET, # Internet
# socket.SOCK_DGRAM) # UDP
# sock.bind((my_ip, my_port))
# # release right after program finishes
# sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# print("Starting - ", threading.currentThread().getName())
# while True:
# msg = input(">>> input for [{}]".format(threading.currentThread().getName()))
# sock.sendto(msg.encode(), (conn_ip, conn_port))
# print("[{}]".format(threading.currentThread().getName()), "S", msg)
def motor_data(
my_port,
conn_port,
my_ip=UDP_IP,
conn_ip=UDP_IP
):
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((my_ip, my_port))
# release right after program finishes
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print("Starting -", threading.currentThread().getName())
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print("[{}]".format(threading.currentThread().getName()), "R", data.decode())
def calibration(
my_port,
conn_port,
my_ip=UDP_IP,
conn_ip=UDP_IP
):
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((my_ip, my_port))
# release right after program finishes
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print("Starting -", threading.currentThread().getName())
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print("[{}]".format(threading.currentThread().getName()), "R", data.decode())
msg = str(float(json.loads(data)['value']))
sock.sendto(msg.encode(), (conn_ip, conn_port))
print("[{}]".format(threading.currentThread().getName()), "S", msg)
t_connection = threading.Thread(
name="connection",
target=connection,
args=(CONN_PORT, CONN_TO_PORT),
)
t_mode = threading.Thread(
name="mode",
target=mode,
args=(MODE_PORT, MODE_TO_PORT),
)
t_motor_data = threading.Thread(
name="motorData",
target=motor_data,
args=(M_DATA_PORT, M_DATA_TO_PORT),
)
t_calibration = threading.Thread(
name="calibration",
target=calibration,
args=(CALI_PORT, CALI_TO_PORT),
)
# t_motor_values = threading.Thread(
# name="motorValues",
# target=input_val,
# args=(M_VAL_PORT, M_VAL_TO_PORT),
# )
# t_safety = threading.Thread(
# name="safety",
# target=input_val,
# args=(SAFE_PORT, SAFE_TO_PORT),
# )
t_connection.start()
t_mode.start()
t_motor_data.start()
t_calibration.start()
# t_motor_values.start()
# t_safety.start()