Use Python code to detect the correct serial port.

This commit is contained in:
yohan 2024-06-10 20:42:55 +02:00
parent 77085832af
commit 2aad216f39
3 changed files with 19 additions and 22 deletions

View File

@ -2,11 +2,10 @@ FROM debian:bookworm
MAINTAINER yohan <783b8c87@scimetis.net>
ENV DEBIAN_FRONTEND noninteractive
ENV TZ Europe/Paris
RUN apt-get update && apt-get -y install gunicorn sqlite3 python3-pip python3-requests python3-yaml python3-flask python3-serial udev
RUN apt-get update && apt-get -y install gunicorn sqlite3 python3-pip python3-requests python3-yaml python3-flask python3-serial
ENV PIP_BREAK_SYSTEM_PACKAGES 1
RUN pip install flask-restx==1.3.0
WORKDIR /root
COPY find_ttyUSB.sh /root/
COPY relay.py /root/
COPY thermostat.py /root/
ENV FLASK_APP thermostat.py

View File

@ -1,10 +0,0 @@
#!/bin/bash
for sysdevpath in $(find /sys/bus/usb/devices/usb*/ -name dev); do
syspath="${sysdevpath%/dev}"
devname="$(udevadm info -q name -p $syspath)"
[[ "$devname" == "ttyUSB"* ]] || continue
eval "$(udevadm info -q property --export -p $syspath)"
[[ -z "$ID_SERIAL" ]] && exit
echo "/dev/$devname - $ID_SERIAL"
done

View File

@ -2,6 +2,7 @@
# Pilotage GCE USB 8 Relay Board
import time
import serial
import serial.tools.list_ports
import argparse
import sys
import subprocess
@ -34,17 +35,24 @@ else:
#print(relays)
find_ttyUSB_output = subprocess.check_output(["./find_ttyUSB.sh"])
device = None
for line in find_ttyUSB_output.decode('utf-8').split("\n"):
if "FTDI_FT232R_USB_UART_A50285BI" in line:
device = line.split(" ")[0]
break
port = None
if device is None:
print ("Relay board not found.")
sys.exit(1)
def list_serial_ports():
ports = serial.tools.list_ports.comports()
for port, desc, hwid in sorted(ports):
print("{}: {} [{}]".format(port, desc, hwid))
def find_serial_ports():
ports = serial.tools.list_ports.comports()
for port, desc, hwid in sorted(ports):
if "FT232R USB UART" in desc:
return port
port = find_serial_ports()
if port is None:
print("Unable to find serial port for relay board please use one of the following")
list_serial_ports()
sys.exit(2)
def init_serial(pPort):
global ser
@ -67,7 +75,7 @@ def init_serial(pPort):
print('Could not connect to ' + ser.portstr)
sys.exit(1)
init_serial(device)
init_serial(port)
if action != 'status':
for relay in relays:
command = relay+action+'\r\n'