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> MAINTAINER yohan <783b8c87@scimetis.net>
ENV DEBIAN_FRONTEND noninteractive ENV DEBIAN_FRONTEND noninteractive
ENV TZ Europe/Paris 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 ENV PIP_BREAK_SYSTEM_PACKAGES 1
RUN pip install flask-restx==1.3.0 RUN pip install flask-restx==1.3.0
WORKDIR /root WORKDIR /root
COPY find_ttyUSB.sh /root/
COPY relay.py /root/ COPY relay.py /root/
COPY thermostat.py /root/ COPY thermostat.py /root/
ENV FLASK_APP thermostat.py 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 # Pilotage GCE USB 8 Relay Board
import time import time
import serial import serial
import serial.tools.list_ports
import argparse import argparse
import sys import sys
import subprocess import subprocess
@ -34,17 +35,24 @@ else:
#print(relays) #print(relays)
find_ttyUSB_output = subprocess.check_output(["./find_ttyUSB.sh"]) port = None
device = None
for line in find_ttyUSB_output.decode('utf-8').split("\n"): def list_serial_ports():
if "FTDI_FT232R_USB_UART_A50285BI" in line: ports = serial.tools.list_ports.comports()
device = line.split(" ")[0] for port, desc, hwid in sorted(ports):
break print("{}: {} [{}]".format(port, desc, hwid))
if device is None: def find_serial_ports():
print ("Relay board not found.") ports = serial.tools.list_ports.comports()
sys.exit(1) 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): def init_serial(pPort):
global ser global ser
@ -67,7 +75,7 @@ def init_serial(pPort):
print('Could not connect to ' + ser.portstr) print('Could not connect to ' + ser.portstr)
sys.exit(1) sys.exit(1)
init_serial(device) init_serial(port)
if action != 'status': if action != 'status':
for relay in relays: for relay in relays:
command = relay+action+'\r\n' command = relay+action+'\r\n'