Я пытаюсь найти способ убедиться, что соединение установлено. закрыто, поэтому я могу открыть снова.
Извините за мой плохой английский, английский - мой родной язык.
Вот код:
Код: Выделить всё
import serial
class LcrDevice:
def __init__(self, port="4", baudrate=9600):
self.ser = self.create_connection(f'COM{port}', baudrate)
def create_connection(self, port, baudrate):
try:
self.ser = serial.Serial(port, baudrate, timeout=1)
except serial.SerialException as e:
print(f"Erro ao conectar o LCR: {e}")
def query_ascii_values(self, command):
# Envia o comando ao dispositivo
self.ser.write((command + '\n').encode('utf-8'))
# Lê a resposta
response = self.ser.readline().decode('utf-8').strip()
# Converte a resposta em uma lista de valores numéricos
values = [float(value) for value in response.split(',') if value] # Assume separação por vírgula
return values
@property
def name(self):
return self.ser.name
Код: Выделить всё
Exception in thread Thread-1 (start_ws):
Traceback (most recent call last):
File "threading.py", line 1038, in _bootstrap_inner
File "threading.py", line 975, in run
File "main.py", line 32, in start_ws
File "middlewares\LcrDevice.py", line 5, in __init__
File "serial\serialwin32.py", line 33, in __init__
File "serial\serialutil.py", line 244, in __init__
File "serial\serialwin32.py", line 80, in open
File "serial\serialwin32.py", line 222, in _reconfigure_port
serial.serialutil.SerialException: Cannot configure port, something went wrong. Original message: PermissionError(13, 'A device attached to the system is not functioning.', None, 31)`
Код: Выделить всё
if hasattr(self, 'ser') and self.ser.is_open:
try:
self.ser.close()
print("Conexão anterior fechada.")
except Exception as e:
print(f"Erro ao fechar a conexão anterior: {e}")`
Подробнее здесь: https://stackoverflow.com/questions/790 ... e-starting