Add message in case of Arduino disconnect at any time

This commit is contained in:
askiiart 2022-11-02 19:26:03 -05:00
parent 0a9de1d69c
commit ff20725b0f

View file

@ -1,8 +1,11 @@
import os
import GPUtil
import time
from pySerialTransfer import pySerialTransfer as txfer
from subprocess import getoutput
try:
import GPUtil
from pySerialTransfer import pySerialTransfer as txfer
from serial import SerialException
except ImportError:
print('Make sure you have installed the required libraries: GPUtil, pySerialTransfer')
try:
# ---
@ -12,6 +15,7 @@ try:
# Then, just remove "/dev/", and the number, from the port, and type it into the variable `port`
# ---
def get_port():
port = 'ttyACM'
while True:
ports = getoutput(f'ls /dev | grep {port}').split('\n')
@ -22,19 +26,20 @@ try:
print('Waiting for Arduino...')
time.sleep(3)
link = txfer.SerialTransfer(port, 115200, timeout=.1)
return port
link = txfer.SerialTransfer(get_port(), 115200, timeout=.1)
link.open()
time.sleep(2)
while True:
try:
time.sleep(1)
gpus = GPUtil.getGPUs()
temp = max(gpu.temperature for gpu in gpus)
speed = int(((temp - 40) / 40) * 100)
min_speed = 0
min_speed = 34
max_speed = 100
if speed < 0:
speed = min_speed
@ -76,6 +81,14 @@ try:
# Evaluate for comm. errors
if speed != rec_int:
print(f'Error: sent {speed}, received {rec_int}')
except OSError or SerialException:
try:
link.close()
except:
pass
link = txfer.SerialTransfer(get_port(), 115200, timeout=.1)
link.open()
except KeyboardInterrupt:
try: