From 530755db903fa2d67c119c387c6f12bc18d0718d Mon Sep 17 00:00:00 2001 From: Emanuele Trabattoni Date: Tue, 10 Jun 2025 16:37:12 +0200 Subject: [PATCH] Fixato per usare porta seriale motherboard --- .vscode/launch.json | 2 +- docker/docker-compose.yaml | 4 ++-- upsmon/dataexchange.txt | 26 ++++++++++++++++++++++++++ upsmon/sniffer.py | 16 ++++++++++++++++ upsmon/ups.py | 12 +++++++++--- 5 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 upsmon/dataexchange.txt create mode 100644 upsmon/sniffer.py diff --git a/.vscode/launch.json b/.vscode/launch.json index 66cbfda..a08d8af 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -31,7 +31,7 @@ "INFLUXDB_TOKEN":"apiv3_tbEpA8JmIRTfr8Wbw9npD79BcMlFsnV4_jhdt-CdUP53Mos61KBODGaggl2g5oKZZvZrZu3e6mpob6zorhEdbg", "INFLUXDB_URL" : "http://edelweiss-srv:8181", "INFLUXDB_DATABASE" : "edelweiss", - "PORT": "/dev/ttyUSB1", + "PORT": "/dev/ttyS0", "BAUD": "2400", "LOG_FILE": "/tmp/upsmon.log", "LOG_FILE_LVL": "WARNING", diff --git a/docker/docker-compose.yaml b/docker/docker-compose.yaml index 25e27d3..578b181 100644 --- a/docker/docker-compose.yaml +++ b/docker/docker-compose.yaml @@ -34,13 +34,13 @@ services: depends_on: - influxdb3 devices: - - /dev/ttyUSB1:/dev/ttyUSB1 + - /dev/ttyS0:/dev/ttyS0 environment: - INTERVAL=5 - INFLUXDB_TOKEN=apiv3_tbEpA8JmIRTfr8Wbw9npD79BcMlFsnV4_jhdt-CdUP53Mos61KBODGaggl2g5oKZZvZrZu3e6mpob6zorhEdbg - INFLUXDB_URL=http://influxdb3:8181 - INFLUXDB_DATABASE=edelweiss - - PORT=/dev/ttyUSB1 + - PORT=/dev/ttyS0 - BAUD=2400 - LOG_FILE=/tmp/upsmon.log - LOG_FILE_LVL=WARNING diff --git a/upsmon/dataexchange.txt b/upsmon/dataexchange.txt new file mode 100644 index 0000000..1841429 --- /dev/null +++ b/upsmon/dataexchange.txt @@ -0,0 +1,26 @@ +VP req: QMD +UPS resp: (#######WPHVT2K0 ###2000 80 1/1 230 230 04 12.0 +VP req: QRI +UPS resp: (230.0 008 048.0 50.0 +VP req: QHE +UPS resp: (242 218 +VP req: QRI +UPS resp: (230.0 008 048.0 50.0 +VP req: QGS +UPS resp: (230.9 50.0 230.7 50.1 001.0 013 373.7 379.4 054.4 ---.- 027.3 100000000001 +VP req: QMOD +UPS resp: (L +VP req: QWS +UPS resp: (0000000000000000000000000000000000000000000000000000000000000000 +VP req: QBV +UPS resp: (054.5 04 04 096 568 +VP req: QSK1 +UPS resp: (NAK +VP req: QSK2 +UPS resp: (NAK +VP req: QFLAG +UPS resp: (EpbrashczDovegfm +VP req: QBYV +UPS resp: (264 170 +VP req: QBYF +UPS resp: (53.0 47.0 \ No newline at end of file diff --git a/upsmon/sniffer.py b/upsmon/sniffer.py new file mode 100644 index 0000000..9abd153 --- /dev/null +++ b/upsmon/sniffer.py @@ -0,0 +1,16 @@ +import serial + +ups_port = "/dev/ttyUSB1" +usb_fake = "/dev/ttyUSBf" + +ups = serial.Serial(port=ups_port, baudrate=2400, parity="N", bytesize=8, stopbits=1) +vp = serial.Serial(port=usb_fake, baudrate=2400, parity="N", bytesize=8, stopbits=1) + +while True: + vp_request = vp.read_until(b'\r') + print(f'VP req: {vp_request.decode('ascii').strip()}') + ups.write(vp_request) + ups.flush() + ups_response = ups.read_until(b'\r') + print(f'UPS resp: {ups_response.decode('ascii').strip()}') + vp.write(ups_response) \ No newline at end of file diff --git a/upsmon/ups.py b/upsmon/ups.py index 4ed0b6a..4f98c5c 100644 --- a/upsmon/ups.py +++ b/upsmon/ups.py @@ -19,7 +19,8 @@ def send(port: serial.Serial, d: str): port.flush() def receive(port: serial.Serial, d: str) -> str: - r = port.read_until(b'\r').decode('ascii').rstrip() + # Expected response, 46chrs (231.5 230.8 229.2 012 50.0 2.27 27.0 00000001 + r = port.read_until(expected=b'\r').decode('ascii').rstrip() LOGGER.debug(f"{d} : {r}") return r @@ -39,13 +40,16 @@ def bruteforceCommands(port: serial.Serial): send(port, d) receive(port, d) - ################## ###### MAIN ###### ################## def main() -> int: INTERVAL = int(env['INTERVAL']) UPS_COMMAND = "Q1" + UPS_STATUS = "QGS" # (Vin Fin Vout Fout Aout Load% ?1 ?2 VbatInt VbatExt Temp Flags + UPS_BATTERY = "QBV" # (Vbat n1 n2 Charge% RunMins + UPS_MODE = "QMOD" # (Mode [Echo, Line, Battery?] + try: # Init InfluxDB-v3 Client write_client = InfluxDBClient3(host=env['INFLUXDB_URL'], @@ -53,6 +57,7 @@ def main() -> int: database=env['INFLUXDB_DATABASE']) # Init Serial port port = serial.Serial(port=env['PORT'], baudrate=int(env['BAUD']), bytesize=8, parity='N', stopbits=1) + port.flush() except Exception as e: LOGGER.error(e) return 1 @@ -70,7 +75,7 @@ def main() -> int: raw_data = receive(port, UPS_COMMAND).lstrip('(').rstrip().split() if len(raw_data) < 8: LOGGER.error(f"Incomplete data: {raw_data}") - continue + break values = { 'inV': float(raw_data[0]), 'outV': float(raw_data[2]), @@ -92,6 +97,7 @@ def main() -> int: ###### END MAIN LOOP ######### ############################## port.close() + LOGGER.warning("Main thread exited normally") return 0 if __name__ == "__main__":