-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient2.py
More file actions
68 lines (56 loc) · 2.27 KB
/
Copy pathclient2.py
File metadata and controls
68 lines (56 loc) · 2.27 KB
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
'''
Note: This script contacts the server through Port 5000. It is known that
National Instrument's LabView also uses this portal. So tagsrv.exe needs to be
terminated first. This is not a problem on Raspberry Pi.
This version has more than one sensor.
'''
import socket
import time
port = 5000
serverIPLst = ['192.168.2.19','192.168.2.20','192.168.2.21']
camNameLST = ['Cam1','Cam2','Cam3']
def decode_config_message(msg_bit):
"""Decode and remove all non-ASCII char's."""
return ''.join([x for x in msg_bit.decode() if ord(x) < 127 and ord(x) > 31])
def reconnect(ServerIp, PORT):
"""Reconnect"""
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # TCP
server_address = (ServerIp, PORT)
server_socket.connect(server_address)
return server_socket
def send_data(server_socket, data_str):
server_socket.sendall(data_str.encode())
def receive_data(server_socket):
flag = False
start_time = time.time()
while True:
try:
receive_data = server_socket.recv(4096)
print(receive_data)
real_data = decode_config_message(receive_data)
print(f'The message is {real_data}')
if len(real_data) > 0:
flag = True
break
except socket.error as e:
print(f"Socket error: {e}")
break
now_time = time.time()
if now_time - start_time > 1:
break
return flag
while True:
instr = input("Type 'START' to start all recordings. 'STOP saveFileName' to stop and save file to the saveFileName. 'TAKE saveFileName' to take one shot and save file. WIPE to erase all data. CANCEL to terminate program. \n")
if "START" in instr or "STOP" in instr or "TAKE" in instr or "WIPE" in instr:
for serverIP, camName in zip(serverIPLst, camNameLST):
try:
server_socket = reconnect(serverIP, port)
instr2 = instr + "_" + camName
send_data(server_socket, instr2)
if receive_data(server_socket):
print("Successful communication!")
server_socket.close()
except socket.error as e:
print(f"Error connecting to {serverIP}:{port} - {e}")
elif "CANCEL" in instr:
break