-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsgData.py
61 lines (47 loc) · 1.64 KB
/
csgData.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
import socket
import threading
import math
import time
import matplotlib.pyplot as plt # type: ignore
# Initialize an empty list to store data points for plotting
data_points = []
def send_data():
while True:
try:
# Connect to the server
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('192.168.123.35', 703)
client_socket.connect(server_address)
# Generate a sinusoidal function with fluctuation
t = time.time()
data = math.sin(t) + 0.5 * math.sin(5 * t) + 0.2 * math.sin(10 * t)
data_str = str(data)
# Print the message before sending it
# print('Sending data:', data_str)
# Add data point to the list for plotting
data_points.append(data)
# Send the data to the server
client_socket.sendall(data_str.encode())
# Close the connection
client_socket.close()
# Plot the graph
plot_graph()
time.sleep(1) # Adjust the interval as needed
except Exception as e:
print("Error:", e)
# Function to plot the graph
def plot_graph():
plt.clf() # Clear the previous plot
plt.plot(data_points, linestyle='-')
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.title('Cardiac Rhythm Monitoring Graph')
plt.ylim(-2, 2) # Adjust the y-axis limits as needed
plt.draw()
plt.pause(0.001) # Pause to update the plot
# Function to start sending data
def start_ecg_machine():
while True:
send_data()
# Start the CSG machine
start_ecg_machine()