From c92745d2bc2b2f232eca98faf0ce45f4de7767cb Mon Sep 17 00:00:00 2001 From: Ayzen Date: Wed, 22 Apr 2026 13:00:32 +0300 Subject: [PATCH] microfix and bat file added --- laser_control/protocol.py | 4 +++ run_device_main.bat | 65 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 run_device_main.bat diff --git a/laser_control/protocol.py b/laser_control/protocol.py index 367b0d7..368d5d8 100644 --- a/laser_control/protocol.py +++ b/laser_control/protocol.py @@ -215,9 +215,13 @@ class Protocol: # ---- Raw I/O def send_raw(self, data: bytes) -> None: + if self._serial is None or not self._serial.is_open: + raise CommunicationError("Serial port is not connected") self._serial.write(data) def receive_raw(self, length: int) -> bytes: + if self._serial is None or not self._serial.is_open: + raise CommunicationError("Serial port is not connected") return self._serial.read(length) # ---- Static encoding helpers (no connection required) --------------- diff --git a/run_device_main.bat b/run_device_main.bat new file mode 100644 index 0000000..1239281 --- /dev/null +++ b/run_device_main.bat @@ -0,0 +1,65 @@ +@echo off +setlocal + +set "PROJECT_DIR=%~dp0" +pushd "%PROJECT_DIR%" >nul || ( + echo Failed to switch to project directory: %PROJECT_DIR% + exit /b 1 +) + +set "VENV_DIR=.venv" +set "VENV_PYTHON=%VENV_DIR%\Scripts\python.exe" +set "VENV_ACTIVATE=%VENV_DIR%\Scripts\activate.bat" + +if exist "%VENV_PYTHON%" goto venv_ready + +echo Creating virtual environment... +call :find_python +if errorlevel 1 goto :error + +"%PYTHON_EXE%" %PYTHON_ARGS% -m venv "%VENV_DIR%" +if errorlevel 1 goto :error + +:venv_ready + +call "%VENV_ACTIVATE%" +if errorlevel 1 goto :error + +if exist "requirements.txt" ( + echo Installing dependencies... + python -m pip install --disable-pip-version-check -r requirements.txt + if errorlevel 1 goto :error +) else ( + echo requirements.txt not found. Skipping dependency installation. +) + +echo Starting _device_main.py... +python _device_main.py +set "EXIT_CODE=%ERRORLEVEL%" + +popd >nul +exit /b %EXIT_CODE% + +:find_python +where py >nul 2>&1 +if not errorlevel 1 ( + set "PYTHON_EXE=py" + set "PYTHON_ARGS=-3" + exit /b 0 +) + +where python >nul 2>&1 +if not errorlevel 1 ( + set "PYTHON_EXE=python" + set "PYTHON_ARGS=" + exit /b 0 +) + +echo Python 3 was not found. Install Python and try again. +exit /b 1 + +:error +set "EXIT_CODE=%ERRORLEVEL%" +echo Script failed with exit code %EXIT_CODE%. +popd >nul +exit /b %EXIT_CODE%