-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_file.py
69 lines (61 loc) · 1.82 KB
/
plot_file.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
# plot_file.py - reads an input file and sends it to the plotter
# SLW 03/21
import time
import websocket
# Global parameter ---------------------------------------
ip = "192.168.1.70:90"
filename = "plot_file"
extension = ".plt"
#---------------------------------------------------------
def send_msg(msg):
try:
ws.send(msg)
result = ws.recv()
except websocket._exceptions.WebSocketTimeoutException:
print("timeout occured")
result = None
return result
#----------------------------------------------------------
def get_buffer_size():
result = send_msg("F")
if result:
buffer_size = int(result.split(':')[1])
return buffer_size
else:
return 0
#----------------------------------------------------------
# open communication channel
ws = websocket.WebSocket()
ws.connect("ws://" + ip, timeout=5)
print("Connected to WebSocket server on IP", ip)
# read input file
if filename.find(extension) < 0:
filename += extension
try:
with open(filename, "r") as f:
lines = f.readlines()
print(len(lines), "lines read")
except IOError:
print("File '" + filename + "' not available or accessible")
lines = None
# send data to plotter
if lines:
buffer_okay = False
for n, l in enumerate(lines):
while True:
buf = get_buffer_size()
if buf > 10000:
buffer_okay = True
elif buf < 1000:
buffer_okay = False
if buffer_okay:
send_msg(l)
if n % 20 == 0:
print(" - {:d} lines sent".format(n))
break;
else:
time.sleep(1)
# finish plot and close connection
#send_msg("H; P")
ws.close()
print("Connection closed")