diff --git a/.vscode/arduino.json b/.vscode/arduino.json index 6490a26..5095d0a 100644 --- a/.vscode/arduino.json +++ b/.vscode/arduino.json @@ -2,5 +2,5 @@ "sketch": "project\\project.ino", "output": "build", "board": "arduino:avr:uno", - "port": "COM6" + "port": "COM4" } \ No newline at end of file diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index e0a2830..69bcd1d 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -18,6 +18,8 @@ "includePath": [ "C:\\Users\\dimae\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\cores\\arduino", "C:\\Users\\dimae\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\variants\\standard", + "C:\\Users\\dimae\\OneDrive\\Документы\\Arduino\\libraries\\Modbus-Serial\\src", + "C:\\Users\\dimae\\OneDrive\\Документы\\Arduino\\libraries\\Modbus-Arduino\\src", "c:\\users\\dimae\\appdata\\local\\arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7\\lib\\gcc\\avr\\7.3.0\\include", "c:\\users\\dimae\\appdata\\local\\arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7\\lib\\gcc\\avr\\7.3.0\\include-fixed", "c:\\users\\dimae\\appdata\\local\\arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7\\avr\\include" diff --git a/main.py b/main.py index e4a1769..a682a59 100644 --- a/main.py +++ b/main.py @@ -1,20 +1,29 @@ from fastapi import FastAPI -import serial +from pymodbus.client import ModbusSerialClient as ModbusClient import uvicorn import threading +import time app = FastAPI() -ser = serial.Serial('COM6', 9600) +# Create a Modbus client instance +client = ModbusClient(method='rtu', port='COM4', baudrate=9600, timeout=1) +client.connect() + +# Shared variable to store the Modbus register value line = '0' -def read_value(): +def read_modbus_value(): global line while True: - if ser.in_waiting > 0: - line = ser.read().decode() + # Read holding register (adjust the register address as needed) + result = client.read_holding_registers(address=1, count=1, unit=1) + if not result.isError(): + line = str(result.registers[0]) + time.sleep(1) # Poll every 1 second -threading.Thread(target=read_value, daemon=True).start() +# Start the Modbus reading in a separate thread +threading.Thread(target=read_modbus_value, daemon=True).start() @app.get("/stat") async def get_stat(): @@ -24,4 +33,5 @@ async def get_stat(): async def root(): return {'message': 'hello world'} -uvicorn.run(app, host="0.0.0.0", port=12346) +if __name__ == "__main__": + uvicorn.run(app, host="0.0.0.0", port=12346) diff --git a/project/project.ino b/project/project.ino index 274298b..b9103bb 100644 --- a/project/project.ino +++ b/project/project.ino @@ -1,6 +1,12 @@ +#include + +// Define pins #define PIN_INPUT 2 #define PIN_LED 13 +// Create Modbus object +ModbusSerial mb(Serial, 1); // Assuming slave ID is 1 + void setup() { Serial.begin(9600); pinMode(PIN_INPUT, INPUT); @@ -9,6 +15,6 @@ void setup() { void loop() { int sensorVal = digitalRead(PIN_INPUT); - Serial.print(sensorVal); // Выводим значение в мониторе порта 3 + mb.task(); // Update Modbus communication delay(500); }