[WIP] пытаемся modbus
This commit is contained in:
parent
8a6bfbc62c
commit
12d647d68a
2
.vscode/arduino.json
vendored
2
.vscode/arduino.json
vendored
@ -2,5 +2,5 @@
|
||||
"sketch": "project\\project.ino",
|
||||
"output": "build",
|
||||
"board": "arduino:avr:uno",
|
||||
"port": "COM6"
|
||||
"port": "COM4"
|
||||
}
|
2
.vscode/c_cpp_properties.json
vendored
2
.vscode/c_cpp_properties.json
vendored
@ -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"
|
||||
|
22
main.py
22
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'}
|
||||
|
||||
if __name__ == "__main__":
|
||||
uvicorn.run(app, host="0.0.0.0", port=12346)
|
||||
|
@ -1,6 +1,12 @@
|
||||
#include <ModbusSerial.h>
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user