From e5abc1fe99340b979b18864d978179f71fe8f5c5 Mon Sep 17 00:00:00 2001 From: vlapa Date: Sat, 13 Jun 2026 20:33:26 +0300 Subject: First --- "PYTHON/COM-\320\277\320\276\321\200\321\202.md" | 41 ++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 "PYTHON/COM-\320\277\320\276\321\200\321\202.md" (limited to 'PYTHON/COM-порт.md') diff --git "a/PYTHON/COM-\320\277\320\276\321\200\321\202.md" "b/PYTHON/COM-\320\277\320\276\321\200\321\202.md" new file mode 100644 index 0000000..b1706c3 --- /dev/null +++ "b/PYTHON/COM-\320\277\320\276\321\200\321\202.md" @@ -0,0 +1,41 @@ +```python +import serial + +from serial.tools import list_ports +ports = list(list_ports.comports()) +i = 1 +for p in ports: + print(i, ' - ', p.device) + i += 1 + +port = 8 # int(input('Введите номер COM-порта: ')) +baudrate = 115200 # int(input('Введите скорость COM-порта: ')) + +while port > len(ports): + print("Такого порта - НЕТ !") + port = int(input('Введите номер COM-порта: ')) + +print(ports[port - 1].device) + +try: + ser = serial.Serial(ports[port - 1].device, baudrate=baudrate) + print("Serial connection established.") + + while True: + line = ser.readline().decode().strip() + + if line: + print(line) + +except serial.SerialException as se: + print("Serial port error:", str(se)) + +except KeyboardInterrupt: + pass + +finally: + if ser.is_open: + ser.close() + print("Serial connection closed.") +``` + -- cgit v1.2.3