summaryrefslogtreecommitdiff
path: root/PYTHON/COM-порт.md
diff options
context:
space:
mode:
authorvlapa <vlapa@ya.ru>2026-06-13 20:33:26 +0300
committervlapa <vlapa@ya.ru>2026-06-13 20:33:26 +0300
commite5abc1fe99340b979b18864d978179f71fe8f5c5 (patch)
tree98fd0ad7c6d0744894d6e74e446daacd9e6dd1b5 /PYTHON/COM-порт.md
First
Diffstat (limited to 'PYTHON/COM-порт.md')
-rw-r--r--PYTHON/COM-порт.md41
1 files changed, 41 insertions, 0 deletions
diff --git a/PYTHON/COM-порт.md b/PYTHON/COM-порт.md
new file mode 100644
index 0000000..b1706c3
--- /dev/null
+++ b/PYTHON/COM-порт.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.")
+```
+