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 import time
from pySerialTransfer import pySerialTransfer as txfer
from subprocess import getoutput 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: try:
# --- # ---
@ -12,70 +15,80 @@ try:
# Then, just remove "/dev/", and the number, from the port, and type it into the variable `port` # Then, just remove "/dev/", and the number, from the port, and type it into the variable `port`
# --- # ---
port = 'ttyACM' def get_port():
while True: port = 'ttyACM'
ports = getoutput(f'ls /dev | grep {port}').split('\n') while True:
if ports != ['']: ports = getoutput(f'ls /dev | grep {port}').split('\n')
ports.sort() if ports != ['']:
port = ports[len(ports) - 1] ports.sort()
break port = ports[len(ports) - 1]
print('Waiting for Arduino...') break
time.sleep(3) 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() link.open()
time.sleep(2)
while True: while True:
time.sleep(1)
gpus = GPUtil.getGPUs()
temp = max(gpu.temperature for gpu in gpus)
speed = int(((temp - 40) / 40) * 100) try:
min_speed = 0 time.sleep(1)
max_speed = 100 gpus = GPUtil.getGPUs()
if speed < 0: temp = max(gpu.temperature for gpu in gpus)
speed = min_speed
elif speed > 100:
speed = max_speed
print(f'Temp: {temp}') speed = int(((temp - 40) / 40) * 100)
print(f'Speed: {speed}') min_speed = 34
max_speed = 100
if speed < 0:
speed = min_speed
elif speed > 100:
speed = max_speed
################################################################################################################ print(f'Temp: {temp}')
# Send data to the Arduino print(f'Speed: {speed}')
################################################################################################################
# send_size will be increased when data is added to payload ################################################################################################################
send_size = 0 # Send data to the Arduino
################################################################################################################
# Adds data to payload # send_size will be increased when data is added to payload
int_size = link.tx_obj(speed, send_size) - send_size send_size = 0
send_size += int_size
# Sends data to Arduino # Adds data to payload
link.send(send_size) int_size = link.tx_obj(speed, send_size) - send_size
send_size += int_size
# Waits for response from Arduino, and reports errors while receiving packets # Sends data to Arduino
while not link.available(): link.send(send_size)
if link.status < 0:
if link.status == txfer.CRC_ERROR:
print('Error: CRC_ERROR')
elif link.status == txfer.PAYLOAD_ERROR:
print('Error: PAYLOAD_ERROR')
elif link.status == txfer.STOP_BYTE_ERROR:
print('Error: STOP_BYTE_ERROR')
else:
print('Error: {}'.format(link.status))
# Parse response from Arduino # Waits for response from Arduino, and reports errors while receiving packets
rec_int = link.rx_obj(obj_type=int, obj_byte_size=int_size, start_pos=(send_size - int_size)) while not link.available():
if link.status < 0:
if link.status == txfer.CRC_ERROR:
print('Error: CRC_ERROR')
elif link.status == txfer.PAYLOAD_ERROR:
print('Error: PAYLOAD_ERROR')
elif link.status == txfer.STOP_BYTE_ERROR:
print('Error: STOP_BYTE_ERROR')
else:
print('Error: {}'.format(link.status))
# Parse response from Arduino
rec_int = link.rx_obj(obj_type=int, obj_byte_size=int_size, start_pos=(send_size - int_size))
# 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()
# Evaluate for comm. errors
if speed != rec_int:
print(f'Error: sent {speed}, received {rec_int}')
except KeyboardInterrupt: except KeyboardInterrupt:
try: try: