-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
75 lines (54 loc) · 1.75 KB
/
main.py
File metadata and controls
75 lines (54 loc) · 1.75 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
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3.10
# -*- coding: utf-8 -*-
import sys
from time import sleep
import psutil
import serial.tools.list_ports
def conn_arduino():
arduino_port = None
try:
ports = serial.tools.list_ports.comports()
for port in sorted(ports):
active_port = serial.Serial(port.device, timeout=1)
print('Connecting to ' + active_port.name + '...')
sleep(2)
active_port.write(b'HLEO\n')
msg = active_port.readline().decode().rstrip()
if msg == 'HBLN':
print('\"Arduino\" found!')
arduino_port = active_port
break
return arduino_port
except Exception as e:
print('Connection to \"Arduino\" failed:')
print(e)
sys.exit(1)
def send_data(active_port):
try:
sleep(3)
msg = active_port.readline().decode().rstrip()
if msg == 'REDY':
print('\"Arduino\" ready! Start sending data')
while True:
cpu = psutil.cpu_percent()
ram = psutil.virtual_memory().percent
active_port.write(('DATA|' + str(cpu) + '|' + str(ram) + '\n').encode())
sleep(1)
else:
print('\"Arduino\" is not ready.')
except Exception as e:
print('Sending data to \"Arduino\" failed:')
print(e)
sys.exit(1)
if __name__ == '__main__':
print('Comp Info\n')
print('Searching port \"Arduino\"...')
while True:
arduino = conn_arduino()
if arduino is not None:
break
else:
print('\"Arduino\" not found!')
print('Continuing to search \"Arduino\"\n')
if arduino is not None:
send_data(arduino)